diff --git a/packages/sdk38/src/wallet.spec.ts b/packages/sdk38/src/wallet.spec.ts index c016b1a8..d9992ad2 100644 --- a/packages/sdk38/src/wallet.spec.ts +++ b/packages/sdk38/src/wallet.spec.ts @@ -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", () => { diff --git a/packages/sdk38/src/wallet.ts b/packages/sdk38/src/wallet.ts index c252d9a7..1efb4075 100644 --- a/packages/sdk38/src/wallet.ts +++ b/packages/sdk38/src/wallet.ts @@ -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 { - 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); } diff --git a/packages/sdk38/types/wallet.d.ts b/packages/sdk38/types/wallet.d.ts index 463ce554..0c29481d 100644 --- a/packages/sdk38/types/wallet.d.ts +++ b/packages/sdk38/types/wallet.d.ts @@ -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; + 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; sign(address: string, message: Uint8Array, prehashType?: PrehashType): Promise;