Skip to content

Commit

Permalink
refactor(hd-wallet)!: expect that bip39 used externally
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Feb 11, 2022
1 parent 951ebb2 commit f6243ad
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 116 deletions.
1 change: 0 additions & 1 deletion docs/guides/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ AeError
│ │ InvalidChecksumError
│ │ InvalidDerivationPathError
│ │ InvalidKeyError
│ │ InvalidMnemonicError
│ │ InvalidPasswordError
│ │ MerkleTreeHashMismatchError
│ │ MessageLimitError
Expand Down
78 changes: 2 additions & 76 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"dependencies": {
"@aeternity/aepp-calldata": "^1.1.0",
"@aeternity/argon2-browser": "^0.1.1",
"@aeternity/bip39": "^0.1.0",
"@aeternity/json-bigint": "^0.3.1",
"@aeternity/uuid": "^0.0.1",
"@babel/runtime-corejs3": "^7.17.2",
Expand Down
7 changes: 0 additions & 7 deletions src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,6 @@ export class InvalidKeyError extends CryptographyError {
}
}

export class InvalidMnemonicError extends CryptographyError {
constructor () {
super('Invalid mnemonic')
this.name = 'InvalidMnemonicError'
}
}

export class InvalidPasswordError extends CryptographyError {
constructor (msg: string) {
super(msg)
Expand Down
19 changes: 3 additions & 16 deletions src/utils/hd-wallet.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import nacl from 'tweetnacl'
import { full as hmac } from 'tweetnacl-auth'
import { fromString } from 'bip32-path'
import { validateMnemonic, mnemonicToSeed, generateMnemonic as genMnemonic } from '@aeternity/bip39'
import { decryptKey, encryptKey } from './crypto'
import { encode } from '../tx/builder/helpers'
import {
InvalidDerivationPathError,
NotHardenedSegmentError,
UnsupportedChildIndexError,
InvalidMnemonicError
InvalidDerivationPathError, NotHardenedSegmentError, UnsupportedChildIndexError
} from './errors'

const ED25519_CURVE = Buffer.from('ed25519 seed')
Expand Down Expand Up @@ -47,10 +43,6 @@ export function getKeyPair (secretKey) {
return nacl.sign.keyPair.fromSeed(secretKey)
}

export function generateMnemonic () {
return genMnemonic()
}

export function getMasterKeyFromSeed (seed) {
const I = hmac(seed, ED25519_CURVE)
const IL = I.slice(0, 32)
Expand Down Expand Up @@ -79,11 +71,7 @@ export function deriveChild ({ secretKey, chainCode }, index) {
}
}

export function generateSaveHDWallet (mnemonic, password) {
if (!validateMnemonic(mnemonic)) {
throw new InvalidMnemonicError()
}
const seed = mnemonicToSeed(mnemonic)
export function generateSaveHDWalletFromSeed (seed, password) {
const walletKey = derivePathFromSeed('m/44h/457h', seed)
return {
secretKey: toHex(encryptKey(password, walletKey.secretKey)),
Expand All @@ -101,8 +89,7 @@ export function getSaveHDWalletAccounts (saveHDWallet, password, accountCount) {
formatAccount(getKeyPair(derivePathFromKey(`${idx}h/0h/0h`, walletKey).secretKey)))
}

export const getHdWalletAccountFromMnemonic = (mnemonic, accountIdx) => {
const seed = mnemonicToSeed(mnemonic)
export const getHdWalletAccountFromSeed = (seed, accountIdx) => {
const walletKey = derivePathFromSeed('m/44h/457h', seed)
const derived = derivePathFromKey(`${accountIdx}h/0h/0h`, walletKey)
const keyPair = getKeyPair(derived.secretKey)
Expand Down
25 changes: 10 additions & 15 deletions test/unit/hd-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
import { expect } from 'chai'
import { describe, it } from 'mocha'
import {
deriveChild, derivePathFromKey, derivePathFromSeed, generateMnemonic,
generateSaveHDWallet, getHdWalletAccountFromMnemonic, getKeyPair,
deriveChild, derivePathFromKey, derivePathFromSeed,
generateSaveHDWalletFromSeed, getHdWalletAccountFromSeed, getKeyPair,
getMasterKeyFromSeed,
getSaveHDWalletAccounts
} from '../../src/utils/hd-wallet'
import { encode } from '../../src/tx/builder/helpers'
import { InvalidDerivationPathError, InvalidMnemonicError } from '../../src/utils/errors'
import { encode, decode } from '../../src/tx/builder/helpers'
import { InvalidDerivationPathError } from '../../src/utils/errors'

describe('hd wallet', () => {
const testMnemonic = 'eye quarter chapter suit cruel scrub verify stuff volume control learn dust'
const testMnemonicSeed = Buffer.from('Git7bFJkmfC1Ho+6YFSFuxSzmDZydmjzk8FubrPDz4PmrkORlBDlfnPTk02Wq9Pj2ZdQ5cTA0SxHKGrq3xSjOw==', 'base64')
const testPassword = 'test-password'
const testSaveHDWallet = {
chainCode: 'dd5cb572e8bddab36882ebbf87854e3b66f565447f20cfac874a5d3d7dd6d0d5',
Expand All @@ -51,9 +51,9 @@ describe('hd wallet', () => {
chainCode: Buffer.from('1fa9899b84631aaadf1daba7f7cf780e2fecfb8dd34a2edf6568eb1a9bb2f627', 'hex')
}

describe('generateSaveHDWallet', () =>
describe('generateSaveHDWalletFromSeed', () =>
it('generates encrypted extended wallet key', () => {
const walletKeys = generateSaveHDWallet(testMnemonic, testPassword)
const walletKeys = generateSaveHDWalletFromSeed(testMnemonicSeed, testPassword)
expect(walletKeys).to.eql(testSaveHDWallet)
}))

Expand Down Expand Up @@ -172,14 +172,9 @@ describe('hd wallet', () => {
})
)

it('Generate mnemonic', () => {
const mnemonic = generateMnemonic()
const wallet = getHdWalletAccountFromMnemonic(mnemonic, 0)
wallet.publicKey.split('_')[0].should.be.equal('ak')
})

it('Try to get wallet from invalid mnemonic', () => {
expect(() => generateSaveHDWallet('asdasdasdasdas')).to.throw(InvalidMnemonicError, 'Invalid mnemonic')
it('get HdWalletAccount from seed', () => {
const wallet = getHdWalletAccountFromSeed(testMnemonicSeed, 0)
decode(wallet.publicKey, 'ak')
})

it('Derive child with invalid path', () => {
Expand Down

0 comments on commit f6243ad

Please sign in to comment.