Remove obsolete bip32.js

This commit is contained in:
Andreas Brekken 2014-01-16 14:03:34 +07:00
parent 5f9e2624f8
commit 966d2ee90d
2 changed files with 0 additions and 339 deletions

View file

@ -1,171 +0,0 @@
var util = require('./util'),
Address = require('./address'),
conv = require('./convert'),
ECKey = require('./eckey').ECKey,
ECPubKey = require('./eckey').ECPubKey,
base58 = require('./base58'),
assert = require('assert'),
Crypto = require('./crypto-js/crypto');
var BIP32key = function(opts) {
if (!opts) opts = {}
if (typeof opts == 'string') {
try {
opts = BIP32key.deserialize(opts);
}
catch(e) {
opts = BIP32key.fromMasterKey(opts);
}
}
this.vbytes = opts.vbytes;
this.depth = opts.depth;
this.fingerprint = opts.fingerprint;
this.i = opts.i;
this.chaincode = opts.chaincode;
this.key = opts.key;
this.type = conv.bytesToString(this.vbytes) == PRIVDERIV ? 'priv' : 'pub'
return this;
}
var PRIVDERIV = BIP32key.PRIVDERIV = '\x04\x88\xAD\xE4'
var PUBDERIV = BIP32key.PUBDERIV = '\x04\x88\xB2\x1E'
var BIP32_PRIME = BIP32key.PRIME = 0x80000000
BIP32key.deserialize = function(str) {
var bytes = base58.decode(str)
var front = bytes.slice(0,bytes.length-4),
back = bytes.slice(bytes.length-4);
var checksum = Crypto.SHA256(Crypto.SHA256(front,{asBytes: true}), {asBytes: true})
.slice(0,4);
if ('' + checksum != '' + back) {
throw new Error('Checksum failed');
}
var type = conv.bytesToString(bytes.slice(0,4)) == PRIVDERIV ? 'priv' : 'pub';
return new BIP32key({
type: type,
vbytes: bytes.slice(0,4),
depth: bytes[4],
fingerprint: bytes.slice(5,9),
i: util.bytesToNum(bytes.slice(9,13).reverse()),
chaincode: bytes.slice(13,45),
key: type == 'priv' ? new ECKey(bytes.slice(46,78).concat([1]),true)
: new ECPubKey(bytes.slice(45,78),true)
})
}
BIP32key.prototype.serialize = function(enc) {
var bytes = []
// 4 byte: version bytes (mainnet: 0x0488B21E public, 0x0488ADE4 private; testnet: 0x043587CF public, 0x04358394 private)
bytes = bytes.concat(this.vbytes)
assert.equal(bytes.length, 4)
// 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, ....
bytes = bytes.concat([this.depth])
assert.equal(bytes.length, 4 + 1)
// 4 bytes: the fingerprint of the parent's key (0x00000000 if master key)
bytes = bytes.concat(this.fingerprint)
assert.equal(bytes.length, 4 + 1 + 4)
// 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)
bytes = bytes.concat(util.numToBytes(this.i, 4).reverse())
assert.equal(bytes.length, 4 + 1 + 4 + 4)
// 32 bytes: the chain code
bytes = bytes.concat(this.chaincode)
assert.equal(bytes.length, 4 + 1 + 4 + 4 + 32)
// 33 bytes: the public key or private key data
// (0x02 + X or 0x03 + X for public keys, 0x00 + k for private keys)
if (this.type == 'priv') {
bytes.push(0)
bytes = bytes.concat(this.key.toBytes().slice(0, 32))
} else {
bytes = bytes.concat(this.key.toBytes())
}
assert.equal(bytes.length, 78)
if (enc == 'bytes') return bytes
if (enc == 'hex') return conv.bytesToHex(bytes)
var checksum = Crypto.SHA256(Crypto.SHA256(bytes, {asBytes: true}), { asBytes: true }).slice(0,4)
bytes = bytes.concat(checksum)
return base58.encode(bytes)
}
BIP32key.prototype.ckd = function(i) {
var priv, pub, newkey, fingerprint, blob, I;
if (this.type == 'priv') {
priv = this.key.export('bytes')
pub = this.key.getPub().export('bytes')
}
else pub = this.key.export('bytes')
if (i >= BIP32_PRIME) {
if (!priv) throw new Error("Can't do private derivation on public key!")
blob = [0].concat(priv.slice(0,32),util.numToBytes(i,4).reverse())
}
else blob = pub.concat(util.numToBytes(i,4).reverse())
I = Crypto.HMAC(Crypto.SHA512,blob,this.chaincode,{ asBytes: true })
if (this.type == 'priv') {
newkey = this.key.add(ECKey(I.slice(0,32).concat([1])))
fingerprint = util.sha256ripe160(this.key.getPub().export('bytes')).slice(0,4)
}
else {
newkey = this.key.add(ECKey(I.slice(0,32).concat([1])).getPub());
fingerprint = util.sha256ripe160(this.key.export('bytes')).slice(0,4)
}
return new BIP32key({
vbytes: this.vbytes,
type: this.type,
depth: this.depth + 1,
fingerprint: fingerprint,
i: i,
chaincode: I.slice(32),
key: newkey
})
}
BIP32key.prototype.clone = function() {
return new BIP32key(this);
}
BIP32key.prototype.privtopub = BIP32key.prototype.getPub = function() {
if (this.type == 'pub') return this.clone()
return new BIP32key({
vbytes: conv.stringToBytes(PUBDERIV),
type: 'pub',
depth: this.depth,
fingerprint: this.fingerprint,
i: this.i,
chaincode: this.chaincode,
key: this.key.getPub()
})
}
BIP32key.fromMasterKey = function(seed) {
var I = Crypto.HMAC(Crypto.SHA512,seed, 'Bitcoin seed', { asBytes: true })
return new BIP32key({
vbytes: conv.stringToBytes(PRIVDERIV),
type: 'priv',
depth: 0,
fingerprint: [0,0,0,0],
i: 0,
chaincode: I.slice(32),
key: new ECKey(I.slice(0,32).concat([1]),true)
})
}
BIP32key.prototype.getKey = function() { return this.key }
BIP32key.prototype.bitcoinAddress = function() {
return new Address(util.sha256ripe160(this.getPub().key.export('bytes')))
}
module.exports = BIP32key;

View file

@ -1,168 +0,0 @@
/* global describe, it */
var assert = require('assert');
var BIP32key = require('../src/bip32.js');
var convert = require('../src/convert.js')
var hexToBytes = convert.hexToBytes;
var bytesToString = convert.bytesToString;
var BIP32_PRIME = 0x80000000;
function checkKey(key, extPriv, extPub) {
assert.equal(key.serialize(), extPriv);
assert.equal(key.getPub().serialize(), extPub);
}
function checkKeyHex(key, extPriv, extPub) {
assert.equal(key.serialize('hex'), extPriv);
assert.equal(key.getPub().serialize('hex'), extPub);
}
describe('BIP32key', function() {
describe('BIP-0032 Test Vectors', function() {
describe('vector 1', function() {
// Extracted from https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#Test_Vectors
var masterHex = '000102030405060708090a0b0c0d0e0f'
, masterBytes = convert.hexToBytes(masterHex)
, masterString = convert.bytesToString(masterBytes)
, m = BIP32key.fromMasterKey(masterString)
it('handles chain m', function() {
var chain = m
// Identifier
//assert.equal(???, '3442193e1bb70916e914552172cd4e2dbc9df811')
// Fingerprint
// assert.equal('???', 0x3442193e)
// Main address
assert.equal(chain.bitcoinAddress().toString(), '15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma')
// Secret key
chain.key.compressed = false
assert.equal(chain.key.toHex(), 'e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35')
chain.key.compressed = true
assert.equal(chain.key.toWif(), 'L52XzL2cMkHxqxBXRyEpnPQZGUs3uKiL3R11XbAdHigRzDozKZeW')
// Public key
assert.equal(chain.getPub().key.toHex(), '0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2')
// Chain code
assert.equal(convert.bytesToHex(chain.chaincode), '873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508')
// Serialized
assert.equal(chain.getPub().serialize('hex'), '0488b21e000000000000000000873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d5080339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2')
assert.equal(chain.serialize('hex'), '0488ade4000000000000000000873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d50800e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35')
assert.equal(chain.getPub().serialize('base58'), 'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8')
assert.equal(chain.serialize('base58'), 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi')
})
it('handles chain m/0\'', function() {
var chain = m.ckd(BIP32key.PRIME + 0)
// Identifier
//assert.equal(???, '5c1bd648ed23aa5fd50ba52b2457c11e9e80a6a7')
// Fingerprint
// assert.equal('???', 0x5c1bd648)
// Main address
// assert.equal(chain.bitcoinAddress().toString(), '19Q2WoS5hSS6T8GjhK8KZLMgmWaq4neXrh')
// Secret key
chain.key.compressed = false
assert.equal(chain.key.toHex(), 'edb2e14f9ee77d26dd93b4ecede8d16ed408ce149b6cd80b0715a2d911a0afea')
chain.key.compressed = true
assert.equal(chain.key.toWif(), 'L5BmPijJjrKbiUfG4zbiFKNqkvuJ8usooJmzuD7Z8dkRoTThYnAT')
// Public key
assert.equal(chain.getPub().key.toHex(), '035a784662a4a20a65bf6aab9ae98a6c068a81c52e4b032c0fb5400c706cfccc56')
// Chain code
assert.equal(convert.bytesToHex(chain.chaincode), '47fdacbd0f1097043b78c63c20c34ef4ed9a111d980047ad16282c7ae6236141')
// Serialized
assert.equal(chain.getPub().serialize('hex'), '0488b21e013442193e8000000047fdacbd0f1097043b78c63c20c34ef4ed9a111d980047ad16282c7ae6236141035a784662a4a20a65bf6aab9ae98a6c068a81c52e4b032c0fb5400c706cfccc56')
assert.equal(chain.serialize('hex'), '0488ade4013442193e8000000047fdacbd0f1097043b78c63c20c34ef4ed9a111d980047ad16282c7ae623614100edb2e14f9ee77d26dd93b4ecede8d16ed408ce149b6cd80b0715a2d911a0afea')
assert.equal(chain.getPub().serialize('base58'), 'xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw')
assert.equal(chain.serialize('base58'), 'xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7')
})
it('handles chain m/0\'/1', function() {
var chain = m.ckd(BIP32key.PRIME + 0).ckd(1)
// Identifier
//assert.equal(???, 'bef5a2f9a56a94aab12459f72ad9cf8cf19c7bbe')
// Fingerprint
// assert.equal('???', 0xbef5a2f9)
// Main address
// assert.equal(chain.bitcoinAddress().toString(), '1JQheacLPdM5ySCkrZkV66G2ApAXe1mqLj')
// Secret key
chain.key.compressed = false
assert.equal(chain.key.toHex(), '3c6cb8d0f6a264c91ea8b5030fadaa8e538b020f0a387421a12de9319dc93368')
chain.key.compressed = true
assert.equal(chain.key.toWif(), 'KyFAjQ5rgrKvhXvNMtFB5PCSKUYD1yyPEe3xr3T34TZSUHycXtMM')
// Public key
assert.equal(chain.getPub().key.toHex(), '03501e454bf00751f24b1b489aa925215d66af2234e3891c3b21a52bedb3cd711c')
// Chain code
assert.equal(convert.bytesToHex(chain.chaincode), '2a7857631386ba23dacac34180dd1983734e444fdbf774041578e9b6adb37c19')
// Serialized
assert.equal(chain.getPub().serialize('hex'), '0488b21e025c1bd648000000012a7857631386ba23dacac34180dd1983734e444fdbf774041578e9b6adb37c1903501e454bf00751f24b1b489aa925215d66af2234e3891c3b21a52bedb3cd711c')
assert.equal(chain.serialize('hex'), '0488ade4025c1bd648000000012a7857631386ba23dacac34180dd1983734e444fdbf774041578e9b6adb37c19003c6cb8d0f6a264c91ea8b5030fadaa8e538b020f0a387421a12de9319dc93368')
assert.equal(chain.getPub().serialize('base58'), 'xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ')
assert.equal(chain.serialize('base58'), 'xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs')
})
})
it('handles vector 2', function() {
return
var seed_str = 'fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542';
var seed = bytesToString(hexToBytes(seed_str));
var key = new BIP32key(seed);
checkKey(key,
'xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U',
'xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB');
var branch = key.ckd(0);
checkKey(branch,
'xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt',
'xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH');
var branch2 = branch.ckd(2147483647 + BIP32_PRIME);
checkKey(branch2,
'xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9',
'xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a');
var branch3 = branch2.ckd(1);
checkKey(branch3,
'xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef',
'xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon');
var branch4 = branch3.ckd(2147483646 + BIP32_PRIME);
checkKey(branch4,
'xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc',
'xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL');
var branch5 = branch4.ckd(2);
checkKey(branch5,
'xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j',
'xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt');
})
})
})