mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-11 14:09:15 +00:00
Add Secp256k1Wallet.generate
This commit is contained in:
parent
2a47d8b580
commit
57914c2a61
@ -18,6 +18,21 @@ describe("Secp256k1Wallet", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("generate", () => {
|
||||
it("defaults to 12 words", async () => {
|
||||
const wallet = await Secp256k1Wallet.generate();
|
||||
expect(wallet.mnemonic.split(" ").length).toEqual(12);
|
||||
});
|
||||
|
||||
it("can use different mnemonic lengths", async () => {
|
||||
expect((await Secp256k1Wallet.generate(12)).mnemonic.split(" ").length).toEqual(12);
|
||||
expect((await Secp256k1Wallet.generate(15)).mnemonic.split(" ").length).toEqual(15);
|
||||
expect((await Secp256k1Wallet.generate(18)).mnemonic.split(" ").length).toEqual(18);
|
||||
expect((await Secp256k1Wallet.generate(21)).mnemonic.split(" ").length).toEqual(21);
|
||||
expect((await Secp256k1Wallet.generate(24)).mnemonic.split(" ").length).toEqual(24);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAccounts", () => {
|
||||
it("resolves to a list of accounts if enabled", async () => {
|
||||
const wallet = await Secp256k1Wallet.fromMnemonic(defaultMnemonic);
|
||||
|
@ -1,6 +1,7 @@
|
||||
import {
|
||||
Bip39,
|
||||
EnglishMnemonic,
|
||||
Random,
|
||||
Secp256k1,
|
||||
Sha256,
|
||||
Sha512,
|
||||
@ -76,6 +77,23 @@ export class Secp256k1Wallet implements OfflineSigner {
|
||||
return new Secp256k1Wallet(mnemonic, privkey, Secp256k1.compressPubkey(uncompressed), prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a new wallet with a BIP39 mnemonic of the given length.
|
||||
*
|
||||
* @param length The number of words in the mnemonic (12, 15, 18, 21 or 24).
|
||||
* @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".
|
||||
*/
|
||||
public static async generate(
|
||||
length: 12 | 15 | 18 | 21 | 24 = 12,
|
||||
hdPath: readonly Slip10RawIndex[] = makeCosmoshubPath(0),
|
||||
prefix = "cosmos",
|
||||
): Promise<Secp256k1Wallet> {
|
||||
const entropyLength = 4 * Math.floor((11 * length) / 33);
|
||||
const entropy = Random.getBytes(entropyLength);
|
||||
const mnemonic = Bip39.encode(entropy);
|
||||
return Secp256k1Wallet.fromMnemonic(mnemonic.toString(), hdPath, prefix);
|
||||
}
|
||||
|
||||
private readonly mnemonicData: EnglishMnemonic;
|
||||
private readonly pubkey: Uint8Array;
|
||||
|
12
packages/sdk38/types/wallet.d.ts
vendored
12
packages/sdk38/types/wallet.d.ts
vendored
@ -28,6 +28,18 @@ export declare class Secp256k1Wallet implements OfflineSigner {
|
||||
hdPath?: readonly Slip10RawIndex[],
|
||||
prefix?: string,
|
||||
): Promise<Secp256k1Wallet>;
|
||||
/**
|
||||
* Generates a new wallet with a BIP39 mnemonic of the given length.
|
||||
*
|
||||
* @param length The number of words in the mnemonic (12, 15, 18, 21 or 24).
|
||||
* @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".
|
||||
*/
|
||||
static generate(
|
||||
length?: 12 | 15 | 18 | 21 | 24,
|
||||
hdPath?: readonly Slip10RawIndex[],
|
||||
prefix?: string,
|
||||
): Promise<Secp256k1Wallet>;
|
||||
private readonly mnemonicData;
|
||||
private readonly pubkey;
|
||||
private readonly privkey;
|
||||
|
Loading…
x
Reference in New Issue
Block a user