Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implements convert mnemonic to privateKey #64

Merged
merged 12 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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