You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.4 KiB
53 lines
1.4 KiB
"use strict";
|
|
|
|
var imports = require('soop').imports();
|
|
var Key = imports.Key || require('../Key');
|
|
var bignum = imports.bignum || require('bignum');
|
|
var assert = require('assert');
|
|
|
|
//a point on the secp256k1 curve
|
|
//x and y are bignums
|
|
var Point = function(x, y) {
|
|
this.x = x;
|
|
this.y = y;
|
|
};
|
|
|
|
Point.add = function(p1, p2) {
|
|
var key1 = p1.toKey();
|
|
key1.compressed = false;
|
|
var key2 = p2.toKey();
|
|
key2.compressed = false;
|
|
var pubKey = Key.addUncompressed(key1.public, key2.public);
|
|
var key = new Key();
|
|
key.compressed = false;
|
|
key.public = pubKey;
|
|
key.compressed = true;
|
|
return Point.fromKey(key);
|
|
};
|
|
|
|
//convert the public key of a Key into a Point
|
|
Point.fromKey = function(key) {
|
|
var point = new Point();
|
|
var pubKeyBuf = new Buffer(key.public);
|
|
var key2 = new Key();
|
|
key2.compressed = key.compressed;
|
|
key2.public = pubKeyBuf;
|
|
key2.compressed = false;
|
|
point.x = bignum.fromBuffer(key2.public.slice(1, 33), {size: 32});
|
|
point.y = bignum.fromBuffer(key2.public.slice(33, 65), {size: 32});
|
|
return point;
|
|
};
|
|
|
|
//convert the Point into the Key containing a compressed public key
|
|
Point.prototype.toKey = function() {
|
|
var xbuf = this.x.toBuffer({size: 32});
|
|
var ybuf = this.y.toBuffer({size: 32});
|
|
var key = new Key();
|
|
key.compressed = false;
|
|
var prefix = new Buffer([0x04]);
|
|
key.public = Buffer.concat([prefix, xbuf, ybuf]); //this might be wrong
|
|
key.compressed = true;
|
|
return key;
|
|
};
|
|
|
|
module.exports = require('soop')(Point);
|
|
|