get BIP32 working in the browser by exposing more crypto
This commit is contained in:
parent
63ce079f2b
commit
c03d3c5818
12
Key.js
12
Key.js
|
@ -10,7 +10,12 @@ if (process.versions) {
|
||||||
var ECKey = require('./browser/vendor-bundle.js').ECKey;
|
var ECKey = require('./browser/vendor-bundle.js').ECKey;
|
||||||
var buffertools = require('buffertools');
|
var buffertools = require('buffertools');
|
||||||
|
|
||||||
var bufferToArray = function(buffer) {
|
var kSpec = function() {
|
||||||
|
this._pub = null;
|
||||||
|
this.compressed = true; // default
|
||||||
|
};
|
||||||
|
|
||||||
|
var bufferToArray = kSpec.bufferToArray = function(buffer) {
|
||||||
var ret = [];
|
var ret = [];
|
||||||
|
|
||||||
var l = buffer.length;
|
var l = buffer.length;
|
||||||
|
@ -21,11 +26,6 @@ if (process.versions) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
var kSpec = function() {
|
|
||||||
this._pub = null;
|
|
||||||
this.compressed = true; // default
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
Object.defineProperty(kSpec.prototype, 'public', {
|
Object.defineProperty(kSpec.prototype, 'public', {
|
||||||
set: function(p){
|
set: function(p){
|
||||||
|
|
67
Point.js
67
Point.js
|
@ -1,6 +1,20 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
var imports = require('soop').imports();
|
var imports = require('soop').imports();
|
||||||
var Key = imports.Key || require('./Key');
|
var Key = imports.Key || require('./Key');
|
||||||
var bignum = imports.bignum || require('bignum');
|
var bignum = imports.bignum || require('bignum');
|
||||||
|
var assert = require('assert');
|
||||||
|
|
||||||
|
//browser
|
||||||
|
if (!process.versions) {
|
||||||
|
var ECKey = require('./browser/vendor-bundle.js').ECKey;
|
||||||
|
var ECPointFp = require('./browser/vendor-bundle.js').ECPointFp;
|
||||||
|
var ECFieldElementFp = require('./browser/vendor-bundle.js').ECFieldElementFp;
|
||||||
|
var getSECCurveByName = require('./browser/vendor-bundle.js').getSECCurveByName;
|
||||||
|
var BigInteger = require('./browser/vendor-bundle.js').BigInteger;
|
||||||
|
var should = require('chai').should();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//a point on the secp256k1 curve
|
//a point on the secp256k1 curve
|
||||||
//x and y are bignums
|
//x and y are bignums
|
||||||
|
@ -27,6 +41,35 @@ Point.add = function(p1, p2) {
|
||||||
|
|
||||||
//browser
|
//browser
|
||||||
else {
|
else {
|
||||||
|
var ecparams = getSECCurveByName('secp256k1');
|
||||||
|
|
||||||
|
var p1xhex = p1.x.toBuffer({size: 32}).toString('hex');
|
||||||
|
var p1x = new BigInteger(p1xhex, 16);
|
||||||
|
var p1yhex = p1.y.toBuffer({size: 32}).toString('hex');
|
||||||
|
var p1y = new BigInteger(p1yhex, 16);
|
||||||
|
var p1px = new ECFieldElementFp(ecparams.getCurve().getQ(), p1x);
|
||||||
|
var p1py = new ECFieldElementFp(ecparams.getCurve().getQ(), p1y);
|
||||||
|
var p1p = new ECPointFp(ecparams.getCurve(), p1px, p1py);
|
||||||
|
|
||||||
|
var p2xhex = p2.x.toBuffer({size: 32}).toString('hex');
|
||||||
|
var p2x = new BigInteger(p2xhex, 16);
|
||||||
|
var p2yhex = p2.y.toBuffer({size: 32}).toString('hex');
|
||||||
|
var p2y = new BigInteger(p2yhex, 16);
|
||||||
|
var p2px = new ECFieldElementFp(ecparams.getCurve().getQ(), p2x);
|
||||||
|
var p2py = new ECFieldElementFp(ecparams.getCurve().getQ(), p2y);
|
||||||
|
var p2p = new ECPointFp(ecparams.getCurve(), p2px, p2py);
|
||||||
|
|
||||||
|
var p = p1p.add(p2p);
|
||||||
|
|
||||||
|
var point = new Point();
|
||||||
|
var pointxbuf = new Buffer(p.getX().toBigInteger().toByteArrayUnsigned());
|
||||||
|
point.x = bignum.fromBuffer(pointxbuf, {size: pointxbuf.length});
|
||||||
|
assert(pointxbuf.length <= 32);
|
||||||
|
var pointybuf = new Buffer(p.getY().toBigInteger().toByteArrayUnsigned());
|
||||||
|
assert(pointybuf.length <= 32);
|
||||||
|
point.y = bignum.fromBuffer(pointybuf, {size: pointybuf.length});
|
||||||
|
|
||||||
|
return point;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -49,6 +92,15 @@ Point.fromKey = function(key) {
|
||||||
|
|
||||||
//browser
|
//browser
|
||||||
else {
|
else {
|
||||||
|
var point = new Point();
|
||||||
|
var pubKeyBuf = new Buffer(key.public);
|
||||||
|
var key2 = new ECKey();
|
||||||
|
key2.setCompressed(key.compressed);
|
||||||
|
key2.setPub(Key.bufferToArray(pubKeyBuf));
|
||||||
|
key2.setCompressed(false);
|
||||||
|
point.x = bignum.fromBuffer((new Buffer(key2.getPub())).slice(1, 33), {size: 32});
|
||||||
|
point.y = bignum.fromBuffer((new Buffer(key2.getPub())).slice(33, 65), {size: 32});
|
||||||
|
return point;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -62,13 +114,24 @@ Point.prototype.toKey = function() {
|
||||||
var key = new Key();
|
var key = new Key();
|
||||||
key.compressed = false;
|
key.compressed = false;
|
||||||
var prefix = new Buffer([0x04]);
|
var prefix = new Buffer([0x04]);
|
||||||
key.public = Buffer.concat([prefix, xbuf, ybuf]); //this is probably wrong
|
key.public = Buffer.concat([prefix, xbuf, ybuf]); //this might be wrong
|
||||||
key.compressed = true;
|
key.compressed = true;
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
//browser
|
//browser
|
||||||
else {
|
else {
|
||||||
|
var xbuf = this.x.toBuffer({size: 32});
|
||||||
|
var ybuf = this.y.toBuffer({size: 32});
|
||||||
|
var key = new ECKey();
|
||||||
|
key.setCompressed(false);
|
||||||
|
var prefix = new Buffer([0x04]);
|
||||||
|
var pub = Buffer.concat([prefix, xbuf, ybuf]); //this might be wrong
|
||||||
|
key.setPub(Key.bufferToArray(pub));
|
||||||
|
key.setCompressed(true);
|
||||||
|
var key2 = new Key();
|
||||||
|
key2.public = new Buffer(key.getPub());
|
||||||
|
return key2;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -314,3 +314,6 @@ ECCurveFp.prototype.equals = curveFpEquals;
|
||||||
ECCurveFp.prototype.getInfinity = curveFpGetInfinity;
|
ECCurveFp.prototype.getInfinity = curveFpGetInfinity;
|
||||||
ECCurveFp.prototype.fromBigInteger = curveFpFromBigInteger;
|
ECCurveFp.prototype.fromBigInteger = curveFpFromBigInteger;
|
||||||
ECCurveFp.prototype.decodePointHex = curveFpDecodePointHex;
|
ECCurveFp.prototype.decodePointHex = curveFpDecodePointHex;
|
||||||
|
|
||||||
|
module.exports.ECPointFp = ECPointFp;
|
||||||
|
module.exports.ECFieldElementFp = ECFieldElementFp;
|
||||||
|
|
|
@ -654,3 +654,5 @@ BigInteger.prototype.square = bnSquare;
|
||||||
// int hashCode()
|
// int hashCode()
|
||||||
// long longValue()
|
// long longValue()
|
||||||
// static BigInteger valueOf(long val)
|
// static BigInteger valueOf(long val)
|
||||||
|
|
||||||
|
module.exports.BigInteger = BigInteger;
|
||||||
|
|
|
@ -171,3 +171,5 @@ function getSECCurveByName(name) {
|
||||||
if(name == "secp256r1") return secp256r1();
|
if(name == "secp256r1") return secp256r1();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports.getSECCurveByName = getSECCurveByName;
|
||||||
|
|
|
@ -94,6 +94,7 @@ describe('BIP32', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should get m/0'/1 ext. public key from m/0' public key from test vector 1", function() {
|
it("should get m/0'/1 ext. public key from m/0' public key from test vector 1", function() {
|
||||||
|
//TEST
|
||||||
var bip32 = new BIP32(vector1_m_private);
|
var bip32 = new BIP32(vector1_m_private);
|
||||||
var child = bip32.derive("m/0'");
|
var child = bip32.derive("m/0'");
|
||||||
var child_pub = new BIP32(child.extendedPublicKeyString());
|
var child_pub = new BIP32(child.extendedPublicKeyString());
|
||||||
|
|
Loading…
Reference in New Issue