Merge branch 'master' of github.com:vbuterin/bitcoinjs-lib

This commit is contained in:
Vitalik Buterin 2014-01-10 15:18:09 -05:00
commit 36ce412bcb
3 changed files with 27 additions and 25 deletions

View file

@ -1,22 +1,19 @@
var Script = require('./script'), var util = require('./util'),
util = require('./util'), Address = require('./address'),
conv = require('./convert'), conv = require('./convert'),
ECKey = require('./eckey').ECKey, ECKey = require('./eckey').ECKey,
ECPubKey = require('./eckey').ECPubKey, ECPubKey = require('./eckey').ECPubKey,
base58 = require('./base58'), base58 = require('./base58'),
Crypto = require('./crypto-js/crypto'), Crypto = require('./crypto-js/crypto');
ECPointFp = require('./jsbn/ec').ECPointFp,
sec = require('./jsbn/sec'),
ecparams = sec("secp256k1");
var BIP32key = function(opts) { var BIP32key = function(opts) {
if (!opts) opts = {} if (!opts) opts = {}
if (typeof opts == "string") { if (typeof opts == 'string') {
try { try {
opts = BIP32key.prototype.deserialize(opts); opts = BIP32key.deserialize(opts);
} }
catch(e) { catch(e) {
opts = BIP32key.prototype.fromMasterKey(opts); opts = BIP32key.fromMasterKey(opts);
} }
} }
this.vbytes = opts.vbytes; this.vbytes = opts.vbytes;
@ -32,14 +29,14 @@ var BIP32key = function(opts) {
var PRIVDERIV = BIP32key.PRIVDERIV = '\x04\x88\xAD\xE4' var PRIVDERIV = BIP32key.PRIVDERIV = '\x04\x88\xAD\xE4'
var PUBDERIV = BIP32key.PUBDERIV = '\x04\x88\xB2\x1E' var PUBDERIV = BIP32key.PUBDERIV = '\x04\x88\xB2\x1E'
BIP32key.prototype.deserialize = function(str) { BIP32key.deserialize = function(str) {
var bytes = base58.decode(str) var bytes = base58.decode(str)
var front = bytes.slice(0,bytes.length-4), var front = bytes.slice(0,bytes.length-4),
back = bytes.slice(bytes.length-4); back = bytes.slice(bytes.length-4);
var checksum = Crypto.SHA256(Crypto.SHA256(front,{asBytes: true}), {asBytes: true}) var checksum = Crypto.SHA256(Crypto.SHA256(front,{asBytes: true}), {asBytes: true})
.slice(0,4); .slice(0,4);
if (""+checksum != ""+back) { if ('' + checksum != '' + back) {
throw new Error("Checksum failed"); throw new Error('Checksum failed');
} }
var type = conv.bytesToString(bytes.slice(0,4)) == PRIVDERIV ? 'priv' : 'pub'; var type = conv.bytesToString(bytes.slice(0,4)) == PRIVDERIV ? 'priv' : 'pub';
return new BIP32key({ return new BIP32key({
@ -80,7 +77,7 @@ BIP32key.prototype.ckd = function(i) {
blob = [0].concat(priv.slice(0,32),util.numToBytes(i,4).reverse()) blob = [0].concat(priv.slice(0,32),util.numToBytes(i,4).reverse())
} }
else blob = pub.concat(util.numToBytes(i,4).reverse()) else blob = pub.concat(util.numToBytes(i,4).reverse())
I = Crypto.HMAC(Crypto.SHA512,blob,this.chaincode,{ asBytes: true }) I = Crypto.HMAC(Crypto.SHA512,blob,this.chaincode,{ asBytes: true })
if (this.type == 'priv') { if (this.type == 'priv') {
@ -119,8 +116,8 @@ BIP32key.prototype.privtopub = BIP32key.prototype.getPub = function() {
}) })
} }
BIP32key.prototype.fromMasterKey = function(seed) { BIP32key.fromMasterKey = function(seed) {
var I = Bitcoin.Crypto.HMAC(Bitcoin.Crypto.SHA512,seed,"Bitcoin seed",{ asBytes: true }) var I = Crypto.HMAC(Crypto.SHA512,seed, 'Bitcoin seed' , { asBytes: true })
return new BIP32key({ return new BIP32key({
vbytes: conv.stringToBytes(PRIVDERIV), vbytes: conv.stringToBytes(PRIVDERIV),
type: 'priv', type: 'priv',
@ -134,4 +131,8 @@ BIP32key.prototype.fromMasterKey = function(seed) {
BIP32key.prototype.getKey = function() { return this.key } BIP32key.prototype.getKey = function() { return this.key }
BIP32key.prototype.bitcoinAddress = function() {
return new Address(util.sha256ripe160(this.getPub().key.export('bytes')))
}
module.exports = BIP32key; module.exports = BIP32key;

View file

@ -3,6 +3,7 @@ var util = require('./util');
var SecureRandom = require('./jsbn/rng'); var SecureRandom = require('./jsbn/rng');
var BigInteger = require('./jsbn/jsbn'); var BigInteger = require('./jsbn/jsbn');
var conv = require('./convert') var conv = require('./convert')
var Crypto = require('./crypto-js/crypto.js')
var ECPointFp = require('./jsbn/ec').ECPointFp; var ECPointFp = require('./jsbn/ec').ECPointFp;
@ -42,12 +43,12 @@ function deterministicGenerateK(hash,key) {
var k = []; var k = [];
for (var i = 0;i < 32;i++) v.push(1); for (var i = 0;i < 32;i++) v.push(1);
for (var i = 0;i < 32;i++) k.push(0); for (var i = 0;i < 32;i++) k.push(0);
k = Bitcoin.Crypto.HMAC(Bitcoin.Crypto.SHA256,v.concat([0]).concat(key).concat(hash),k,{ asBytes: true }) k = Crypto.HMAC(Crypto.SHA256,v.concat([0]).concat(key).concat(hash),k,{ asBytes: true })
v = Bitcoin.Crypto.HMAC(Bitcoin.Crypto.SHA256,v,k,{ asBytes: true }) v = Crypto.HMAC(Crypto.SHA256,v,k,{ asBytes: true })
k = Bitcoin.Crypto.HMAC(Bitcoin.Crypto.SHA256,v.concat([1]).concat(key).concat(hash),k,{ asBytes: true }) k = Crypto.HMAC(Crypto.SHA256,v.concat([1]).concat(key).concat(hash),k,{ asBytes: true })
v = Bitcoin.Crypto.HMAC(Bitcoin.Crypto.SHA256,v,k,{ asBytes: true }) v = Crypto.HMAC(Crypto.SHA256,v,k,{ asBytes: true })
v = Bitcoin.Crypto.HMAC(Bitcoin.Crypto.SHA256,v,k,{ asBytes: true }) v = Crypto.HMAC(Crypto.SHA256,v,k,{ asBytes: true })
return Bitcoin.BigInteger.fromByteArrayUnsigned(v); return BigInteger.fromByteArrayUnsigned(v);
} }
var ECDSA = { var ECDSA = {

View file

@ -376,11 +376,11 @@ ECPointFp.decodeFrom = function (ecparams, enc) {
var xBa = enc.slice(1), var xBa = enc.slice(1),
x = BigInteger.fromByteArrayUnsigned(xBa), x = BigInteger.fromByteArrayUnsigned(xBa),
p = ecparams.getQ(), p = ecparams.getQ(),
xCubedPlus7 = x.multiply(x).multiply(x).add(new Bitcoin.BigInteger('7')).mod(p), xCubedPlus7 = x.multiply(x).multiply(x).add(new BigInteger('7')).mod(p),
pPlus1Over4 = p.add(new Bitcoin.BigInteger('1')) pPlus1Over4 = p.add(new BigInteger('1'))
.divide(new Bitcoin.BigInteger('4')), .divide(new BigInteger('4')),
y = xCubedPlus7.modPow(pPlus1Over4,p); y = xCubedPlus7.modPow(pPlus1Over4,p);
if (y.mod(new Bitcoin.BigInteger('2')).toString() != ''+(type % 2)) { if (y.mod(new BigInteger('2')).toString() != ''+(type % 2)) {
y = p.subtract(y) y = p.subtract(y)
} }
} }