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 7 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: implements convert mnemonic to privateKey of Secp256k1
gyuguen marked this conversation as resolved.
Show resolved Hide resolved


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

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

## Convert mnemonic to Secp256k1 privateKey

```ts
import {Secp256k1 as CryptoSecp256k1, stringToPath, sha256} from "@cosmjs/crypto";
import {panacead} from "../testutils";
import {TextEncoder} from "util";
import {Secp256k1} from "./secp256k1";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import {Secp256k1 as CryptoSecp256k1, stringToPath, sha256} from "@cosmjs/crypto";
import {panacead} from "../testutils";
import {TextEncoder} from "util";
import {Secp256k1} from "./secp256k1";
import { Secp256k1 as CryptoSecp256k1, stringToPath } from "@cosmjs/crypto";
import { Secp256k1 } from "@medibloc/panacea-js";


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()
});
});
35 changes: 21 additions & 14 deletions src/crypto/secp256k1.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import ecc from "secp256k1";
import { randomBytes } from "crypto";
import {randomBytes} from "crypto";
import {Bip39, EnglishMnemonic, Slip10, Slip10Curve, Slip10RawIndex} from "@cosmjs/crypto"
gyuguen marked this conversation as resolved.
Show resolved Hide resolved

export class Secp256k1 {
static generatePrivateKey(): Uint8Array {
let privKey
do {
privKey = randomBytes(32)
} while (!ecc.privateKeyVerify(privKey));
return privKey;
}
static generatePrivateKey(): Uint8Array {
gyuguen marked this conversation as resolved.
Show resolved Hide resolved
let privKey
do {
privKey = randomBytes(32)
} while (!ecc.privateKeyVerify(privKey));
return privKey;
}

static getPublicKeyCompressed(privKey: Uint8Array): Uint8Array {
return ecc.publicKeyCreate(privKey);
}
static getPublicKeyCompressed(privKey: Uint8Array): Uint8Array {
return ecc.publicKeyCreate(privKey);
}

static sign(data32: Uint8Array, privKey: Uint8Array): Uint8Array {
return ecc.ecdsaSign(data32, privKey).signature;
}
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
gyuguen marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading