mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-11 14:09:15 +00:00
proto-signing: Rearrange DirectSecp256k1HdWallet options
This commit is contained in:
parent
646fe7cc55
commit
e6f821e2ce
@ -1,4 +1,4 @@
|
||||
import { coins } from "@cosmjs/amino";
|
||||
import { coins, makeCosmoshubPath } from "@cosmjs/amino";
|
||||
import { Secp256k1, Secp256k1Signature, sha256 } from "@cosmjs/crypto";
|
||||
import { fromBase64, fromHex } from "@cosmjs/encoding";
|
||||
|
||||
@ -19,6 +19,17 @@ describe("DirectSecp256k1HdWallet", () => {
|
||||
expect(wallet).toBeTruthy();
|
||||
expect(wallet.mnemonic).toEqual(defaultMnemonic);
|
||||
});
|
||||
|
||||
it("works with options", async () => {
|
||||
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(defaultMnemonic, {
|
||||
bip39Password: "password123",
|
||||
hdPath: makeCosmoshubPath(123),
|
||||
prefix: "yolo",
|
||||
});
|
||||
expect(wallet.mnemonic).toEqual(defaultMnemonic);
|
||||
expect((wallet as any).pubkey).not.toEqual(defaultPubkey);
|
||||
expect((wallet as any).address.slice(0, 4)).toEqual("yolo");
|
||||
});
|
||||
});
|
||||
|
||||
describe("generate", () => {
|
||||
|
@ -23,22 +23,39 @@ interface Secp256k1Derivation {
|
||||
readonly prefix: string;
|
||||
}
|
||||
|
||||
export interface DirectSecp256k1HdWalletOptions {
|
||||
/** The password to use when deriving a BIP39 seed from a mnemonic. */
|
||||
readonly bip39Password: string;
|
||||
/** The BIP-32/SLIP-10 derivation path. Defaults to the Cosmos Hub/ATOM path `m/44'/118'/0'/0/0`. */
|
||||
readonly hdPath: HdPath;
|
||||
/** The bech32 address prefix (human readable part). Defaults to "cosmos". */
|
||||
readonly prefix: string;
|
||||
}
|
||||
|
||||
const defaultOptions: DirectSecp256k1HdWalletOptions = {
|
||||
bip39Password: "",
|
||||
hdPath: makeCosmoshubPath(0),
|
||||
prefix: "cosmos",
|
||||
};
|
||||
|
||||
/** A wallet for protobuf based signing using SIGN_MODE_DIRECT */
|
||||
export class DirectSecp256k1HdWallet implements OfflineDirectSigner {
|
||||
/**
|
||||
* Restores a wallet from the given BIP39 mnemonic.
|
||||
*
|
||||
* @param mnemonic Any valid English mnemonic.
|
||||
* @param hdPath The BIP-32/SLIP-10 derivation path. Defaults to the Cosmos Hub/ATOM path `m/44'/118'/0'/0/0`.
|
||||
* @param prefix The bech32 address prefix (human readable part). Defaults to "cosmos".
|
||||
* @param options An optional `DirectSecp256k1HdWalletOptions` object optionally containing a bip39Password, hdPath, and prefix.
|
||||
*/
|
||||
public static async fromMnemonic(
|
||||
mnemonic: string,
|
||||
hdPath: HdPath = makeCosmoshubPath(0),
|
||||
prefix = "cosmos",
|
||||
options: Partial<DirectSecp256k1HdWalletOptions> = {},
|
||||
): Promise<DirectSecp256k1HdWallet> {
|
||||
const { bip39Password, hdPath, prefix } = {
|
||||
...defaultOptions,
|
||||
...options,
|
||||
};
|
||||
const mnemonicChecked = new EnglishMnemonic(mnemonic);
|
||||
const seed = await Bip39.mnemonicToSeed(mnemonicChecked);
|
||||
const seed = await Bip39.mnemonicToSeed(mnemonicChecked, bip39Password);
|
||||
const { privkey } = Slip10.derivePath(Slip10Curve.Secp256k1, seed, hdPath);
|
||||
const uncompressed = (await Secp256k1.makeKeypair(privkey)).pubkey;
|
||||
return new DirectSecp256k1HdWallet(
|
||||
@ -65,7 +82,7 @@ export class DirectSecp256k1HdWallet implements OfflineDirectSigner {
|
||||
const entropyLength = 4 * Math.floor((11 * length) / 33);
|
||||
const entropy = Random.getBytes(entropyLength);
|
||||
const mnemonic = Bip39.encode(entropy);
|
||||
return DirectSecp256k1HdWallet.fromMnemonic(mnemonic.toString(), hdPath, prefix);
|
||||
return DirectSecp256k1HdWallet.fromMnemonic(mnemonic.toString(), { hdPath: hdPath, prefix: prefix });
|
||||
}
|
||||
|
||||
/** Base secret */
|
||||
|
@ -10,7 +10,7 @@ export {
|
||||
TsProtoGeneratedType,
|
||||
PbjsGeneratedType,
|
||||
} from "./registry";
|
||||
export { DirectSecp256k1HdWallet } from "./directsecp256k1hdwallet";
|
||||
export { DirectSecp256k1HdWallet, DirectSecp256k1HdWalletOptions } from "./directsecp256k1hdwallet";
|
||||
export { DirectSecp256k1Wallet } from "./directsecp256k1wallet";
|
||||
export { decodePubkey, encodePubkey } from "./pubkey";
|
||||
export {
|
||||
|
Loading…
x
Reference in New Issue
Block a user