remove "(classname): " from tests
...to reduce the burden on writing new code
This commit is contained in:
parent
912bed1d9c
commit
4cff6a41f4
@ -21,7 +21,7 @@ Address.prototype.fromPubkey = function(pubkey, network, compressed) {
|
|||||||
Address.prototype.fromString = function(str) {
|
Address.prototype.fromString = function(str) {
|
||||||
var buf = base58check.decode(str);
|
var buf = base58check.decode(str);
|
||||||
if (buf.length !== 1 + 20)
|
if (buf.length !== 1 + 20)
|
||||||
throw new Error('address: Address buffers must be exactly 21 bytes');
|
throw new Error('Address buffers must be exactly 21 bytes');
|
||||||
var version = buf[0];
|
var version = buf[0];
|
||||||
if (version === constants['mainnet']['pubkeyhash']) {
|
if (version === constants['mainnet']['pubkeyhash']) {
|
||||||
this.network = 'mainnet';
|
this.network = 'mainnet';
|
||||||
@ -64,11 +64,11 @@ Address.prototype.toString = function() {
|
|||||||
|
|
||||||
Address.prototype.validate = function() {
|
Address.prototype.validate = function() {
|
||||||
if (!Buffer.isBuffer(this.hash) || this.hash.length !== 20)
|
if (!Buffer.isBuffer(this.hash) || this.hash.length !== 20)
|
||||||
throw new Error('address: hash must be a buffer of 20 bytes');
|
throw new Error('hash must be a buffer of 20 bytes');
|
||||||
if (this.network !== 'mainnet' && this.network !== 'testnet')
|
if (this.network !== 'mainnet' && this.network !== 'testnet')
|
||||||
throw new Error('address: network must be "mainnet" or "testnet"');
|
throw new Error('network must be "mainnet" or "testnet"');
|
||||||
if (this.type !== 'pubkeyhash' && this.type !== 'p2sh')
|
if (this.type !== 'pubkeyhash' && this.type !== 'p2sh')
|
||||||
throw new Error('address: type must be "pubkeyhash" or "p2sh"');
|
throw new Error('type must be "pubkeyhash" or "p2sh"');
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -9,12 +9,12 @@ var Base58Check = function Base58Check(buf) {
|
|||||||
|
|
||||||
Base58Check.decode = function(s) {
|
Base58Check.decode = function(s) {
|
||||||
if (typeof s !== 'string')
|
if (typeof s !== 'string')
|
||||||
throw new Error('Base58Check: Input must be a string');
|
throw new Error('Input must be a string');
|
||||||
|
|
||||||
var buf = base58.decode(s);
|
var buf = base58.decode(s);
|
||||||
|
|
||||||
if (buf.length < 4)
|
if (buf.length < 4)
|
||||||
throw new Error("Base58Check: Input string too short");
|
throw new Error("Input string too short");
|
||||||
|
|
||||||
var data = buf.slice(0, -4);
|
var data = buf.slice(0, -4);
|
||||||
var csum = buf.slice(-4);
|
var csum = buf.slice(-4);
|
||||||
@ -23,14 +23,14 @@ Base58Check.decode = function(s) {
|
|||||||
var hash4 = hash.slice(0, 4);
|
var hash4 = hash.slice(0, 4);
|
||||||
|
|
||||||
if (csum.toString('hex') !== hash4.toString('hex'))
|
if (csum.toString('hex') !== hash4.toString('hex'))
|
||||||
throw new Error("Base58Check: Checksum mismatch");
|
throw new Error("Checksum mismatch");
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
Base58Check.encode = function(buf) {
|
Base58Check.encode = function(buf) {
|
||||||
if (!Buffer.isBuffer(buf))
|
if (!Buffer.isBuffer(buf))
|
||||||
throw new Error('Base58Check: Input must be a buffer');
|
throw new Error('Input must be a buffer');
|
||||||
var checkedBuf = new Buffer(buf.length + 4);
|
var checkedBuf = new Buffer(buf.length + 4);
|
||||||
var hash = sha256sha256(buf);
|
var hash = sha256sha256(buf);
|
||||||
buf.copy(checkedBuf);
|
buf.copy(checkedBuf);
|
||||||
|
|||||||
30
lib/bip32.js
30
lib/bip32.js
@ -37,14 +37,14 @@ BIP32.prototype.fromRandom = function(network) {
|
|||||||
BIP32.prototype.fromString = function(str) {
|
BIP32.prototype.fromString = function(str) {
|
||||||
var decoded = base58.decode(str);
|
var decoded = base58.decode(str);
|
||||||
if (decoded.length != 82)
|
if (decoded.length != 82)
|
||||||
throw new Error('bip32: Not enough data, expected 82 and received ' + decoded.length);
|
throw new Error('gcNot enough data, expected 82 and received ' + decoded.length);
|
||||||
var checksum = decoded.slice(78, 82);
|
var checksum = decoded.slice(78, 82);
|
||||||
var bytes = decoded.slice(0, 78);
|
var bytes = decoded.slice(0, 78);
|
||||||
|
|
||||||
var hash = Hash.sha256sha256(bytes);
|
var hash = Hash.sha256sha256(bytes);
|
||||||
|
|
||||||
if (hash[0] != checksum[0] || hash[1] != checksum[1] || hash[2] != checksum[2] || hash[3] != checksum[3])
|
if (hash[0] != checksum[0] || hash[1] != checksum[1] || hash[2] != checksum[2] || hash[3] != checksum[3])
|
||||||
throw new Error('bip32: Invalid checksum');
|
throw new Error('gcInvalid checksum');
|
||||||
|
|
||||||
if (bytes !== undefined && bytes !== null)
|
if (bytes !== undefined && bytes !== null)
|
||||||
this.initFromBytes(bytes);
|
this.initFromBytes(bytes);
|
||||||
@ -55,11 +55,11 @@ BIP32.prototype.fromSeed = function(bytes, network) {
|
|||||||
network = 'mainnet';
|
network = 'mainnet';
|
||||||
|
|
||||||
if (!Buffer.isBuffer(bytes))
|
if (!Buffer.isBuffer(bytes))
|
||||||
throw new Error('bip32: bytes must be a buffer');
|
throw new Error('gcbytes must be a buffer');
|
||||||
if (bytes.length < 128 / 8)
|
if (bytes.length < 128 / 8)
|
||||||
throw new Error('bip32: Need more than 128 bytes of entropy');
|
throw new Error('gcNeed more than 128 bytes of entropy');
|
||||||
if (bytes.length > 512 / 8)
|
if (bytes.length > 512 / 8)
|
||||||
throw new Error('bip32: More than 512 bytes of entropy is nonstandard');
|
throw new Error('gcMore than 512 bytes of entropy is nonstandard');
|
||||||
var hash = Hash.sha512hmac(bytes, new Buffer('Bitcoin seed'));
|
var hash = Hash.sha512hmac(bytes, new Buffer('Bitcoin seed'));
|
||||||
|
|
||||||
this.depth = 0x00;
|
this.depth = 0x00;
|
||||||
@ -82,7 +82,7 @@ BIP32.prototype.fromSeed = function(bytes, network) {
|
|||||||
BIP32.prototype.initFromBytes = function(bytes) {
|
BIP32.prototype.initFromBytes = function(bytes) {
|
||||||
// Both pub and private extended keys are 78 bytes
|
// Both pub and private extended keys are 78 bytes
|
||||||
if (bytes.length != 78)
|
if (bytes.length != 78)
|
||||||
throw new Error('bip32: not enough data');
|
throw new Error('gcnot enough data');
|
||||||
|
|
||||||
this.version = u32(bytes.slice(0, 4));
|
this.version = u32(bytes.slice(0, 4));
|
||||||
this.depth = u8(bytes.slice(4, 5));
|
this.depth = u8(bytes.slice(4, 5));
|
||||||
@ -112,7 +112,7 @@ BIP32.prototype.initFromBytes = function(bytes) {
|
|||||||
this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer());
|
this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer());
|
||||||
this.hasPrivateKey = false;
|
this.hasPrivateKey = false;
|
||||||
} else {
|
} else {
|
||||||
throw new Error('bip32: Invalid key');
|
throw new Error('gcInvalid key');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.buildExtendedPublicKey();
|
this.buildExtendedPublicKey();
|
||||||
@ -133,7 +133,7 @@ BIP32.prototype.buildExtendedPublicKey = function() {
|
|||||||
v = constants.testnet.bip32pubkey;
|
v = constants.testnet.bip32pubkey;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error('bip32: Unknown version');
|
throw new Error('gcUnknown version');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Version
|
// Version
|
||||||
@ -164,7 +164,7 @@ BIP32.prototype.extendedPublicKeyString = function(format) {
|
|||||||
} else if (format === 'hex') {
|
} else if (format === 'hex') {
|
||||||
return this.extendedPublicKey.toString('hex');;
|
return this.extendedPublicKey.toString('hex');;
|
||||||
} else {
|
} else {
|
||||||
throw new Error('bip32: bad format');
|
throw new Error('gcbad format');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,7 +200,7 @@ BIP32.prototype.extendedPrivateKeyString = function(format) {
|
|||||||
} else if (format === 'hex') {
|
} else if (format === 'hex') {
|
||||||
return this.extendedPrivateKey.toString('hex');
|
return this.extendedPrivateKey.toString('hex');
|
||||||
} else {
|
} else {
|
||||||
throw new Error('bip32: bad format');
|
throw new Error('gcbad format');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -217,12 +217,12 @@ BIP32.prototype.derive = function(path) {
|
|||||||
var c = e[i];
|
var c = e[i];
|
||||||
|
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
if (c != 'm') throw new Error('bip32: invalid path');
|
if (c != 'm') throw new Error('gcinvalid path');
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parseInt(c.replace("'", "")).toString() !== c.replace("'", ""))
|
if (parseInt(c.replace("'", "")).toString() !== c.replace("'", ""))
|
||||||
throw new Error('bip32: invalid path');
|
throw new Error('gcinvalid path');
|
||||||
|
|
||||||
var usePrivate = (c.length > 1) && (c[c.length - 1] == '\'');
|
var usePrivate = (c.length > 1) && (c[c.length - 1] == '\'');
|
||||||
var childIndex = parseInt(usePrivate ? c.slice(0, c.length - 1) : c) & 0x7fffffff;
|
var childIndex = parseInt(usePrivate ? c.slice(0, c.length - 1) : c) & 0x7fffffff;
|
||||||
@ -238,7 +238,7 @@ BIP32.prototype.derive = function(path) {
|
|||||||
|
|
||||||
BIP32.prototype.deriveChild = function(i) {
|
BIP32.prototype.deriveChild = function(i) {
|
||||||
if (typeof i !== 'number')
|
if (typeof i !== 'number')
|
||||||
throw new Error('bip32: i must be a number');
|
throw new Error('gci must be a number');
|
||||||
|
|
||||||
var ib = [];
|
var ib = [];
|
||||||
ib.push((i >> 24) & 0xff);
|
ib.push((i >> 24) & 0xff);
|
||||||
@ -254,7 +254,7 @@ BIP32.prototype.deriveChild = function(i) {
|
|||||||
this.version == constants.testnet.bip32privkey);
|
this.version == constants.testnet.bip32privkey);
|
||||||
|
|
||||||
if (usePrivate && (!this.hasPrivateKey || !isPrivate))
|
if (usePrivate && (!this.hasPrivateKey || !isPrivate))
|
||||||
throw new Error('bip32: Cannot do private key derivation without private key');
|
throw new Error('gcCannot do private key derivation without private key');
|
||||||
|
|
||||||
var ret = null;
|
var ret = null;
|
||||||
if (this.hasPrivateKey) {
|
if (this.hasPrivateKey) {
|
||||||
@ -329,7 +329,7 @@ BIP32.prototype.toString = function() {
|
|||||||
|
|
||||||
function uint(f, size) {
|
function uint(f, size) {
|
||||||
if (f.length < size)
|
if (f.length < size)
|
||||||
throw new Error('bip32: not enough data');
|
throw new Error('gcnot enough data');
|
||||||
var n = 0;
|
var n = 0;
|
||||||
for (var i = 0; i < size; i++) {
|
for (var i = 0; i < size; i++) {
|
||||||
n *= 256;
|
n *= 256;
|
||||||
|
|||||||
@ -141,7 +141,7 @@ ECDSA.prototype.sign = function() {
|
|||||||
var d = privkey.bn;
|
var d = privkey.bn;
|
||||||
|
|
||||||
if (!hashbuf || !privkey || !k || !d)
|
if (!hashbuf || !privkey || !k || !d)
|
||||||
throw new Error('ecdsa: invalid parameters');
|
throw new Error('invalid parameters');
|
||||||
|
|
||||||
var N = Point.getN();
|
var N = Point.getN();
|
||||||
var G = Point.getG();
|
var G = Point.getG();
|
||||||
|
|||||||
@ -46,12 +46,12 @@ Hash.sha512.blocksize = 1024;
|
|||||||
|
|
||||||
Hash.hmac = function(hashf, data, key) {
|
Hash.hmac = function(hashf, data, key) {
|
||||||
if (!Buffer.isBuffer(data) || !Buffer.isBuffer(key))
|
if (!Buffer.isBuffer(data) || !Buffer.isBuffer(key))
|
||||||
throw new Error('hash: data and key must be buffers');
|
throw new Error('data and key must be buffers');
|
||||||
|
|
||||||
//http://en.wikipedia.org/wiki/Hash-based_message_authentication_code
|
//http://en.wikipedia.org/wiki/Hash-based_message_authentication_code
|
||||||
//http://tools.ietf.org/html/rfc4868#section-2
|
//http://tools.ietf.org/html/rfc4868#section-2
|
||||||
if (!hashf.blocksize)
|
if (!hashf.blocksize)
|
||||||
throw new Error('hash: Blocksize for hash function unknown');
|
throw new Error('Blocksize for hash function unknown');
|
||||||
|
|
||||||
var blocksize = hashf.blocksize/8;
|
var blocksize = hashf.blocksize/8;
|
||||||
|
|
||||||
|
|||||||
@ -38,12 +38,12 @@ Point.prototype.getY = function() {
|
|||||||
Point.prototype.validate = function() {
|
Point.prototype.validate = function() {
|
||||||
var p2 = Point.fromX(this.getY().isOdd(), this.getX());
|
var p2 = Point.fromX(this.getY().isOdd(), this.getX());
|
||||||
if (!(p2.y.cmp(this.y) === 0))
|
if (!(p2.y.cmp(this.y) === 0))
|
||||||
throw new Error('point: Invalid y value of public key');
|
throw new Error('Invalid y value of public key');
|
||||||
if (!(this.getX().gt(-1) && this.getX().lt(Point.getN()))
|
if (!(this.getX().gt(-1) && this.getX().lt(Point.getN()))
|
||||||
||!(this.getY().gt(-1) && this.getY().lt(Point.getN())))
|
||!(this.getY().gt(-1) && this.getY().lt(Point.getN())))
|
||||||
throw new Error('point: Point does not lie on the curve');
|
throw new Error('Point does not lie on the curve');
|
||||||
if (!(this.mul(Point.getN()).isInfinity()))
|
if (!(this.mul(Point.getN()).isInfinity()))
|
||||||
throw new Error('point: Point times N must be infinity');
|
throw new Error('Point times N must be infinity');
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -13,11 +13,11 @@ var Privkey = function Privkey(bn, network, compressed) {
|
|||||||
|
|
||||||
Privkey.prototype.validate = function() {
|
Privkey.prototype.validate = function() {
|
||||||
if (!this.bn.lt(point.getN()))
|
if (!this.bn.lt(point.getN()))
|
||||||
throw new Error('privkey: Number must be less than N');
|
throw new Error('Number must be less than N');
|
||||||
if (typeof constants[this.network] === undefined)
|
if (typeof constants[this.network] === undefined)
|
||||||
throw new Error('privkey: Must specify the network ("mainnet" or "testnet")');
|
throw new Error('Must specify the network ("mainnet" or "testnet")');
|
||||||
if (typeof this.compressed !== 'boolean')
|
if (typeof this.compressed !== 'boolean')
|
||||||
throw new Error('privkey: Must specify whether the corresponding public key is compressed or not (true or false)');
|
throw new Error('Must specify whether the corresponding public key is compressed or not (true or false)');
|
||||||
};
|
};
|
||||||
|
|
||||||
Privkey.prototype.toWIF = function() {
|
Privkey.prototype.toWIF = function() {
|
||||||
@ -47,14 +47,14 @@ Privkey.prototype.fromWIF = function(str) {
|
|||||||
else if (buf.length === 1 + 32)
|
else if (buf.length === 1 + 32)
|
||||||
this.compressed = false;
|
this.compressed = false;
|
||||||
else
|
else
|
||||||
throw new Error('privkey: Length of buffer must be 33 (uncompressed) or 34 (compressed)');
|
throw new Error('Length of buffer must be 33 (uncompressed) or 34 (compressed)');
|
||||||
|
|
||||||
if (buf[0] === constants.mainnet.privkey)
|
if (buf[0] === constants.mainnet.privkey)
|
||||||
this.network = 'mainnet';
|
this.network = 'mainnet';
|
||||||
else if (buf[0] === constants.testnet.privkey)
|
else if (buf[0] === constants.testnet.privkey)
|
||||||
this.network = 'testnet';
|
this.network = 'testnet';
|
||||||
else
|
else
|
||||||
throw new Error('privkey: Invalid network');
|
throw new Error('Invalid network');
|
||||||
|
|
||||||
this.bn = Bn.fromBuffer(buf.slice(1, 32 + 1));
|
this.bn = Bn.fromBuffer(buf.slice(1, 32 + 1));
|
||||||
};
|
};
|
||||||
|
|||||||
@ -5,7 +5,7 @@ var Pubkey = function Pubkey(point) {
|
|||||||
if (!(this instanceof Pubkey))
|
if (!(this instanceof Pubkey))
|
||||||
return new Pubkey(point);
|
return new Pubkey(point);
|
||||||
if (point && !point.getX() && !point.getY())
|
if (point && !point.getX() && !point.getY())
|
||||||
throw new Error('pubkey: Invalid point');
|
throw new Error('Invalid point');
|
||||||
this.point = point;
|
this.point = point;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ Pubkey.prototype.fromDER = function(buf) {
|
|||||||
var xbuf = buf.slice(1, 33);
|
var xbuf = buf.slice(1, 33);
|
||||||
var ybuf = buf.slice(33, 65);
|
var ybuf = buf.slice(33, 65);
|
||||||
if (xbuf.length !== 32 || ybuf.length !== 32 || buf.length !== 65)
|
if (xbuf.length !== 32 || ybuf.length !== 32 || buf.length !== 65)
|
||||||
throw new Error('pubkey: Length of x and y must be 32 bytes');
|
throw new Error('Length of x and y must be 32 bytes');
|
||||||
var x = bn(xbuf);
|
var x = bn(xbuf);
|
||||||
var y = bn(ybuf);
|
var y = bn(ybuf);
|
||||||
this.point = Point(x, y);
|
this.point = Point(x, y);
|
||||||
@ -27,7 +27,7 @@ Pubkey.prototype.fromDER = function(buf) {
|
|||||||
var x = bn(xbuf);
|
var x = bn(xbuf);
|
||||||
this.fromX(false, x);
|
this.fromX(false, x);
|
||||||
} else {
|
} else {
|
||||||
throw new Error('pubkey: Invalid DER format pubkey');
|
throw new Error('Invalid DER format pubkey');
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
@ -38,7 +38,7 @@ Pubkey.prototype.fromString = function(str) {
|
|||||||
|
|
||||||
Pubkey.prototype.fromX = function(odd, x) {
|
Pubkey.prototype.fromX = function(odd, x) {
|
||||||
if (typeof odd !== 'boolean')
|
if (typeof odd !== 'boolean')
|
||||||
throw new Error('pubkey: Must specify whether y is odd or not (true or false)');
|
throw new Error('Must specify whether y is odd or not (true or false)');
|
||||||
this.point = Point.fromX(odd, x);
|
this.point = Point.fromX(odd, x);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ Pubkey.prototype.toBuffer = function() {
|
|||||||
|
|
||||||
Pubkey.prototype.toDER = function(compressed) {
|
Pubkey.prototype.toDER = function(compressed) {
|
||||||
if (typeof compressed !== 'boolean')
|
if (typeof compressed !== 'boolean')
|
||||||
throw new Error('pubkey: Must specify whether the public key is compressed or not (true or false)');
|
throw new Error('Must specify whether the public key is compressed or not (true or false)');
|
||||||
|
|
||||||
var x = this.point.getX();
|
var x = this.point.getX();
|
||||||
var y = this.point.getY();
|
var y = this.point.getY();
|
||||||
|
|||||||
@ -16,14 +16,14 @@ Random.getRandomBufferNode = function(size) {
|
|||||||
|
|
||||||
Random.getRandomBufferBrowser = function(size) {
|
Random.getRandomBufferBrowser = function(size) {
|
||||||
if (!window.crypto && !window.msCrypto)
|
if (!window.crypto && !window.msCrypto)
|
||||||
throw new Error('random: window.crypto not available');
|
throw new Error('window.crypto not available');
|
||||||
|
|
||||||
if (window.crypto && window.crypto.getRandomValues)
|
if (window.crypto && window.crypto.getRandomValues)
|
||||||
var crypto = window.crypto;
|
var crypto = window.crypto;
|
||||||
else if (window.msCrypto && window.msCrypto.getRandomValues) //internet explorer
|
else if (window.msCrypto && window.msCrypto.getRandomValues) //internet explorer
|
||||||
var crypto = window.msCrypto;
|
var crypto = window.msCrypto;
|
||||||
else
|
else
|
||||||
throw new Error('random: window.crypto.getRandomValues not available');
|
throw new Error('window.crypto.getRandomValues not available');
|
||||||
|
|
||||||
var bbuf = new Uint8Array(size);
|
var bbuf = new Uint8Array(size);
|
||||||
crypto.getRandomValues(bbuf);
|
crypto.getRandomValues(bbuf);
|
||||||
|
|||||||
@ -17,11 +17,11 @@ Signature.prototype.fromCompressed = function(buf) {
|
|||||||
var b3 = buf.slice(33, 65);
|
var b3 = buf.slice(33, 65);
|
||||||
|
|
||||||
if (!(i === 0 || i === 1 || i === 2 || i === 3))
|
if (!(i === 0 || i === 1 || i === 2 || i === 3))
|
||||||
throw new Error('signature: i must be 0, 1, 2, or 3');
|
throw new Error('i must be 0, 1, 2, or 3');
|
||||||
if (b2.length !== 32)
|
if (b2.length !== 32)
|
||||||
throw new Error('signature: r must be 32 bytes');
|
throw new Error('r must be 32 bytes');
|
||||||
if (b3.length !== 32)
|
if (b3.length !== 32)
|
||||||
throw new Error('signature: s must be 32 bytes');
|
throw new Error('s must be 32 bytes');
|
||||||
|
|
||||||
this.i = i;
|
this.i = i;
|
||||||
this.r = BN().fromBuffer(b2);
|
this.r = BN().fromBuffer(b2);
|
||||||
@ -41,42 +41,42 @@ Signature.prototype.fromString = function(str) {
|
|||||||
|
|
||||||
Signature.parseDER = function(buf) {
|
Signature.parseDER = function(buf) {
|
||||||
if (!Buffer.isBuffer(buf))
|
if (!Buffer.isBuffer(buf))
|
||||||
throw new Error('signature: DER formatted signature should be a buffer');
|
throw new Error('DER formatted signature should be a buffer');
|
||||||
|
|
||||||
var header = buf[0];
|
var header = buf[0];
|
||||||
|
|
||||||
if (header !== 0x30)
|
if (header !== 0x30)
|
||||||
throw new Error('signature: Header byte should be 0x30');
|
throw new Error('Header byte should be 0x30');
|
||||||
|
|
||||||
var length = buf[1];
|
var length = buf[1];
|
||||||
if (length !== buf.slice(2).length)
|
if (length !== buf.slice(2).length)
|
||||||
throw new Error('signature: Length byte should length of what follows');
|
throw new Error('Length byte should length of what follows');
|
||||||
|
|
||||||
var rheader = buf[2 + 0];
|
var rheader = buf[2 + 0];
|
||||||
if (rheader !== 0x02)
|
if (rheader !== 0x02)
|
||||||
throw new Error('signature: Integer byte for r should be 0x02');
|
throw new Error('Integer byte for r should be 0x02');
|
||||||
|
|
||||||
var rlength = buf[2 + 1];
|
var rlength = buf[2 + 1];
|
||||||
var rbuf = buf.slice(2 + 2, 2 + 2 + rlength);
|
var rbuf = buf.slice(2 + 2, 2 + 2 + rlength);
|
||||||
var r = BN().fromBuffer(rbuf);
|
var r = BN().fromBuffer(rbuf);
|
||||||
var rneg = buf[2 + 1 + 1] === 0x00 ? true : false;
|
var rneg = buf[2 + 1 + 1] === 0x00 ? true : false;
|
||||||
if (rlength !== rbuf.length)
|
if (rlength !== rbuf.length)
|
||||||
throw new Error('signature: Length of r incorrect');
|
throw new Error('Length of r incorrect');
|
||||||
|
|
||||||
var sheader = buf[2 + 2 + rlength + 0];
|
var sheader = buf[2 + 2 + rlength + 0];
|
||||||
if (sheader !== 0x02)
|
if (sheader !== 0x02)
|
||||||
throw new Error('signature: Integer byte for s should be 0x02');
|
throw new Error('Integer byte for s should be 0x02');
|
||||||
|
|
||||||
var slength = buf[2 + 2 + rlength + 1];
|
var slength = buf[2 + 2 + rlength + 1];
|
||||||
var sbuf = buf.slice(2 + 2 + rlength + 2, 2 + 2 + rlength + 2 + slength);
|
var sbuf = buf.slice(2 + 2 + rlength + 2, 2 + 2 + rlength + 2 + slength);
|
||||||
var s = BN().fromBuffer(sbuf);
|
var s = BN().fromBuffer(sbuf);
|
||||||
var sneg = buf[2 + 2 + rlength + 2 + 2] === 0x00 ? true : false;
|
var sneg = buf[2 + 2 + rlength + 2 + 2] === 0x00 ? true : false;
|
||||||
if (slength !== sbuf.length)
|
if (slength !== sbuf.length)
|
||||||
throw new Error('signature: Length of s incorrect');
|
throw new Error('Length of s incorrect');
|
||||||
|
|
||||||
var sumlength = 2 + 2 + rlength + 2 + slength;
|
var sumlength = 2 + 2 + rlength + 2 + slength;
|
||||||
if (length !== sumlength - 2)
|
if (length !== sumlength - 2)
|
||||||
throw new Error('signature: Length of signature incorrect');
|
throw new Error('Length of signature incorrect');
|
||||||
|
|
||||||
var obj = {
|
var obj = {
|
||||||
header: header,
|
header: header,
|
||||||
@ -99,7 +99,7 @@ Signature.parseDER = function(buf) {
|
|||||||
Signature.prototype.toCompressed = function(i) {
|
Signature.prototype.toCompressed = function(i) {
|
||||||
i = typeof i === 'number' ? i : this.i;
|
i = typeof i === 'number' ? i : this.i;
|
||||||
if (!(i === 0 || i === 1 || i === 2 || i === 3))
|
if (!(i === 0 || i === 1 || i === 2 || i === 3))
|
||||||
throw new Error('signature: i must be equal to 0, 1, 2, or 3');
|
throw new Error('i must be equal to 0, 1, 2, or 3');
|
||||||
|
|
||||||
var b1 = new Buffer([i]);
|
var b1 = new Buffer([i]);
|
||||||
var b2 = this.r.toBuffer({size: 32});
|
var b2 = this.r.toBuffer({size: 32});
|
||||||
|
|||||||
@ -126,7 +126,7 @@ describe('Address', function() {
|
|||||||
address.network = 'unknown';
|
address.network = 'unknown';
|
||||||
(function() {
|
(function() {
|
||||||
address.validate();
|
address.validate();
|
||||||
}).should.throw('address: network must be "mainnet" or "testnet"');
|
}).should.throw('network must be "mainnet" or "testnet"');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an error on this invalid type', function() {
|
it('should throw an error on this invalid type', function() {
|
||||||
@ -135,7 +135,7 @@ describe('Address', function() {
|
|||||||
address.type = 'unknown';
|
address.type = 'unknown';
|
||||||
(function() {
|
(function() {
|
||||||
address.validate();
|
address.validate();
|
||||||
}).should.throw('address: type must be "pubkeyhash" or "p2sh"');
|
}).should.throw('type must be "pubkeyhash" or "p2sh"');
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@ -25,7 +25,7 @@ describe('Base58Check', function() {
|
|||||||
it('should throw an error when the input is not a buffer', function() {
|
it('should throw an error when the input is not a buffer', function() {
|
||||||
(function() {
|
(function() {
|
||||||
Base58Check.encode("string")
|
Base58Check.encode("string")
|
||||||
}).should.throw('Base58Check: Input must be a buffer');
|
}).should.throw('Input must be a buffer');
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
@ -39,13 +39,13 @@ describe('Base58Check', function() {
|
|||||||
it('should throw an error when input is not a string', function() {
|
it('should throw an error when input is not a string', function() {
|
||||||
(function() {
|
(function() {
|
||||||
Base58Check.decode(5);
|
Base58Check.decode(5);
|
||||||
}).should.throw('Base58Check: Input must be a string');
|
}).should.throw('Input must be a string');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an error when input is too short', function() {
|
it('should throw an error when input is too short', function() {
|
||||||
(function() {
|
(function() {
|
||||||
Base58Check.decode(enc.slice(0, 1));
|
Base58Check.decode(enc.slice(0, 1));
|
||||||
}).should.throw('Base58Check: Input string too short');
|
}).should.throw('Input string too short');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an error when there is a checksum mismatch', function() {
|
it('should throw an error when there is a checksum mismatch', function() {
|
||||||
@ -54,7 +54,7 @@ describe('Base58Check', function() {
|
|||||||
var enc2 = base58.encode(buf2);
|
var enc2 = base58.encode(buf2);
|
||||||
(function() {
|
(function() {
|
||||||
Base58Check.decode(enc2);
|
Base58Check.decode(enc2);
|
||||||
}).should.throw('Base58Check: Checksum mismatch');
|
}).should.throw('Checksum mismatch');
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@ -109,7 +109,7 @@ describe('Point', function() {
|
|||||||
var p = point(x, y);
|
var p = point(x, y);
|
||||||
(function() {
|
(function() {
|
||||||
p.validate();
|
p.validate();
|
||||||
}).should.throw('point: Invalid y value of public key');
|
}).should.throw('Invalid y value of public key');
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@ -119,7 +119,7 @@ describe('Pubkey', function() {
|
|||||||
pk.fromString(hex);
|
pk.fromString(hex);
|
||||||
(function() {
|
(function() {
|
||||||
pk.validate();
|
pk.validate();
|
||||||
}).should.throw('point: Invalid y value of public key');
|
}).should.throw('Invalid y value of public key');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not throw an error if pubkey is infinity', function() {
|
it('should not throw an error if pubkey is infinity', function() {
|
||||||
@ -127,7 +127,7 @@ describe('Pubkey', function() {
|
|||||||
pk.point = Point.getG().mul(Point.getN());
|
pk.point = Point.getG().mul(Point.getN());
|
||||||
(function() {
|
(function() {
|
||||||
pk.validate();
|
pk.validate();
|
||||||
}).should.throw('point: Point cannot be equal to Infinity');
|
}).should.throw('Point cannot be equal to Infinity');
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user