mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-11 14:09:15 +00:00
Add mnemonic storage to Secp256k1Wallet
This commit is contained in:
parent
8798e7046f
commit
2a47d8b580
@ -10,9 +10,12 @@ describe("Secp256k1Wallet", () => {
|
||||
const defaultPubkey = fromHex("02baa4ef93f2ce84592a49b1d729c074eab640112522a7a89f7d03ebab21ded7b6");
|
||||
const defaultAddress = "cosmos1jhg0e7s6gn44tfc5k37kr04sznyhedtc9rzys5";
|
||||
|
||||
it("can be constructed", async () => {
|
||||
const wallet = await Secp256k1Wallet.fromMnemonic(defaultMnemonic);
|
||||
expect(wallet).toBeTruthy();
|
||||
describe("fromMnemonic", () => {
|
||||
it("works", async () => {
|
||||
const wallet = await Secp256k1Wallet.fromMnemonic(defaultMnemonic);
|
||||
expect(wallet).toBeTruthy();
|
||||
expect(wallet.mnemonic).toEqual(defaultMnemonic);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAccounts", () => {
|
||||
|
@ -65,27 +65,35 @@ export function makeCosmoshubPath(a: number): readonly Slip10RawIndex[] {
|
||||
|
||||
export class Secp256k1Wallet implements OfflineSigner {
|
||||
public static async fromMnemonic(
|
||||
mnemonic: string,
|
||||
mnemonicInput: string,
|
||||
hdPath: readonly Slip10RawIndex[] = makeCosmoshubPath(0),
|
||||
prefix = "cosmos",
|
||||
): Promise<Secp256k1Wallet> {
|
||||
const seed = await Bip39.mnemonicToSeed(new EnglishMnemonic(mnemonic));
|
||||
const mnemonic = new EnglishMnemonic(mnemonicInput);
|
||||
const seed = await Bip39.mnemonicToSeed(mnemonic);
|
||||
const { privkey } = Slip10.derivePath(Slip10Curve.Secp256k1, seed, hdPath);
|
||||
const uncompressed = (await Secp256k1.makeKeypair(privkey)).pubkey;
|
||||
return new Secp256k1Wallet(privkey, Secp256k1.compressPubkey(uncompressed), prefix);
|
||||
return new Secp256k1Wallet(mnemonic, privkey, Secp256k1.compressPubkey(uncompressed), prefix);
|
||||
}
|
||||
|
||||
|
||||
private readonly mnemonicData: EnglishMnemonic;
|
||||
private readonly pubkey: Uint8Array;
|
||||
private readonly privkey: Uint8Array;
|
||||
private readonly prefix: string;
|
||||
private readonly algo: Algo = "secp256k1";
|
||||
|
||||
private constructor(privkey: Uint8Array, pubkey: Uint8Array, prefix: string) {
|
||||
private constructor(mnemonic: EnglishMnemonic, privkey: Uint8Array, pubkey: Uint8Array, prefix: string) {
|
||||
this.mnemonicData = mnemonic;
|
||||
this.privkey = privkey;
|
||||
this.pubkey = pubkey;
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
public get mnemonic(): string {
|
||||
return this.mnemonicData.toString();
|
||||
}
|
||||
|
||||
private get address(): string {
|
||||
return rawSecp256k1PubkeyToAddress(this.pubkey, this.prefix);
|
||||
}
|
||||
|
4
packages/sdk38/types/wallet.d.ts
vendored
4
packages/sdk38/types/wallet.d.ts
vendored
@ -24,15 +24,17 @@ export interface OfflineSigner {
|
||||
export declare function makeCosmoshubPath(a: number): readonly Slip10RawIndex[];
|
||||
export declare class Secp256k1Wallet implements OfflineSigner {
|
||||
static fromMnemonic(
|
||||
mnemonic: string,
|
||||
mnemonicInput: string,
|
||||
hdPath?: readonly Slip10RawIndex[],
|
||||
prefix?: string,
|
||||
): Promise<Secp256k1Wallet>;
|
||||
private readonly mnemonicData;
|
||||
private readonly pubkey;
|
||||
private readonly privkey;
|
||||
private readonly prefix;
|
||||
private readonly algo;
|
||||
private constructor();
|
||||
get mnemonic(): string;
|
||||
private get address();
|
||||
getAccounts(): Promise<readonly AccountData[]>;
|
||||
sign(address: string, message: Uint8Array, prehashType?: PrehashType): Promise<StdSignature>;
|
||||
|
Loading…
x
Reference in New Issue
Block a user