Skip to content

Commit

Permalink
add secp256k1 bip32 derivation
Browse files Browse the repository at this point in the history
  • Loading branch information
joyqvq committed Sep 8, 2022
1 parent ce826be commit 9aac9ec
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"dependencies": {
"@mysten/sui.js": "workspace:*",
"@reduxjs/toolkit": "^1.8.3",
"@scure/bip32": "^1.1.0",
"bip39-light": "^1.0.7",
"bootstrap-icons": "^1.9.1",
"buffer": "^6.0.3",
Expand Down
6 changes: 5 additions & 1 deletion wallet/src/shared/cryptography/mnemonics.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { Base64DataBuffer, Ed25519Keypair, Secp256k1Keypair } from '@mysten/sui.js';
import {
Base64DataBuffer,
Ed25519Keypair,
Secp256k1Keypair,
} from '@mysten/sui.js';
import { describe, it, expect } from 'vitest';

import {
Expand Down
31 changes: 12 additions & 19 deletions wallet/src/shared/cryptography/mnemonics.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { HDKey } from '@scure/bip32';
import bip39 from 'bip39-light';
import { derivePath, getPublicKey } from 'ed25519-hd-key';
import nacl from 'tweetnacl';

import BIP32Factory from 'bip32';
import { BIP32Interface } from 'bip32';

import type { Ed25519KeypairData, Secp256k1KeypairData } from '@mysten/sui.js';

/**
Expand Down Expand Up @@ -62,11 +60,7 @@ export function normalizeMnemonics(mnemonics: string): string {
* @param path path string (e.g. `m/44'/784'/0'/0'/0'`)
*/
export function isValidHardenedPath(path: string): boolean {
if (
!new RegExp("^m\\/44'\\/784'\\/0'\\/[0-9]+'\\/[0-9]+'+$").test(
path
)
) {
if (!new RegExp("^m\\/44'\\/784'\\/0'\\/[0-9]+'\\/[0-9]+'+$").test(path)) {
return false;
}
return true;
Expand Down Expand Up @@ -101,25 +95,20 @@ export function deriveKeypairFromMnemonics(
*
* @param path path string (e.g. `m/44'/784'/1'/0/0`)
*/
export function isValidBIP32Path(path: string): boolean {
if (
!new RegExp("^m\\/44'\\/784'\\/1'\\/[0-9]+\\/[0-9]+$").test(
path
)
) {
export function isValidBIP32Path(path: string): boolean {
if (!new RegExp("^m\\/44'\\/784'\\/1'\\/[0-9]+\\/[0-9]+$").test(path)) {
return false;
}
return true;
}


/**
* Derive Secp256k1 public key and private key from the Mnemonics using BIP32 derivation path.
* @param mnemonics a 12-word seed phrase
* @param path path string (`m/44'/784'/1'/0/0`)
* @returns public key and private key
*/
export function deriveSecp256k1KeypairFromMnemonics(
export function deriveSecp256k1KeypairFromMnemonics(
path: string,
mnemonics: string
): Secp256k1KeypairData {
Expand All @@ -131,11 +120,15 @@ export function deriveKeypairFromMnemonics(
if (!validateMnemonics(normalized)) {
throw new Error('Invalid mnemonics');
}
const key = HDKey.fromMasterSeed(bip39.mnemonicToSeed(normalized)).derive(
path
);

const { key } = derivePath(path, bip39.mnemonicToSeedHex(normalized));
if (key.privateKey === null || key.publicKey === null) {
throw new Error('Invalid derivation path');
}

return { publicKey: getPublicKey(key), secretKey: key };
return { publicKey: key.publicKey, secretKey: key.privateKey };
}


export const validateMnemonics = bip39.validateMnemonic;

0 comments on commit 9aac9ec

Please sign in to comment.