ethers.js/src.ts/utils/hdnode.ts

344 lines
11 KiB
TypeScript
Raw Normal View History

"use strict";
2018-06-13 22:39:39 +03:00
// See: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
// See: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
import * as errors from '../errors';
// The English language word list.
// For additional word lists, please see /src.tc/wordlists/
import { langEn } from '../wordlists/lang-en';
2018-07-16 07:09:13 +03:00
// Automatically register English?
//import { register } from '../wordlists/wordlist';
//register(langEn);
2018-06-13 22:39:39 +03:00
import { Base58 } from "./basex";
import { arrayify, concat, hexDataSlice, hexZeroPad, hexlify } from './bytes';
import { BigNumber, bigNumberify } from './bignumber';
import { toUtf8Bytes, UnicodeNormalizationForm } from './utf8';
import { pbkdf2 } from './pbkdf2';
import { computeHmac, SupportedAlgorithms } from './hmac';
import { defineReadOnly, isType, setType } from './properties';
2018-10-07 08:09:56 +03:00
import { computeAddress, KeyPair } from './secp256k1';
import { ripemd160, sha256 } from './sha2';
2018-06-13 22:39:39 +03:00
const N = bigNumberify("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141");
// Imported Types
import { Arrayish } from './bytes';
import { Wordlist } from './wordlist';
2018-07-16 07:09:13 +03:00
2018-06-13 22:39:39 +03:00
// "Bitcoin seed"
const MasterSecret = toUtf8Bytes('Bitcoin seed');
2018-06-13 22:39:39 +03:00
const HardenedBit = 0x80000000;
2018-06-13 22:39:39 +03:00
// Returns a byte with the MSB bits set
function getUpperMask(bits: number): number {
return ((1 << bits) - 1) << (8 - bits);
}
// Returns a byte with the LSB bits set
function getLowerMask(bits: number): number {
return (1 << bits) - 1;
}
function bytes32(value: Arrayish | BigNumber | number): string {
return hexZeroPad(hexlify(value), 32);
}
function base58check(data: Uint8Array): string {
let checksum = hexDataSlice(sha256(sha256(data)), 0, 4);
return Base58.encode(concat([ data, checksum ]));
}
const _constructorGuard: any = {};
2018-06-18 12:42:41 +03:00
export const defaultPath = "m/44'/60'/0'/0/0";
export class HDNode {
2018-06-13 22:39:39 +03:00
readonly privateKey: string;
readonly publicKey: string;
readonly fingerprint: string;
readonly parentFingerprint: string;
2018-10-07 08:09:56 +03:00
readonly address: string;
2018-06-13 22:39:39 +03:00
readonly mnemonic: string;
readonly path: string;
readonly chainCode: string;
readonly index: number;
readonly depth: number;
2018-06-19 09:12:57 +03:00
/**
* This constructor should not be called directly.
*
* Please use:
* - fromMnemonic
* - fromSeed
*/
constructor(constructorGuard: any, privateKey: string, publicKey: string, parentFingerprint: string, chainCode: string, index: number, depth: number, mnemonic: string, path: string) {
errors.checkNew(this, HDNode);
2018-06-13 22:39:39 +03:00
if (constructorGuard !== _constructorGuard) {
throw new Error('HDNode constructor cannot be called directly');
}
if (privateKey) {
let keyPair = new KeyPair(privateKey);
defineReadOnly(this, 'privateKey', keyPair.privateKey);
defineReadOnly(this, 'publicKey', keyPair.compressedPublicKey);
} else {
defineReadOnly(this, 'privateKey', null);
defineReadOnly(this, 'publicKey', hexlify(publicKey));
}
2018-06-13 22:39:39 +03:00
defineReadOnly(this, 'parentFingerprint', parentFingerprint);
defineReadOnly(this, 'fingerprint', hexDataSlice(ripemd160(sha256(this.publicKey)), 0, 4));
2018-06-13 22:39:39 +03:00
2018-10-07 08:09:56 +03:00
defineReadOnly(this, 'address', computeAddress(this.publicKey));
defineReadOnly(this, 'chainCode', chainCode);
2018-06-13 22:39:39 +03:00
2018-06-19 09:12:57 +03:00
defineReadOnly(this, 'index', index);
defineReadOnly(this, 'depth', depth);
2018-06-13 22:39:39 +03:00
2018-06-19 09:12:57 +03:00
defineReadOnly(this, 'mnemonic', mnemonic);
defineReadOnly(this, 'path', path);
setType(this, 'HDNode');
2018-06-13 22:39:39 +03:00
}
get extendedKey(): string {
// We only support the mainnet values for now, but if anyone needs
// testnet values, let me know. I believe current senitment is that
// we should always use mainnet, and use BIP-44 to derive the network
// - Mainnet: public=0x0488B21E, private=0x0488ADE4
// - Testnet: public=0x043587CF, private=0x04358394
if (this.depth >= 256) { throw new Error("Depth too large!"); }
return base58check(concat([
((this.privateKey != null) ? "0x0488ADE4": "0x0488B21E"),
hexlify(this.depth),
this.parentFingerprint,
hexZeroPad(hexlify(this.index), 4),
this.chainCode,
((this.privateKey != null) ? concat([ "0x00", this.privateKey ]): this.publicKey),
]));
}
2018-06-13 22:39:39 +03:00
neuter(): HDNode {
return new HDNode(_constructorGuard, null, this.publicKey, this.parentFingerprint, this.chainCode, this.index, this.depth, null, this.path);
}
2018-06-13 22:39:39 +03:00
private _derive(index: number): HDNode {
if (index > 0xffffffff) { throw new Error("invalid index - " + String(index)); }
2018-06-13 22:39:39 +03:00
// Base path
let path = this.path;
if (path) { path += '/' + (index & ~HardenedBit); }
2018-06-13 22:39:39 +03:00
let data = new Uint8Array(37);
2018-06-13 22:39:39 +03:00
if (index & HardenedBit) {
if (!this.privateKey) {
throw new Error('cannot derive child of neutered node');
}
2018-06-13 22:39:39 +03:00
// Data = 0x00 || ser_256(k_par)
data.set(arrayify(this.privateKey), 1);
// Hardened path
if (path) { path += "'"; }
} else {
// Data = ser_p(point(k_par))
data.set(arrayify(this.publicKey));
2018-06-13 22:39:39 +03:00
}
// Data += ser_32(i)
for (let i = 24; i >= 0; i -= 8) { data[33 + (i >> 3)] = ((index >> (24 - i)) & 0xff); }
2018-06-13 22:39:39 +03:00
let I = computeHmac(SupportedAlgorithms.sha512, this.chainCode, data);
let IL = I.slice(0, 32);
let IR = I.slice(32);
2018-06-13 22:39:39 +03:00
// The private key
let ki: string = null
// The public key
let Ki: string = null;
if (this.privateKey) {
ki = bytes32(bigNumberify(IL).add(this.privateKey).mod(N));
} else {
let ek = new KeyPair(hexlify(IL));
Ki = ek._addPoint(this.publicKey);
}
2018-06-13 22:39:39 +03:00
return new HDNode(_constructorGuard, ki, Ki, this.fingerprint, bytes32(IR), index, this.depth + 1, this.mnemonic, path);
2018-06-13 22:39:39 +03:00
}
derivePath(path: string): HDNode {
let components = path.split('/');
2018-06-13 22:39:39 +03:00
if (components.length === 0 || (components[0] === 'm' && this.depth !== 0)) {
throw new Error('invalid path - ' + path);
2018-06-13 22:39:39 +03:00
}
if (components[0] === 'm') { components.shift(); }
let result: HDNode = this;
for (let i = 0; i < components.length; i++) {
let component = components[i];
2018-06-13 22:39:39 +03:00
if (component.match(/^[0-9]+'$/)) {
let index = parseInt(component.substring(0, component.length - 1));
2018-06-13 22:39:39 +03:00
if (index >= HardenedBit) { throw new Error('invalid path index - ' + component); }
result = result._derive(HardenedBit + index);
} else if (component.match(/^[0-9]+$/)) {
let index = parseInt(component);
2018-06-13 22:39:39 +03:00
if (index >= HardenedBit) { throw new Error('invalid path index - ' + component); }
result = result._derive(index);
} else {
throw new Error('invlaid path component - ' + component);
}
}
return result;
}
static isHDNode(value: any): value is HDNode {
return isType(value, 'HDNode');
}
static fromExtendedKey(extendedKey: string): HDNode {
return null;
}
}
2018-06-15 11:18:17 +03:00
function _fromSeed(seed: Arrayish, mnemonic: string): HDNode {
let seedArray: Uint8Array = arrayify(seed);
if (seedArray.length < 16 || seedArray.length > 64) { throw new Error('invalid seed'); }
2018-06-13 22:39:39 +03:00
let I: Uint8Array = arrayify(computeHmac(SupportedAlgorithms.sha512, MasterSecret, seedArray));
2018-06-13 22:39:39 +03:00
return new HDNode(_constructorGuard, bytes32(I.slice(0, 32)), null, "0x00000000", bytes32(I.slice(32)), 0, 0, mnemonic, 'm');
2018-06-13 22:39:39 +03:00
}
export function fromMnemonic(mnemonic: string, wordlist?: Wordlist): HDNode {
2018-06-13 22:39:39 +03:00
// Check that the checksum s valid (will throw an error)
mnemonicToEntropy(mnemonic, wordlist);
2018-06-13 22:39:39 +03:00
return _fromSeed(mnemonicToSeed(mnemonic), mnemonic);
}
export function fromSeed(seed: Arrayish): HDNode {
2018-06-13 22:39:39 +03:00
return _fromSeed(seed, null);
}
export function mnemonicToSeed(mnemonic: string, password?: string): string {
if (!password) { password = ''; }
2018-06-13 22:39:39 +03:00
let salt = toUtf8Bytes('mnemonic' + password, UnicodeNormalizationForm.NFKD);
2018-06-13 22:39:39 +03:00
2018-06-19 09:12:57 +03:00
return hexlify(pbkdf2(toUtf8Bytes(mnemonic, UnicodeNormalizationForm.NFKD), salt, 2048, 64, 'sha512'));
2018-06-13 22:39:39 +03:00
}
export function mnemonicToEntropy(mnemonic: string, wordlist?: Wordlist): string {
if (!wordlist) { wordlist = langEn; }
errors.checkNormalize();
let words = wordlist.split(mnemonic);
2018-06-13 22:39:39 +03:00
if ((words.length % 3) !== 0) { throw new Error('invalid mnemonic'); }
let entropy = arrayify(new Uint8Array(Math.ceil(11 * words.length / 8)));
2018-06-13 22:39:39 +03:00
let offset = 0;
for (let i = 0; i < words.length; i++) {
let index = wordlist.getWordIndex(words[i].normalize('NFKD'));
2018-06-13 22:39:39 +03:00
if (index === -1) { throw new Error('invalid mnemonic'); }
for (let bit = 0; bit < 11; bit++) {
2018-06-13 22:39:39 +03:00
if (index & (1 << (10 - bit))) {
entropy[offset >> 3] |= (1 << (7 - (offset % 8)));
}
offset++;
}
}
let entropyBits = 32 * words.length / 3;
2018-06-13 22:39:39 +03:00
let checksumBits = words.length / 3;
let checksumMask = getUpperMask(checksumBits);
2018-06-13 22:39:39 +03:00
let checksum = arrayify(sha256(entropy.slice(0, entropyBits / 8)))[0];
2018-06-13 22:39:39 +03:00
checksum &= checksumMask;
if (checksum !== (entropy[entropy.length - 1] & checksumMask)) {
throw new Error('invalid checksum');
}
return hexlify(entropy.slice(0, entropyBits / 8));
}
export function entropyToMnemonic(entropy: Arrayish, wordlist?: Wordlist): string {
2018-06-13 22:39:39 +03:00
entropy = arrayify(entropy);
if ((entropy.length % 4) !== 0 || entropy.length < 16 || entropy.length > 32) {
throw new Error('invalid entropy');
}
let indices: Array<number> = [ 0 ];
2018-06-13 22:39:39 +03:00
let remainingBits = 11;
for (let i = 0; i < entropy.length; i++) {
2018-06-13 22:39:39 +03:00
// Consume the whole byte (with still more to go)
if (remainingBits > 8) {
2018-06-15 11:18:17 +03:00
indices[indices.length - 1] <<= 8;
indices[indices.length - 1] |= entropy[i];
2018-06-13 22:39:39 +03:00
remainingBits -= 8;
// This byte will complete an 11-bit index
} else {
2018-06-15 11:18:17 +03:00
indices[indices.length - 1] <<= remainingBits;
indices[indices.length - 1] |= entropy[i] >> (8 - remainingBits);
2018-06-13 22:39:39 +03:00
// Start the next word
2018-06-15 11:18:17 +03:00
indices.push(entropy[i] & getLowerMask(8 - remainingBits));
2018-06-13 22:39:39 +03:00
remainingBits += 3;
}
}
// Compute the checksum bits
let checksum = arrayify(sha256(entropy))[0];
let checksumBits = entropy.length / 4;
2018-06-13 22:39:39 +03:00
checksum &= getUpperMask(checksumBits);
// Shift the checksum into the word indices
2018-06-15 11:18:17 +03:00
indices[indices.length - 1] <<= checksumBits;
indices[indices.length - 1] |= (checksum >> (8 - checksumBits));
2018-06-13 22:39:39 +03:00
if (!wordlist) { wordlist = langEn; }
return wordlist.join(indices.map((index) => wordlist.getWord(index)));
2018-06-13 22:39:39 +03:00
}
export function isValidMnemonic(mnemonic: string, wordlist?: Wordlist): boolean {
2018-06-13 22:39:39 +03:00
try {
mnemonicToEntropy(mnemonic, wordlist);
2018-06-13 22:39:39 +03:00
return true;
} catch (error) { }
return false;
}
2018-06-18 12:42:41 +03:00