Skip to content

Commit

Permalink
Merge pull request #413 from casper-ecosystem/hotfix-hdkey-issues
Browse files Browse the repository at this point in the history
fix hdkey issues
  • Loading branch information
hoffmannjan authored Apr 18, 2024
2 parents e74c611 + d7150bd commit 73bc0a7
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 41 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Removed
-->

## Unreleased

### Fixed

- invalid seed usage in the HDKey ([#377](https://github.com/casper-ecosystem/casper-js-sdk/pull/377))

## [2.15.4] - 2024-02-12

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions src/lib/CasperHDKeys/CasperHDKey.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import { wordlist as chiWordlist } from '@scure/bip39/wordlists/simplified-chinese';
import { wordlist as engWordlist } from '@scure/bip39/wordlists/english';

import { CasperHDKey } from './CasperHDKey';

Expand All @@ -13,6 +14,7 @@ describe('CasperHDKey', () => {
it('should set user specific wordlist', () => {
CasperHDKey.setWordlist(chiWordlist);
expect(CasperHDKey.getWordlist()).eq(chiWordlist);
CasperHDKey.setWordlist(engWordlist);
});

it('should generate 24 length mnemonic', () => {
Expand Down
26 changes: 8 additions & 18 deletions src/lib/CasperHDKeys/CasperHDKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ export abstract class CasperHDKey<AsymmetricKey> {
// Registered at https://github.com/satoshilabs/slips/blob/master/slip-0044.md
static readonly bip44Index = 506;

// TODO: Check if seed can be public
constructor(
private seed: Uint8Array,
private signatureAlorithm: SignatureAlgorithm
public seed: Uint8Array,
public signatureAlgorithm: SignatureAlgorithm
) {}

// see https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki#path-levels
Expand Down Expand Up @@ -54,8 +55,11 @@ export abstract class CasperHDKey<AsymmetricKey> {
* @param mnemonic word array
* @returns relevant `Uint8Array`
*/
public static mnemonicToSeed(mnemonic: string): Uint8Array {
return bip39.mnemonicToEntropy(mnemonic, CasperHDKey.getWordlist());
public static mnemonicToSeed(
mnemonic: string,
passphrase?: string
): Uint8Array {
return bip39.mnemonicToSeedSync(mnemonic, passphrase);
}

/**
Expand Down Expand Up @@ -91,20 +95,6 @@ export abstract class CasperHDKey<AsymmetricKey> {
return engWordlist;
}

/**
* Returns SignatureAlgorithm
*/
public get signatureAlgorithm(): SignatureAlgorithm {
return this.signatureAlorithm;
}

/**
* Returns current wallet's mnemonic
*/
public get mnemonic(): string {
return bip39.entropyToMnemonic(this.seed, CasperHDKey.getWordlist());
}

/**
* Derive the child key based on BIP44
* @param index index of the child
Expand Down
18 changes: 8 additions & 10 deletions src/lib/CasperHDKeys/Ed25519HDKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ describe('Ed25519HDKey', () => {
]
},
{
seed:
'fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542',
seed: 'fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542',
vectors: [
{
chain: 'm',
Expand Down Expand Up @@ -103,17 +102,16 @@ describe('Ed25519HDKey', () => {
}
];
it('should generate key from mnemonic', () => {
const key = Ed25519HDKey.new();
const mn =
'equip will roof matter pink blind book anxiety banner elbow sun young';

const key0 = key.deriveChild(1);
const hdKey = Ed25519HDKey.fromMnemonic(mn);

const mn = key.mnemonic;
const key0 = hdKey.deriveChild(0);

const recoveredKey = Ed25519HDKey.fromMnemonic(mn);

const recoveredKey0 = recoveredKey.deriveChild(1);

expect(key0.accountHex()).eq(recoveredKey0.accountHex());
expect(key0.accountHex(false)).to.eq(
'016ca12581a38658e71826aa124ffb9f3e3fdc4687b2379da609b187a5b11a80f1'
);
});

it('should generate r+s signature', () => {
Expand Down
10 changes: 10 additions & 0 deletions src/lib/CasperHDKeys/Ed25519HDKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import { HDKey } from './hdkey';
import { CasperHDKey } from './CasperHDKey';
import { Ed25519, SignatureAlgorithm } from '../Keys';

/**
* Casper HD Key for Ed25519.
*
* Examples
* ```ts
* const mn = Ed25519HDKey.newMnemonic();
* const hdKey = Ed25519HDKey.fromMnemonic(mn);
* const key0 = hdKey.deriveChild(0);
* ```
*/
export class Ed25519HDKey extends CasperHDKey<Ed25519> {
private hdKey: HDKey;

Expand Down
16 changes: 7 additions & 9 deletions src/lib/CasperHDKeys/Secp256K1HDKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ describe('Secp256K1HDKey', () => {
]
},
{
seed:
'fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542',
seed: 'fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542',
vectors: [
{
chain: 'm',
Expand Down Expand Up @@ -103,17 +102,16 @@ describe('Secp256K1HDKey', () => {
}
];
it('should generate key from mnemonic', () => {
const key = Secp256K1HDKey.new();

const key0 = key.deriveChild(1);

const mn = key.mnemonic;
const mn =
'equip will roof matter pink blind book anxiety banner elbow sun young';

const recoveredKey = Secp256K1HDKey.fromMnemonic(mn);

const recoveredKey0 = recoveredKey.deriveChild(1);
const key0 = recoveredKey.deriveChild(0);

expect(key0.accountHex()).eq(recoveredKey0.accountHex());
expect(key0.accountHex(false)).eq(
'02028b2ddbe59976ad2f4138ca46553866de5124d13db4e13611ca751eedde9e0297'
);
});

it('should generate r+s signature', () => {
Expand Down
10 changes: 10 additions & 0 deletions src/lib/CasperHDKeys/Secp256K1HDKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import { sha256 } from '@noble/hashes/sha256';
import { CasperHDKey } from './CasperHDKey';
import { Secp256K1, SignatureAlgorithm } from '../Keys';

/**
* Casper HD Key for Secp256K1.
*
* Examples
* ```ts
* const mn = Secp256K1HDKey.newMnemonic();
* const hdKey = Secp256K1HDKey.fromMnemonic(mn);
* const key0 = hdKey.deriveChild(0);
* ```
*/
export class Secp256K1HDKey extends CasperHDKey<Secp256K1> {
private hdKey: HDKey;

Expand Down
5 changes: 3 additions & 2 deletions src/lib/Keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,11 @@ export abstract class AsymmetricKey {

/**
* Gets the hexadecimal public key of the account
* @param {boolean} checksummed Indicates whether the public key should be checksummed, default: `true`
* @returns The public key of the `AsymmetricKey` as a hexadecimal string
*/
public accountHex(): string {
return this.publicKey.toHex();
public accountHex(checksummed = true): string {
return this.publicKey.toHex(checksummed);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const path = require('path');
const copyPlugin = require('copy-webpack-plugin');
const webpack = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
.BundleAnalyzerPlugin;
const BundleAnalyzerPlugin =
require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const nodeExternals = require('webpack-node-externals');

/** @type { import('webpack').Configuration } */
Expand Down

0 comments on commit 73bc0a7

Please sign in to comment.