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 KeyPair = function ()
{
this._curve = sjcl.ecc.curves['c256'];
function KeyPair() {
this._curve = sjcl.ecc.curves['c256'];
this._secret = null;
this._pubkey = null;
};
KeyPair.from_bn_secret = function (j)
{
if (j instanceof this) {
return j.clone();
} else {
return (new this()).parse_bn_secret(j);
}
KeyPair.from_bn_secret = function (j) {
return j instanceof this ? j.clone() : (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);
return this;
};
@@ -29,8 +22,7 @@ KeyPair.prototype.parse_bn_secret = function (j)
*
* @private
*/
KeyPair.prototype._pub = function ()
{
KeyPair.prototype._pub = function () {
var curve = this._curve;
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.
*/
KeyPair.prototype.to_hex_pub = function ()
{
KeyPair.prototype.to_hex_pub = function () {
var pub = this._pub();
if (!pub) return null;
if (!pub) {
return null;
}
var point = pub._point, y_even = point.y.mod(2).equals(0);
return sjcl.codec.hex.fromBits(sjcl.bitArray.concat(
[sjcl.bitArray.partial(8, y_even ? 0x02 : 0x03)],
point.x.toBits(this._curve.r.bitLength())
)).toUpperCase();
};
KeyPair.prototype.sign = function (hash)
{
hash = UInt256.from_json(hash);
KeyPair.prototype.sign = function (hash) {
var hash = UInt256.from_json(hash);
return this._secret.signDER(hash.to_bits(), 0);
};

View File

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