Skip to content

Commit

Permalink
feat: implements convert mnemonic to privateKey (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyuguen authored Aug 18, 2022
1 parent 73f8bef commit c71ebf9
Show file tree
Hide file tree
Showing 5 changed files with 692 additions and 669 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## Unreleased

TBD
- [\#64](https://github.com/medibloc/panacea-js/pull/64) feat: implement a function to convert a mnemonic to a secp256k1 private key


## [v2.0.1](https://github.com/medibloc/panacea-js/releases/tag/v2.0.1) - 2022-06-03

Expand Down
13 changes: 13 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,16 @@ assertIsDeliverTxSuccess(result);
const didDocumentWithSeq = await client.getPanaceaClient().getDid(didDocument.id);
console.log(didDocumentWithSeq);
```

## Convert mnemonic to Secp256k1 privateKey

```ts
import { Secp256k1 as CryptoSecp256k1, stringToPath } from "@cosmjs/crypto";
import { Secp256k1 } from "./secp256k1";

const mnemonic = "bulb rail ...";
const hdPath = stringToPath("m/44'/371'/0'/0/0");

const privateKey = await Secp256k1.parseMnemonicToPrivateKey(mnemonic, hdPath);
const {pubkey} = await CryptoSecp256k1.makeKeypair(privateKey);
```
19 changes: 19 additions & 0 deletions src/crypto/secp256k1.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Secp256k1 as CryptoSecp256k1, stringToPath, sha256 } from "@cosmjs/crypto";
import { panacead } from "../testutils";
import { TextEncoder } from "util";
import { Secp256k1 } from "./secp256k1";

describe("Secp256k1", () => {

it("parseMnemonicToPrivateKey", async () => {
const body = "testBody";
const hashedBody = sha256(new TextEncoder().encode(body));
const hdPath = stringToPath("m/44'/371'/0'/0/0");

const privateKey = await Secp256k1.parseMnemonicToPrivateKey(panacead.mnemonic, hdPath);
const {pubkey} = await CryptoSecp256k1.makeKeypair(privateKey);

const signature = await CryptoSecp256k1.createSignature(hashedBody, privateKey)
expect(CryptoSecp256k1.verifySignature(signature, hashedBody, pubkey)).toBeTruthy()
});
});
7 changes: 7 additions & 0 deletions src/crypto/secp256k1.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ecc from "secp256k1";
import { randomBytes } from "crypto";
import { Bip39, EnglishMnemonic, Slip10, Slip10Curve, Slip10RawIndex } from "@cosmjs/crypto"

export class Secp256k1 {
static generatePrivateKey(): Uint8Array {
Expand All @@ -17,4 +18,10 @@ export class Secp256k1 {
static sign(data32: Uint8Array, privKey: Uint8Array): Uint8Array {
return ecc.ecdsaSign(data32, privKey).signature;
}

static async parseMnemonicToPrivateKey(mnemonic: string, hdPath: readonly Slip10RawIndex[]): Promise<Uint8Array> {
const mnemonicChecked = new EnglishMnemonic(mnemonic);
const seed = await Bip39.mnemonicToSeed(mnemonicChecked, "");
return Slip10.derivePath(Slip10Curve.Secp256k1, seed, hdPath).privkey;
}
}
Loading

0 comments on commit c71ebf9

Please sign in to comment.