-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
1,276 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module.exports = { | ||
extends: [ | ||
'@metamask/eslint-config', | ||
'@metamask/eslint-config/config/nodejs', | ||
], | ||
plugins: [ | ||
'json', | ||
], | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
}, | ||
ignorePatterns: [ | ||
'!.eslintrc.js', | ||
'node_modules/', | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,83 @@ | ||
// node | ||
const crypto = require('crypto') | ||
const assert = require('assert') | ||
const crypto = require('crypto'); | ||
const assert = require('assert'); | ||
// npm | ||
const secp256k1 = require('secp256k1') | ||
const createKeccakHash = require('keccak') | ||
const secp256k1 = require('secp256k1'); | ||
const createKeccakHash = require('keccak'); | ||
|
||
const HARDENED_OFFSET = 0x80000000 | ||
const HARDENED_OFFSET = 0x80000000; | ||
|
||
module.exports = { | ||
// standard | ||
deriveChildKey, | ||
// utility | ||
bip32PathToMultipath, | ||
privateKeyToEthAddress, | ||
} | ||
}; | ||
|
||
function bip32PathToMultipath(bip32Path) { | ||
let pathParts = bip32Path.trim().split('/') | ||
let pathParts = bip32Path.trim().split('/'); | ||
// strip "m" noop | ||
if (pathParts[0].toLowerCase() === 'm') pathParts = pathParts.slice(1) | ||
const multipath = pathParts.map(part => 'bip32:' + part).join('/') | ||
return multipath | ||
if (pathParts[0].toLowerCase() === 'm') { | ||
pathParts = pathParts.slice(1); | ||
} | ||
const multipath = pathParts.map((part) => `bip32:${part}`).join('/'); | ||
return multipath; | ||
} | ||
|
||
function deriveChildKey (parentKey, pathPart) { | ||
const isHardened = pathPart.includes(`'`) | ||
const indexPart = pathPart.split(`'`)[0] | ||
const childIndex = parseInt(indexPart, 10) | ||
assert(childIndex < HARDENED_OFFSET, 'Invalid index') | ||
assert(parentKey.length === 64, 'Parent key invalid length') | ||
function deriveChildKey(parentKey, pathPart) { | ||
const isHardened = pathPart.includes(`'`); | ||
const indexPart = pathPart.split(`'`)[0]; | ||
const childIndex = parseInt(indexPart, 10); | ||
assert(childIndex < HARDENED_OFFSET, 'Invalid index'); | ||
assert(parentKey.length === 64, 'Parent key invalid length'); | ||
|
||
const parentPrivateKey = parentKey.slice(0, 32) | ||
const parentExtraEntropy = parentKey.slice(32) | ||
const secretExtension = deriveSecretExtension({ parentPrivateKey, childIndex, isHardened }) | ||
const parentPrivateKey = parentKey.slice(0, 32); | ||
const parentExtraEntropy = parentKey.slice(32); | ||
const secretExtension = deriveSecretExtension({ parentPrivateKey, childIndex, isHardened }); | ||
|
||
const { privateKey, extraEntropy } = generateKey({ | ||
parentPrivateKey, | ||
parentExtraEntropy, | ||
secretExtension, | ||
}) | ||
}); | ||
|
||
return Buffer.concat([privateKey, extraEntropy]) | ||
return Buffer.concat([privateKey, extraEntropy]); | ||
} | ||
|
||
// the bip32 secret extension is created from the parent private or public key and the child index | ||
function deriveSecretExtension ({ parentPrivateKey, childIndex, isHardened }) { | ||
function deriveSecretExtension({ parentPrivateKey, childIndex, isHardened }) { | ||
if (isHardened) { | ||
// Hardened child | ||
const indexBuffer = Buffer.allocUnsafe(4) | ||
indexBuffer.writeUInt32BE(childIndex + HARDENED_OFFSET, 0) | ||
const pk = parentPrivateKey | ||
const zb = Buffer.alloc(1, 0) | ||
return Buffer.concat([zb, pk, indexBuffer]) | ||
} else { | ||
// Normal child | ||
const indexBuffer = Buffer.allocUnsafe(4) | ||
indexBuffer.writeUInt32BE(childIndex, 0) | ||
const parentPublicKey = secp256k1.publicKeyCreate(parentPrivateKey, true) | ||
return Buffer.concat([parentPublicKey, indexBuffer]) | ||
const indexBuffer = Buffer.allocUnsafe(4); | ||
indexBuffer.writeUInt32BE(childIndex + HARDENED_OFFSET, 0); | ||
const pk = parentPrivateKey; | ||
const zb = Buffer.alloc(1, 0); | ||
return Buffer.concat([zb, pk, indexBuffer]); | ||
} | ||
// Normal child | ||
const indexBuffer = Buffer.allocUnsafe(4); | ||
indexBuffer.writeUInt32BE(childIndex, 0); | ||
const parentPublicKey = secp256k1.publicKeyCreate(parentPrivateKey, true); | ||
return Buffer.concat([parentPublicKey, indexBuffer]); | ||
|
||
} | ||
|
||
function generateKey ({ parentPrivateKey, parentExtraEntropy, secretExtension }) { | ||
const entropy = crypto.createHmac('sha512', parentExtraEntropy).update(secretExtension).digest() | ||
const keyMaterial = entropy.slice(0, 32) | ||
function generateKey({ parentPrivateKey, parentExtraEntropy, secretExtension }) { | ||
const entropy = crypto.createHmac('sha512', parentExtraEntropy).update(secretExtension).digest(); | ||
const keyMaterial = entropy.slice(0, 32); | ||
// extraEntropy is also called "chaincode" | ||
const extraEntropy = entropy.slice(32) | ||
const privateKey = secp256k1.privateKeyTweakAdd(parentPrivateKey, keyMaterial) | ||
return { privateKey, extraEntropy } | ||
const extraEntropy = entropy.slice(32); | ||
const privateKey = secp256k1.privateKeyTweakAdd(parentPrivateKey, keyMaterial); | ||
return { privateKey, extraEntropy }; | ||
} | ||
|
||
function keccak (a, bits = 256) { | ||
return createKeccakHash('keccak' + bits).update(a).digest() | ||
function keccak(a, bits = 256) { | ||
return createKeccakHash(`keccak${bits}`).update(a).digest(); | ||
} | ||
|
||
function privateKeyToEthAddress(keyBuffer) { | ||
const privateKey = keyBuffer.slice(0, 32) | ||
const publicKey = secp256k1.publicKeyCreate(privateKey, false).slice(1) | ||
return keccak(publicKey).slice(-20) | ||
const privateKey = keyBuffer.slice(0, 32); | ||
const publicKey = secp256k1.publicKeyCreate(privateKey, false).slice(1); | ||
return keccak(publicKey).slice(-20); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
// node | ||
const crypto = require('crypto') | ||
const crypto = require('crypto'); | ||
// npm | ||
const bip39 = require('bip39') | ||
const bip39 = require('bip39'); | ||
|
||
const ROOT_BASE_SECRET = Buffer.from('Bitcoin seed', 'utf8') | ||
const ROOT_BASE_SECRET = Buffer.from('Bitcoin seed', 'utf8'); | ||
|
||
module.exports = { | ||
// standard | ||
deriveChildKey, | ||
// utility | ||
bip39MnemonicToMultipath, | ||
} | ||
}; | ||
|
||
function bip39MnemonicToMultipath(mnemonic) { | ||
return `bip39:${mnemonic.trim()}` | ||
return `bip39:${mnemonic.trim()}`; | ||
} | ||
|
||
// this creates a child key using bip39, ignoring the parent key | ||
function deriveChildKey (_parentKey, pathPart) { | ||
const mnemonic = pathPart | ||
const seedBuffer = bip39.mnemonicToSeed(mnemonic) | ||
const entropy = crypto.createHmac('sha512', ROOT_BASE_SECRET).update(seedBuffer).digest() | ||
function deriveChildKey(_parentKey, pathPart) { | ||
const mnemonic = pathPart; | ||
const seedBuffer = bip39.mnemonicToSeed(mnemonic); | ||
const entropy = crypto.createHmac('sha512', ROOT_BASE_SECRET).update(seedBuffer).digest(); | ||
|
||
return entropy | ||
return entropy; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
const bip32 = require('./bip32') | ||
const bip39 = require('./bip39') | ||
const bip32 = require('./bip32'); | ||
const bip39 = require('./bip39'); | ||
|
||
module.exports = { | ||
bip32, | ||
bip39, | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.