2014-02-28 16:05:43 +08:00
var assert = require ( 'assert' )
2014-04-08 22:13:03 +10:00
var base58 = require ( './base58' )
2014-04-17 06:18:31 +10:00
2014-05-03 10:04:54 +08:00
var BigInteger = require ( 'bigi' )
2014-04-08 22:00:28 +10:00
var crypto = require ( './crypto' )
2014-05-13 16:44:29 +10:00
var ECKey = require ( './eckey' )
var ECPubKey = require ( './ecpubkey' )
2014-05-31 20:16:42 +10:00
var ECPointFp = require ( './ec' ) . ECPointFp
2014-05-05 13:23:22 +10:00
var networks = require ( './networks' )
2014-01-16 14:03:09 +07:00
2014-04-22 02:19:30 +10:00
var sec = require ( './sec' )
2014-04-17 06:45:57 +10:00
var ecparams = sec ( "secp256k1" )
2014-05-31 20:22:32 +10:00
function findBIP32ParamsByVersion ( version ) {
for ( var name in networks ) {
var network = networks [ name ]
for ( var type in network . bip32 ) {
if ( version != network . bip32 [ type ] ) continue
return {
2014-06-03 15:58:42 +10:00
isPrivate : ( type === 'private' ) ,
2014-05-31 20:22:32 +10:00
network : network
}
}
}
assert ( false , 'Could not find version ' + version . toString ( 16 ) )
}
2014-05-31 20:16:42 +10:00
function HDWallet ( K , chainCode , network ) {
2014-05-30 18:41:03 +10:00
network = network || networks . bitcoin
2014-02-28 17:26:16 +08:00
2014-05-31 20:16:42 +10:00
assert ( Buffer . isBuffer ( chainCode ) , 'Expected Buffer, got ' + chainCode )
assert ( network . bip32 , 'Unknown BIP32 constants for network' )
2014-05-12 09:56:00 +10:00
2014-05-31 20:16:42 +10:00
this . chainCode = chainCode
2014-03-31 11:47:47 +08:00
this . depth = 0
2014-05-12 09:56:00 +10:00
this . index = 0
2014-05-31 20:16:42 +10:00
this . network = network
if ( K instanceof BigInteger ) {
this . priv = new ECKey ( K , true )
this . pub = this . priv . pub
} else {
this . pub = new ECPubKey ( K , true )
}
2014-01-16 14:03:09 +07:00
}
2014-05-12 09:56:00 +10:00
HDWallet . MASTER _SECRET = new Buffer ( 'Bitcoin seed' )
2014-01-16 14:03:09 +07:00
HDWallet . HIGHEST _BIT = 0x80000000
HDWallet . LENGTH = 78
2014-05-31 20:16:42 +10:00
HDWallet . fromSeedBuffer = function ( seed , network ) {
var I = crypto . HmacSHA512 ( seed , HDWallet . MASTER _SECRET )
var IL = I . slice ( 0 , 32 )
var IR = I . slice ( 32 )
// In case IL is 0 or >= n, the master key is invalid
// This is handled by `new ECKey` in the HDWallet constructor
var pIL = BigInteger . fromBuffer ( IL )
return new HDWallet ( pIL , IR , network )
}
2014-05-30 18:41:03 +10:00
HDWallet . fromSeedHex = function ( hex , network ) {
2014-05-31 20:16:42 +10:00
return HDWallet . fromSeedBuffer ( new Buffer ( hex , 'hex' ) , network )
2014-01-16 14:03:09 +07:00
}
2014-04-04 05:10:12 +11:00
HDWallet . fromBase58 = function ( string ) {
var buffer = base58 . decode ( string )
2014-01-16 14:03:09 +07:00
2014-04-04 05:10:12 +11:00
var payload = buffer . slice ( 0 , - 4 )
var checksum = buffer . slice ( - 4 )
2014-01-16 14:03:09 +07:00
2014-05-31 20:17:41 +10:00
var newChecksum = crypto . hash256 ( payload ) . slice ( 0 , 4 )
2014-05-30 19:00:49 +10:00
assert . deepEqual ( newChecksum , checksum , 'Invalid checksum' )
2014-01-16 14:03:09 +07:00
2014-04-17 06:18:31 +10:00
return HDWallet . fromBuffer ( payload )
2014-01-16 14:03:09 +07:00
}
2014-05-31 20:16:42 +10:00
HDWallet . fromBuffer = function ( buffer ) {
assert . strictEqual ( buffer . length , HDWallet . LENGTH , 'Invalid buffer length' )
2014-03-31 11:47:47 +08:00
2014-04-17 06:18:31 +10:00
// 4 byte: version bytes
2014-05-31 20:16:42 +10:00
var version = buffer . readUInt32BE ( 0 )
2014-05-31 20:22:32 +10:00
var params = findBIP32ParamsByVersion ( version )
2014-01-16 14:03:09 +07:00
2014-03-31 11:47:47 +08:00
// 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, ...
2014-05-31 20:16:42 +10:00
var depth = buffer . readUInt8 ( 4 )
2014-01-16 14:03:09 +07:00
2014-03-31 11:47:47 +08:00
// 4 bytes: the fingerprint of the parent's key (0x00000000 if master key)
2014-05-31 20:16:42 +10:00
var parentFingerprint = buffer . readUInt32BE ( 5 )
if ( depth === 0 ) {
assert . strictEqual ( parentFingerprint , 0x00000000 , 'Invalid parent fingerprint' )
2014-04-17 06:18:31 +10:00
}
2014-01-16 14:03:09 +07:00
2014-03-31 11:47:47 +08:00
// 4 bytes: child number. This is the number i in xi = xpar/i, with xi the key being serialized.
// This is encoded in MSB order. (0x00000000 if master key)
2014-05-31 20:16:42 +10:00
var index = buffer . readUInt32BE ( 9 )
assert ( depth > 0 || index === 0 , 'Invalid index' )
2014-01-16 14:03:09 +07:00
2014-03-31 11:47:47 +08:00
// 32 bytes: the chain code
2014-05-31 20:16:42 +10:00
var chainCode = buffer . slice ( 13 , 45 )
2014-01-16 14:03:09 +07:00
2014-03-31 11:47:47 +08:00
// 33 bytes: the public key or private key data (0x02 + X or 0x03 + X for
// public keys, 0x00 + k for private keys)
2014-05-31 20:16:42 +10:00
var data = buffer . slice ( 45 , 78 )
var hd
2014-05-31 20:22:32 +10:00
if ( params . isPrivate ) {
2014-05-31 20:16:42 +10:00
assert . strictEqual ( data . readUInt8 ( 0 ) , 0x00 , 'Invalid private key' )
data = data . slice ( 1 )
2014-05-16 13:46:06 +10:00
2014-05-31 20:16:42 +10:00
var D = BigInteger . fromBuffer ( data )
2014-05-31 20:22:32 +10:00
hd = new HDWallet ( D , chainCode , params . network )
2014-03-31 11:47:47 +08:00
} else {
2014-05-31 20:16:42 +10:00
var decode = ECPointFp . decodeFrom ( ecparams . getCurve ( ) , data )
assert . equal ( decode . compressed , true , 'Invalid public key' )
2014-05-31 20:28:16 +10:00
// When importing a serialized extended public key, implementations must verify whether the X coordinate in the public key data corresponds to a point on the curve. If not, the extended public key is invalid.
decode . Q . validate ( )
2014-05-31 20:22:32 +10:00
hd = new HDWallet ( decode . Q , chainCode , params . network )
2014-03-31 11:47:47 +08:00
}
2014-01-16 14:03:09 +07:00
2014-05-31 20:16:42 +10:00
hd . depth = depth
hd . index = index
hd . parentFingerprint = parentFingerprint
2014-03-31 11:47:47 +08:00
return hd
2014-01-16 14:03:09 +07:00
}
2014-06-03 15:58:42 +10:00
HDWallet . fromHex = function ( hex , isPrivate ) {
2014-06-01 16:39:07 +10:00
return HDWallet . fromBuffer ( new Buffer ( hex , 'hex' ) )
}
2014-01-16 14:03:09 +07:00
HDWallet . prototype . getIdentifier = function ( ) {
2014-04-17 19:08:16 +10:00
return crypto . hash160 ( this . pub . toBuffer ( ) )
2014-01-16 14:03:09 +07:00
}
HDWallet . prototype . getFingerprint = function ( ) {
2014-04-17 06:18:31 +10:00
return this . getIdentifier ( ) . slice ( 0 , 4 )
2014-01-16 14:03:09 +07:00
}
2014-03-22 18:17:54 +11:00
HDWallet . prototype . getAddress = function ( ) {
2014-05-31 18:53:39 +10:00
return this . pub . getAddress ( this . network . pubKeyHash )
2014-01-16 14:03:09 +07:00
}
2014-06-03 16:19:44 +10:00
HDWallet . prototype . toBase58 = function ( isPrivate ) {
var buffer = this . toBuffer ( isPrivate )
var checksum = crypto . hash256 ( buffer ) . slice ( 0 , 4 )
return base58 . encode ( Buffer . concat ( [
buffer ,
checksum
] ) )
}
2014-06-03 15:58:42 +10:00
HDWallet . prototype . toBuffer = function ( isPrivate ) {
2014-06-03 16:20:37 +10:00
if ( isPrivate == undefined ) isPrivate = ! ! this . priv
2014-03-31 11:47:47 +08:00
// Version
2014-06-03 15:58:42 +10:00
var version = isPrivate ? this . network . bip32 . private : this . network . bip32 . public
2014-04-17 06:18:31 +10:00
var buffer = new Buffer ( HDWallet . LENGTH )
2014-03-31 11:47:47 +08:00
2014-04-17 06:18:31 +10:00
// 4 bytes: version bytes
buffer . writeUInt32BE ( version , 0 )
2014-01-16 14:03:09 +07:00
2014-03-31 11:47:47 +08:00
// Depth
// 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, ....
2014-04-17 06:18:31 +10:00
buffer . writeUInt8 ( this . depth , 4 )
2014-03-31 11:47:47 +08:00
// 4 bytes: the fingerprint of the parent's key (0x00000000 if master key)
2014-06-01 16:04:44 +10:00
var fingerprint = ( this . depth === 0 ) ? 0x00000000 : this . parentFingerprint
2014-04-17 06:18:31 +10:00
buffer . writeUInt32BE ( fingerprint , 5 )
2014-03-31 11:47:47 +08:00
// 4 bytes: child number. This is the number i in xi = xpar/i, with xi the key being serialized.
2014-04-17 06:18:31 +10:00
// This is encoded in Big endian. (0x00000000 if master key)
buffer . writeUInt32BE ( this . index , 9 )
2014-03-31 11:47:47 +08:00
// 32 bytes: the chain code
2014-05-31 20:16:42 +10:00
this . chainCode . copy ( buffer , 13 )
2014-03-31 11:47:47 +08:00
// 33 bytes: the public key or private key data
2014-06-03 15:58:42 +10:00
if ( isPrivate ) {
2014-04-18 06:19:14 +10:00
assert ( this . priv , 'Missing private key' )
2014-04-17 06:18:31 +10:00
// 0x00 + k for private keys
buffer . writeUInt8 ( 0 , 45 )
2014-05-16 13:46:06 +10:00
this . priv . D . toBuffer ( 32 ) . copy ( buffer , 46 )
2014-03-31 11:47:47 +08:00
} else {
2014-04-17 06:18:31 +10:00
// X9.62 encoding for public keys
2014-04-17 19:08:16 +10:00
this . pub . toBuffer ( ) . copy ( buffer , 45 )
2014-03-31 11:47:47 +08:00
}
return buffer
2014-01-16 14:03:09 +07:00
}
2014-06-01 16:39:07 +10:00
2014-06-03 15:58:42 +10:00
HDWallet . prototype . toHex = function ( isPrivate ) {
return this . toBuffer ( isPrivate ) . toString ( 'hex' )
2014-01-16 14:03:09 +07:00
}
2014-05-10 09:41:18 +10:00
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions
HDWallet . prototype . derive = function ( index ) {
var isHardened = index >= HDWallet . HIGHEST _BIT
var indexBuffer = new Buffer ( 4 )
indexBuffer . writeUInt32BE ( index , 0 )
2014-05-04 16:37:18 +10:00
2014-05-10 09:41:18 +10:00
var data
2014-01-16 14:03:09 +07:00
2014-05-10 09:41:18 +10:00
// Hardened child
if ( isHardened ) {
assert ( this . priv , 'Could not derive hardened child key' )
// data = 0x00 || ser256(kpar) || ser32(index)
data = Buffer . concat ( [
this . priv . D . toBuffer ( 33 ) ,
indexBuffer
] )
2014-03-31 11:47:47 +08:00
2014-05-10 09:41:18 +10:00
// Normal child
2014-03-31 11:47:47 +08:00
} else {
2014-05-10 09:41:18 +10:00
// data = serP(point(kpar)) || ser32(index)
// = serP(Kpar) || ser32(index)
data = Buffer . concat ( [
this . pub . toBuffer ( ) ,
indexBuffer
] )
2014-03-31 11:47:47 +08:00
}
2014-05-31 20:16:42 +10:00
var I = crypto . HmacSHA512 ( data , this . chainCode )
2014-05-10 09:41:18 +10:00
var IL = I . slice ( 0 , 32 )
var IR = I . slice ( 32 )
2014-01-16 14:03:09 +07:00
2014-05-10 09:41:18 +10:00
var pIL = BigInteger . fromBuffer ( IL )
2014-04-17 06:18:31 +10:00
2014-05-31 20:16:42 +10:00
// In case parse256(IL) >= n, proceed with the next value for i
if ( pIL . compareTo ( ecparams . getN ( ) ) >= 0 ) {
return this . derive ( index + 1 )
}
2014-05-10 09:41:18 +10:00
// Private parent key -> private child key
2014-03-31 11:47:47 +08:00
if ( this . priv ) {
2014-05-10 09:41:18 +10:00
// ki = parse256(IL) + kpar (mod n)
var ki = pIL . add ( this . priv . D ) . mod ( ecparams . getN ( ) )
2014-05-31 20:16:42 +10:00
// In case ki == 0, proceed with the next value for i
if ( ki . signum ( ) === 0 ) {
2014-05-10 09:41:18 +10:00
return this . derive ( index + 1 )
}
2014-04-17 06:45:57 +10:00
2014-05-31 20:16:42 +10:00
hd = new HDWallet ( ki , IR , this . network )
2014-05-10 09:41:18 +10:00
// Public parent key -> public child key
2014-03-31 11:47:47 +08:00
} else {
2014-05-10 09:41:18 +10:00
// Ki = point(parse256(IL)) + Kpar
// = G*IL + Kpar
var Ki = ecparams . getG ( ) . multiply ( pIL ) . add ( this . pub . Q )
2014-05-31 20:16:42 +10:00
// In case Ki is the point at infinity, proceed with the next value for i
if ( Ki . isInfinity ( ) ) {
2014-05-10 09:41:18 +10:00
return this . derive ( index + 1 )
}
2014-04-17 06:45:57 +10:00
2014-05-31 20:16:42 +10:00
hd = new HDWallet ( Ki , IR , this . network )
2014-03-31 11:47:47 +08:00
}
hd . depth = this . depth + 1
2014-05-10 09:41:18 +10:00
hd . index = index
2014-05-31 20:16:42 +10:00
hd . parentFingerprint = this . getFingerprint ( ) . readUInt32BE ( 0 )
2014-05-10 09:41:18 +10:00
2014-03-31 11:47:47 +08:00
return hd
2014-01-16 14:03:09 +07:00
}
2014-06-03 16:14:15 +10:00
HDWallet . prototype . deriveHardened = function ( index ) {
2014-05-10 09:41:18 +10:00
// Only derives hardened private keys by default
2014-03-31 11:47:47 +08:00
return this . derive ( index + HDWallet . HIGHEST _BIT )
2014-01-16 14:03:09 +07:00
}
HDWallet . prototype . toString = HDWallet . prototype . toBase58
2014-03-08 13:02:40 +08:00
2014-04-04 05:10:12 +11:00
module . exports = HDWallet