From f6aa01c44572ffd22245ad7cc6b7a39d77be0a2c Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Fri, 28 Mar 2014 18:07:23 -0400 Subject: [PATCH] add basic tests for all functions in Point --- bitcore.js | 1 + browser/build.js | 1 + test/test.Point.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 test/test.Point.js diff --git a/bitcore.js b/bitcore.js index 548b3d9..2831359 100644 --- a/bitcore.js +++ b/bitcore.js @@ -24,6 +24,7 @@ requireWhenAccessed('VersionedData', './util/VersionedData'); requireWhenAccessed('BinaryParser', './util/BinaryParser'); requireWhenAccessed('Address', './Address'); requireWhenAccessed('BIP32', './BIP32'); +requireWhenAccessed('Point', './Point'); requireWhenAccessed('Opcode', './Opcode'); requireWhenAccessed('Script', './Script'); requireWhenAccessed('Transaction', './Transaction'); diff --git a/browser/build.js b/browser/build.js index 24efa9b..3969418 100644 --- a/browser/build.js +++ b/browser/build.js @@ -38,6 +38,7 @@ var modules = [ 'PrivateKey', 'RpcClient', 'Key', + 'Point', 'SIN', 'SINKey', 'Script', diff --git a/test/test.Point.js b/test/test.Point.js new file mode 100644 index 0000000..f7e3163 --- /dev/null +++ b/test/test.Point.js @@ -0,0 +1,69 @@ +'use strict'; + +var assert = require('assert'); +var chai = chai || require('chai'); +var bitcore = bitcore || require('../bitcore'); +var coinUtil = coinUtil || require('../util/util'); +var buffertools = require('buffertools'); +var bignum = require('bignum'); + +var should = chai.should(); + +var Point = bitcore.Point; +var Key = bitcore.Key; + +describe('Key', function() { + + it('should initialize the main object', function() { + should.exist(Point); + }); + + it('should be able to create instance', function() { + var p = new Point(); + should.exist(p); + }); + + it('should add these two points correctly', function() { + //these points are from one of the BIP32 test vectors + var axhex = "69b154b42ff9452c31251cb341d7db01ad603dc56d64f9c5fb9e7031b89a241d"; + var ayhex = "eeedc91342b3c8982c1e676435780fe5f9d62f3f692e8d1512485d77fab35997"; + var a = new Point(bignum.fromBuffer((new Buffer(axhex, 'hex')), {size: 32}), bignum.fromBuffer((new Buffer(ayhex, 'hex')), {size: 32})); + var bxhex = "5a784662a4a20a65bf6aab9ae98a6c068a81c52e4b032c0fb5400c706cfccc56"; + var byhex = "7f717885be239daadce76b568958305183ad616ff74ed4dc219a74c26d35f839"; + var b = new Point(bignum.fromBuffer((new Buffer(bxhex, 'hex')), {size: 32}), bignum.fromBuffer((new Buffer(byhex, 'hex')), {size: 32})); + var sxhex = "501e454bf00751f24b1b489aa925215d66af2234e3891c3b21a52bedb3cd711c"; + var syhex = "008794c1df8131b9ad1e1359965b3f3ee2feef0866be693729772be14be881ab"; + var s = new Point(bignum.fromBuffer((new Buffer(sxhex, 'hex')), {size: 32}), bignum.fromBuffer((new Buffer(syhex, 'hex')), {size: 32})); + var sum = Point.add(a, b); + s.x.toBuffer({size: 32}).toString('hex').should.equal(sum.x.toBuffer({size: 32}).toString('hex')); + s.y.toBuffer({size: 32}).toString('hex').should.equal(sum.y.toBuffer({size: 32}).toString('hex')); + }); + + it('should convert a Point into the public key of a Key', function() { + var axhex = "69b154b42ff9452c31251cb341d7db01ad603dc56d64f9c5fb9e7031b89a241d"; + var axbuf = new Buffer(axhex, 'hex'); + var ayhex = "eeedc91342b3c8982c1e676435780fe5f9d62f3f692e8d1512485d77fab35997"; + var aybuf = new Buffer(ayhex, 'hex'); + var a = new Point(bignum.fromBuffer(axbuf, {size: 32}), bignum.fromBuffer(aybuf, {size: 32})); + + var pubKeyBufCompressedHex = "0369b154b42ff9452c31251cb341d7db01ad603dc56d64f9c5fb9e7031b89a241d"; + var key = new Key(); + key.public = new Buffer(pubKeyBufCompressedHex, 'hex'); + + key.public.toString('hex').should.equal(a.toKey().public.toString('hex')); + }); + + it('should convert the public key of a Key into a Point', function() { + var axhex = "69b154b42ff9452c31251cb341d7db01ad603dc56d64f9c5fb9e7031b89a241d"; + var ayhex = "eeedc91342b3c8982c1e676435780fe5f9d62f3f692e8d1512485d77fab35997"; + + var pubKeyBufCompressedHex = "0369b154b42ff9452c31251cb341d7db01ad603dc56d64f9c5fb9e7031b89a241d"; + var key = new Key(); + key.public = new Buffer(pubKeyBufCompressedHex, 'hex'); + + var point = Point.fromKey(key); + point.x.toBuffer({size: 32}).toString('hex').should.equal(axhex); + point.y.toBuffer({size: 32}).toString('hex').should.equal(ayhex); + }); + +});