This commit is contained in:
wltsmrz
2013-09-09 14:42:07 -07:00
parent dad760f9ba
commit 8cab1ae200
2 changed files with 63 additions and 53 deletions

View File

@@ -2,24 +2,17 @@ var sjcl = require('../../../build/sjcl');
var UInt256 = require('./uint256').UInt256; var UInt256 = require('./uint256').UInt256;
var KeyPair = function () function KeyPair() {
{ this._curve = sjcl.ecc.curves['c256'];
this._curve = sjcl.ecc.curves['c256'];
this._secret = null; this._secret = null;
this._pubkey = null; this._pubkey = null;
}; };
KeyPair.from_bn_secret = function (j) KeyPair.from_bn_secret = function (j) {
{ return j instanceof this ? j.clone() : (new this()).parse_bn_secret(j);
if (j instanceof this) {
return j.clone();
} else {
return (new this()).parse_bn_secret(j);
}
}; };
KeyPair.prototype.parse_bn_secret = function (j) KeyPair.prototype.parse_bn_secret = function (j) {
{
this._secret = new sjcl.ecc.ecdsa.secretKey(sjcl.ecc.curves['c256'], j); this._secret = new sjcl.ecc.ecdsa.secretKey(sjcl.ecc.curves['c256'], j);
return this; return this;
}; };
@@ -29,8 +22,7 @@ KeyPair.prototype.parse_bn_secret = function (j)
* *
* @private * @private
*/ */
KeyPair.prototype._pub = function () KeyPair.prototype._pub = function () {
{
var curve = this._curve; var curve = this._curve;
if (!this._pubkey && this._secret) { if (!this._pubkey && this._secret) {
@@ -46,21 +38,23 @@ KeyPair.prototype._pub = function ()
* *
* Key will be returned as a compressed pubkey - 33 bytes converted to hex. * Key will be returned as a compressed pubkey - 33 bytes converted to hex.
*/ */
KeyPair.prototype.to_hex_pub = function () KeyPair.prototype.to_hex_pub = function () {
{
var pub = this._pub(); var pub = this._pub();
if (!pub) return null;
if (!pub) {
return null;
}
var point = pub._point, y_even = point.y.mod(2).equals(0); var point = pub._point, y_even = point.y.mod(2).equals(0);
return sjcl.codec.hex.fromBits(sjcl.bitArray.concat( return sjcl.codec.hex.fromBits(sjcl.bitArray.concat(
[sjcl.bitArray.partial(8, y_even ? 0x02 : 0x03)], [sjcl.bitArray.partial(8, y_even ? 0x02 : 0x03)],
point.x.toBits(this._curve.r.bitLength()) point.x.toBits(this._curve.r.bitLength())
)).toUpperCase(); )).toUpperCase();
}; };
KeyPair.prototype.sign = function (hash) KeyPair.prototype.sign = function (hash) {
{ var hash = UInt256.from_json(hash);
hash = UInt256.from_json(hash);
return this._secret.signDER(hash.to_bits(), 0); return this._secret.signDER(hash.to_bits(), 0);
}; };

View File

@@ -1,30 +1,40 @@
var extend = require('extend'); var extend = require('extend');
var utils = require('./utils'); var utils = require('./utils');
var UInt160 = require('./uint160').UInt160; var UInt160 = require('./uint160').UInt160;
var Amount = require('./amount').Amount; var Amount = require('./amount').Amount;
/** /**
* Meta data processing facility. * Meta data processing facility.
*/ */
var Meta = function (raw_data) function Meta(raw_data) {
{ var self = this;
this.nodes = [];
for (var i = 0, l = raw_data.AffectedNodes.length; i < l; i++) { this.nodes = [ ];
var an = raw_data.AffectedNodes[i],
result = {};
["CreatedNode", "ModifiedNode", "DeletedNode"].forEach(function (x) { this.node_types = [
if (an[x]) result.diffType = x; 'CreatedNode'
, 'ModifiedNode'
, 'DeletedNode'
];
for (var i=0, l=raw_data.AffectedNodes.length; i<l; i++) {
var an = raw_data.AffectedNodes[i];
var result = { };
self.node_types.forEach(function (x) {
if (an.hasOwnProperty(x)) {
result.diffType = x;
}
}); });
if (!result.diffType) return null; if (!result.diffType) {
return null;
}
an = an[result.diffType]; an = an[result.diffType];
result.entryType = an.LedgerEntryType; result.entryType = an.LedgerEntryType;
result.ledgerIndex = an.LedgerIndex; result.ledgerIndex = an.LedgerIndex;
result.fields = extend({}, an.PreviousFields, an.NewFields, an.FinalFields); result.fields = extend({}, an.PreviousFields, an.NewFields, an.FinalFields);
result.fieldsPrev = an.PreviousFields || {}; result.fieldsPrev = an.PreviousFields || {};
result.fieldsNew = an.NewFields || {}; result.fieldsNew = an.NewFields || {};
@@ -69,29 +79,40 @@ var Meta = function (raw_data)
* The second parameter to the callback is the index of the node in the metadata * The second parameter to the callback is the index of the node in the metadata
* (first entry is index 0). * (first entry is index 0).
*/ */
Meta.prototype.each = function (fn) Meta.prototype.each = function (fn) {
{
for (var i = 0, l = this.nodes.length; i < l; i++) { for (var i = 0, l = this.nodes.length; i < l; i++) {
fn(this.nodes[i], i); fn(this.nodes[i], i);
} }
}; };
([ 'forEach'
, 'map'
, 'filter'
, 'every'
, 'reduce'
]).forEach(function(fn) {
Meta.prototype[fn] = function() {
return Array.prototype[fn].apply(this.nodes, arguments);
}
});
var amountFieldsAffectingIssuer = [ var amountFieldsAffectingIssuer = [
"LowLimit", "HighLimit", "TakerPays", "TakerGets" "LowLimit"
, "HighLimit"
, "TakerPays"
, "TakerGets"
]; ];
Meta.prototype.getAffectedAccounts = function ()
{ Meta.prototype.getAffectedAccounts = function () {
var accounts = []; var accounts = [ ];
// This code should match the behavior of the C++ method: // This code should match the behavior of the C++ method:
// TransactionMetaSet::getAffectedAccounts // TransactionMetaSet::getAffectedAccounts
this.each(function (an) { this.nodes.forEach(function (an) {
var fields = (an.diffType === "CreatedNode") ? an.fieldsNew : an.fieldsFinal; var fields = (an.diffType === "CreatedNode") ? an.fieldsNew : an.fieldsFinal;
for (var i in fields) { for (var i in fields) {
var field = fields[i]; var field = fields[i];
if (typeof field === 'string' && UInt160.is_valid(field)) {
if ("string" === typeof field && UInt160.is_valid(field)) {
accounts.push(field); accounts.push(field);
} else if (amountFieldsAffectingIssuer.indexOf(i) !== -1) { } else if (amountFieldsAffectingIssuer.indexOf(i) !== -1) {
var amount = Amount.from_json(field); var amount = Amount.from_json(field);
@@ -103,16 +124,13 @@ Meta.prototype.getAffectedAccounts = function ()
} }
}); });
accounts = utils.arrayUnique(accounts); return utils.arrayUnique(accounts);
return accounts;
}; };
Meta.prototype.getAffectedBooks = function () Meta.prototype.getAffectedBooks = function () {
{ var books = [ ];
var books = [];
this.each(function (an) { this.nodes.forEach(function (an) {
if (an.entryType !== 'Offer') return; if (an.entryType !== 'Offer') return;
var gets = Amount.from_json(an.fields.TakerGets); var gets = Amount.from_json(an.fields.TakerGets);
@@ -129,9 +147,7 @@ Meta.prototype.getAffectedBooks = function ()
books.push(key); books.push(key);
}); });
books = utils.arrayUnique(books); return utils.arrayUnique(books);
return books;
}; };
exports.Meta = Meta; exports.Meta = Meta;