more json improvements.
This commit is contained in:
parent
003330e678
commit
e5de8db660
@ -22,8 +22,10 @@ function Block(data, subtype) {
|
|||||||
if (!(this instanceof Block))
|
if (!(this instanceof Block))
|
||||||
return new Block(data, subtype);
|
return new Block(data, subtype);
|
||||||
|
|
||||||
|
assert(typeof data.hash !== 'string');
|
||||||
|
|
||||||
this.type = 'block';
|
this.type = 'block';
|
||||||
this.subtype = subtype;
|
this.subtype = subtype || data.subtype;
|
||||||
this.version = data.version;
|
this.version = data.version;
|
||||||
this.prevBlock = utils.toHex(data.prevBlock);
|
this.prevBlock = utils.toHex(data.prevBlock);
|
||||||
this.merkleRoot = utils.toHex(data.merkleRoot);
|
this.merkleRoot = utils.toHex(data.merkleRoot);
|
||||||
@ -480,6 +482,51 @@ Block.prototype.inspect = function inspect() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Block.prototype.toJSON = function toJSON() {
|
Block.prototype.toJSON = function toJSON() {
|
||||||
|
var json = {
|
||||||
|
type: 'block',
|
||||||
|
subtype: this.subtype,
|
||||||
|
height: this.height,
|
||||||
|
network: this.network,
|
||||||
|
relayedBy: this.relayedBy,
|
||||||
|
hash: utils.revHex(this.hash('hex')),
|
||||||
|
version: this.version,
|
||||||
|
prevBlock: utils.revHex(this.prevBlock),
|
||||||
|
merkleRoot: utils.revHex(this.merkleRoot),
|
||||||
|
ts: this.ts,
|
||||||
|
bits: this.bits,
|
||||||
|
nonce: this.nonce,
|
||||||
|
totalTX: this.totalTX,
|
||||||
|
txs: this.txs.map(function(tx) {
|
||||||
|
return tx.toJSON();
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
if (this.subtype === 'headers')
|
||||||
|
return json;
|
||||||
|
|
||||||
|
if (this.subtype === 'merkleblock') {
|
||||||
|
json.hashes = this.hashes;
|
||||||
|
json.flags = this.flags;
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
return json;
|
||||||
|
};
|
||||||
|
|
||||||
|
Block._fromJSON = function _fromJSON(json) {
|
||||||
|
json.prevBlock = utils.revHex(json.prevBlock);
|
||||||
|
json.merkleRoot = utils.revHex(json.merkleRoot);
|
||||||
|
json.txs = json.txs.map(function(tx) {
|
||||||
|
return bcoin.tx._fromJSON(tx);
|
||||||
|
});
|
||||||
|
return json;
|
||||||
|
};
|
||||||
|
|
||||||
|
Block.fromJSON = function fromJSON(json) {
|
||||||
|
return new Block(Block._fromJSON(json), json.subtype);
|
||||||
|
};
|
||||||
|
|
||||||
|
Block.prototype.toCompact = function toCompact() {
|
||||||
return {
|
return {
|
||||||
type: 'block',
|
type: 'block',
|
||||||
subtype: this.subtype,
|
subtype: this.subtype,
|
||||||
@ -493,8 +540,8 @@ Block.prototype.toJSON = function toJSON() {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
Block.fromJSON = function fromJSON(json) {
|
Block._fromCompact = function _fromCompact(json) {
|
||||||
var raw, parser, data, block;
|
var raw, parser, data;
|
||||||
|
|
||||||
assert.equal(json.type, 'block');
|
assert.equal(json.type, 'block');
|
||||||
|
|
||||||
@ -502,53 +549,22 @@ Block.fromJSON = function fromJSON(json) {
|
|||||||
|
|
||||||
parser = new bcoin.protocol.parser();
|
parser = new bcoin.protocol.parser();
|
||||||
|
|
||||||
if (json.subtype === 'merkleblock')
|
if (json.subtype === 'headers')
|
||||||
|
data = parser.parseHeaders(raw);
|
||||||
|
else if (json.subtype === 'merkleblock')
|
||||||
data = parser.parseMerkleBlock(raw);
|
data = parser.parseMerkleBlock(raw);
|
||||||
else if (json.subtype === 'block' || json.subtype === 'header')
|
else
|
||||||
data = parser.parseBlock(raw);
|
data = parser.parseBlock(raw);
|
||||||
|
|
||||||
data.height = json.height;
|
data.height = json.height;
|
||||||
data.network = json.network;
|
data.network = json.network;
|
||||||
data.relayedBy = json.relayedBy;
|
data.relayedBy = json.relayedBy;
|
||||||
|
|
||||||
block = new Block(data, json.subtype);
|
return data;
|
||||||
|
|
||||||
block._hash = json.hash;
|
|
||||||
|
|
||||||
return block;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Block.prototype.toFullJSON = function toFullJSON() {
|
Block.fromCompact = function fromCompact(json) {
|
||||||
return {
|
return new Block(Block._fromCompact(json), json.subtype);
|
||||||
type: 'block',
|
|
||||||
subtype: this.subtype,
|
|
||||||
height: this.height,
|
|
||||||
network: this.network,
|
|
||||||
relayedBy: this.relayedBy,
|
|
||||||
hash: utils.revHex(this.hash('hex')),
|
|
||||||
version: this.version,
|
|
||||||
prevBlock: utils.revHex(this.prevBlock),
|
|
||||||
merkleRoot: utils.revHex(this.merkleRoot),
|
|
||||||
ts: this.ts,
|
|
||||||
bits: this.bits,
|
|
||||||
nonce: this.nonce,
|
|
||||||
txs: this.txs.map(function(tx) {
|
|
||||||
return tx.toFullJSON();
|
|
||||||
})
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
Block.fromFullJSON = function fromFullJSON(json) {
|
|
||||||
json.prevBlock = utils.revHex(json.prevBlock);
|
|
||||||
json.merkleRoot = utils.revHex(json.merkleRoot);
|
|
||||||
json.txs = json.txs.map(function(tx) {
|
|
||||||
tx = bcoin.tx.fromFullJSON(tx);
|
|
||||||
tx.ts = block.ts;
|
|
||||||
tx.block = block.hash('hex');
|
|
||||||
tx.height = block.height;
|
|
||||||
return tx;
|
|
||||||
});
|
|
||||||
return new Block(json, json.subtype);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Block.prototype.toRaw = function toRaw(enc) {
|
Block.prototype.toRaw = function toRaw(enc) {
|
||||||
@ -556,7 +572,7 @@ Block.prototype.toRaw = function toRaw(enc) {
|
|||||||
|
|
||||||
assert(this.subtype === 'block');
|
assert(this.subtype === 'block');
|
||||||
|
|
||||||
data = new Buffer(this.render());
|
data = this.render();
|
||||||
|
|
||||||
if (enc === 'hex')
|
if (enc === 'hex')
|
||||||
data = utils.toHex(data);
|
data = utils.toHex(data);
|
||||||
@ -564,13 +580,17 @@ Block.prototype.toRaw = function toRaw(enc) {
|
|||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
Block.fromRaw = function fromRaw(data, enc) {
|
Block._fromRaw = function _fromRaw(data, enc) {
|
||||||
var parser = new bcoin.protocol.parser();
|
var parser = new bcoin.protocol.parser();
|
||||||
|
|
||||||
if (enc === 'hex')
|
if (enc === 'hex')
|
||||||
data = new Buffer(data, 'hex');
|
data = new Buffer(data, 'hex');
|
||||||
|
|
||||||
return new Block(parser.parseBlock(data), 'block');
|
return parser.parseBlock(data);
|
||||||
|
};
|
||||||
|
|
||||||
|
Block.fromRaw = function fromRaw(data, enc) {
|
||||||
|
return new Block(Block._fromRaw(data), 'block');
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -40,6 +40,7 @@ function Coin(tx, index) {
|
|||||||
this.spent = false;
|
this.spent = false;
|
||||||
} else {
|
} else {
|
||||||
options = tx;
|
options = tx;
|
||||||
|
assert(typeof options.script !== 'string');
|
||||||
this.version = options.version;
|
this.version = options.version;
|
||||||
this.height = options.height;
|
this.height = options.height;
|
||||||
this.value = options.value;
|
this.value = options.value;
|
||||||
@ -119,32 +120,6 @@ Coin.prototype.__defineGetter__('age', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
Coin.prototype.toJSON = function toJSON() {
|
Coin.prototype.toJSON = function toJSON() {
|
||||||
return {
|
|
||||||
version: this.version,
|
|
||||||
height: this.height,
|
|
||||||
value: utils.btc(this.value),
|
|
||||||
script: utils.toHex(bcoin.script.encode(this.script)),
|
|
||||||
hash: this.hash,
|
|
||||||
index: this.index,
|
|
||||||
spent: this.spent,
|
|
||||||
address: this.getAddress()
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
Coin.fromJSON = function fromJSON(json) {
|
|
||||||
return new Coin({
|
|
||||||
version: json.version,
|
|
||||||
height: json.height,
|
|
||||||
value: utils.satoshi(json.value),
|
|
||||||
script: bcoin.script.decode(new Buffer(json.script, 'hex')),
|
|
||||||
hash: json.hash,
|
|
||||||
index: json.index,
|
|
||||||
spent: json.spent,
|
|
||||||
address: json.address
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Coin.prototype.toFullJSON = function toFullJSON() {
|
|
||||||
return {
|
return {
|
||||||
version: this.version,
|
version: this.version,
|
||||||
height: this.height,
|
height: this.height,
|
||||||
@ -152,22 +127,39 @@ Coin.prototype.toFullJSON = function toFullJSON() {
|
|||||||
script: utils.toHex(bcoin.script.encode(this.script)),
|
script: utils.toHex(bcoin.script.encode(this.script)),
|
||||||
hash: utils.revHex(this.hash),
|
hash: utils.revHex(this.hash),
|
||||||
index: this.index,
|
index: this.index,
|
||||||
spent: this.spent,
|
spent: this.spent
|
||||||
address: this.getAddress()
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
Coin.fromFullJSON = function fromFullJSON(json) {
|
Coin._fromJSON = function _fromJSON(json) {
|
||||||
return new Coin({
|
return {
|
||||||
version: json.version,
|
version: json.version,
|
||||||
height: json.height,
|
height: json.height,
|
||||||
value: utils.satoshi(json.value),
|
value: utils.satoshi(json.value),
|
||||||
script: bcoin.script.decode(new Buffer(json.script, 'hex')),
|
script: bcoin.script.decode(new Buffer(json.script, 'hex')),
|
||||||
hash: utils.revHex(json.hash),
|
hash: utils.revHex(json.hash),
|
||||||
index: json.index,
|
index: json.index,
|
||||||
spent: json.spent,
|
spent: json.spent
|
||||||
address: json.address
|
};
|
||||||
});
|
};
|
||||||
|
|
||||||
|
Coin.fromJSON = function fromJSON(json) {
|
||||||
|
return new Coin(Coin._fromJSON(json));
|
||||||
|
};
|
||||||
|
|
||||||
|
Coin.prototype.toCompact = function toCompact() {
|
||||||
|
return {
|
||||||
|
type: 'coin',
|
||||||
|
coin: this.toRaw('hex')
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
Coin._fromCompact = function _fromCompact(json) {
|
||||||
|
return Coin._fromRaw(json.coin, 'hex');
|
||||||
|
};
|
||||||
|
|
||||||
|
Coin.fromCompact = function fromCompact(json) {
|
||||||
|
return new Coin(Coin._fromCompact(json));
|
||||||
};
|
};
|
||||||
|
|
||||||
Coin.prototype.toRaw = function toRaw(enc) {
|
Coin.prototype.toRaw = function toRaw(enc) {
|
||||||
@ -179,13 +171,19 @@ Coin.prototype.toRaw = function toRaw(enc) {
|
|||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
Coin.fromRaw = function fromRaw(data, enc) {
|
Coin._fromRaw = function _fromRaw(data, enc) {
|
||||||
|
var parser = new bcoin.protocol.parser();
|
||||||
|
|
||||||
if (enc === 'hex')
|
if (enc === 'hex')
|
||||||
data = new Buffer(data, 'hex');
|
data = new Buffer(data, 'hex');
|
||||||
|
|
||||||
data = parser.parseCoin(data, true);
|
data = parser.parseCoin(data, true);
|
||||||
|
|
||||||
return new Coin(data);
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
Coin.fromRaw = function fromRaw(data, enc) {
|
||||||
|
return new Coin(Coin._fromRaw(data, enc));
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -871,7 +871,7 @@ HDPrivateKey.prototype.toJSON = function toJSON(passphrase) {
|
|||||||
return json;
|
return json;
|
||||||
};
|
};
|
||||||
|
|
||||||
HDPrivateKey.fromJSON = function fromJSON(json, passphrase) {
|
HDPrivateKey._fromJSON = function _fromJSON(json, passphrase) {
|
||||||
assert.equal(json.v, 1);
|
assert.equal(json.v, 1);
|
||||||
assert.equal(json.name, 'hdkey');
|
assert.equal(json.name, 'hdkey');
|
||||||
|
|
||||||
@ -879,23 +879,47 @@ HDPrivateKey.fromJSON = function fromJSON(json, passphrase) {
|
|||||||
throw new Error('Cannot decrypt address');
|
throw new Error('Cannot decrypt address');
|
||||||
|
|
||||||
if (json.mnemonic) {
|
if (json.mnemonic) {
|
||||||
return new HDPrivateKey({
|
return {
|
||||||
seed: new HDSeed({
|
seed: {
|
||||||
mnemonic: json.encrypted
|
mnemonic: json.encrypted
|
||||||
? utils.decrypt(json.mnemonic, passphrase)
|
? utils.decrypt(json.mnemonic, passphrase)
|
||||||
: json.mnemonic,
|
: json.mnemonic,
|
||||||
passphrase: json.encrypted
|
passphrase: json.encrypted
|
||||||
? utils.decrypt(json.passphrase, passphrase)
|
? utils.decrypt(json.passphrase, passphrase)
|
||||||
: json.passphrase
|
: json.passphrase
|
||||||
})
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (json.xprivkey) {
|
||||||
|
return {
|
||||||
|
xprivkey: json.encrypted
|
||||||
|
? utils.decrypt(json.xprivkey, passphrase)
|
||||||
|
: json.xprivkey
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (json.xpubkey) {
|
||||||
|
return {
|
||||||
|
xpubkey: json.xpubkey
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
HDPrivateKey.fromJSON = function fromJSON(json, passphrase) {
|
||||||
|
json = HDPrivateKey._fromJSON(json, passphrase);
|
||||||
|
|
||||||
|
if (json.seed) {
|
||||||
|
return new HDPrivateKey({
|
||||||
|
seed: new HDSeed(json.seed)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (json.xprivkey) {
|
if (json.xprivkey) {
|
||||||
return new HDPrivateKey({
|
return new HDPrivateKey({
|
||||||
xkey: json.encrypted
|
xkey: json.xprivkey
|
||||||
? utils.decrypt(json.xprivkey, passphrase)
|
|
||||||
: json.xprivkey
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -904,8 +928,6 @@ HDPrivateKey.fromJSON = function fromJSON(json, passphrase) {
|
|||||||
xkey: json.xpubkey
|
xkey: json.xpubkey
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(false);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -106,7 +106,7 @@ HTTPServer.prototype._init = function _init() {
|
|||||||
return next(err);
|
return next(err);
|
||||||
if (!tx)
|
if (!tx)
|
||||||
return send(404);
|
return send(404);
|
||||||
send(200, tx.toFullJSON());
|
send(200, tx.toJSON());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -118,7 +118,7 @@ HTTPServer.prototype._init = function _init() {
|
|||||||
return next(err);
|
return next(err);
|
||||||
if (!txs.length)
|
if (!txs.length)
|
||||||
return send(404);
|
return send(404);
|
||||||
send(200, txs.map(function(tx) { return tx.toFullJSON(); }));
|
send(200, txs.map(function(tx) { return tx.toJSON(); }));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -129,7 +129,7 @@ HTTPServer.prototype._init = function _init() {
|
|||||||
return next(err);
|
return next(err);
|
||||||
if (!txs.length)
|
if (!txs.length)
|
||||||
return send(404);
|
return send(404);
|
||||||
send(200, txs.map(function(tx) { return tx.toFullJSON(); }));
|
send(200, txs.map(function(tx) { return tx.toJSON(); }));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -147,7 +147,7 @@ HTTPServer.prototype._init = function _init() {
|
|||||||
return next(err);
|
return next(err);
|
||||||
if (!block)
|
if (!block)
|
||||||
return send(404);
|
return send(404);
|
||||||
send(200, block.toFullJSON());
|
send(200, block.toJSON());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -20,6 +20,8 @@ function Input(options) {
|
|||||||
if (!(this instanceof Input))
|
if (!(this instanceof Input))
|
||||||
return new Input(options);
|
return new Input(options);
|
||||||
|
|
||||||
|
assert(typeof options.script !== 'string');
|
||||||
|
|
||||||
prevout = options.prevout || options.out;
|
prevout = options.prevout || options.out;
|
||||||
|
|
||||||
this.prevout = {
|
this.prevout = {
|
||||||
@ -365,28 +367,71 @@ Input.prototype.inspect = function inspect() {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
Input.prototype.toFullJSON = function toFullJSON() {
|
Input.prototype.toJSON = function toJSON() {
|
||||||
return {
|
return {
|
||||||
prevout: {
|
prevout: {
|
||||||
hash: utils.revHex(this.prevout.hash),
|
hash: utils.revHex(this.prevout.hash),
|
||||||
index: this.prevout.index
|
index: this.prevout.index
|
||||||
},
|
},
|
||||||
output: this.output ? this.output.toFullJSON() : null,
|
output: this.output ? this.output.toJSON() : null,
|
||||||
script: utils.toHex(bcoin.script.encode(this.script)),
|
script: utils.toHex(bcoin.script.encode(this.script)),
|
||||||
sequence: this.sequence
|
sequence: this.sequence
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
Input.fromFullJSON = function fromFullJSON(json) {
|
Input._fromJSON = function _fromJSON(json) {
|
||||||
return new Input({
|
return {
|
||||||
prevout: {
|
prevout: {
|
||||||
hash: utils.revHex(json.prevout.hash),
|
hash: utils.revHex(json.prevout.hash),
|
||||||
index: json.prevout.index
|
index: json.prevout.index
|
||||||
},
|
},
|
||||||
output: json.output ? bcoin.coin.fromFullJSON(json.output) : null,
|
output: json.output ? bcoin.coin._fromJSON(json.output) : null,
|
||||||
script: bcoin.script.decode(new Buffer(json.script, 'hex')),
|
script: bcoin.script.decode(new Buffer(json.script, 'hex')),
|
||||||
sequence: json.sequence
|
sequence: json.sequence
|
||||||
});
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
Input.fromJSON = function fromJSON(json) {
|
||||||
|
return new Input(Input._fromJSON(json));
|
||||||
|
};
|
||||||
|
|
||||||
|
Input.prototype.toCompact = function toCompact() {
|
||||||
|
return {
|
||||||
|
type: 'input',
|
||||||
|
input: this.toRaw('hex')
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
Input._fromCompact = function _fromCompact(json) {
|
||||||
|
return Input._fromRaw(json.input, 'hex');
|
||||||
|
};
|
||||||
|
|
||||||
|
Input.fromCompact = function fromCompact(json) {
|
||||||
|
return new Input(Input._fromCompact(json));
|
||||||
|
};
|
||||||
|
|
||||||
|
Input.prototype.toRaw = function toRaw(enc) {
|
||||||
|
var data = bcoin.protocol.framer.input(this);
|
||||||
|
|
||||||
|
if (enc === 'hex')
|
||||||
|
data = utils.toHex(data);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
Input._fromRaw = function _fromRaw(data, enc) {
|
||||||
|
var parser = new bcoin.protocol.parser();
|
||||||
|
|
||||||
|
if (enc === 'hex')
|
||||||
|
data = new Buffer(data, 'hex');
|
||||||
|
|
||||||
|
data = parser.parseInput(data);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
Input.fromRaw = function fromRaw(data, enc) {
|
||||||
|
return new Input(Input._fromRaw(data, enc));
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -161,7 +161,7 @@ KeyPair.toSecret = function toSecret(privateKey, compressed) {
|
|||||||
return utils.toBase58(buf);
|
return utils.toBase58(buf);
|
||||||
};
|
};
|
||||||
|
|
||||||
KeyPair.fromSecret = function fromSecret(privateKey) {
|
KeyPair._fromSecret = function _fromSecret(privateKey) {
|
||||||
var key, compressed;
|
var key, compressed;
|
||||||
|
|
||||||
key = utils.fromBase58(privateKey);
|
key = utils.fromBase58(privateKey);
|
||||||
@ -178,10 +178,14 @@ KeyPair.fromSecret = function fromSecret(privateKey) {
|
|||||||
compressed = false;
|
compressed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new KeyPair({
|
return {
|
||||||
privateKey: privateKey,
|
privateKey: privateKey,
|
||||||
compressed: compressed
|
compressed: compressed
|
||||||
});
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
KeyPair.fromSecret = function fromSecret(privateKey) {
|
||||||
|
return new KeyPair(KeyPair._fromSecret(privateKey));
|
||||||
};
|
};
|
||||||
|
|
||||||
KeyPair.verify = function verify(msg, sig, key) {
|
KeyPair.verify = function verify(msg, sig, key) {
|
||||||
@ -215,7 +219,7 @@ KeyPair.prototype.toJSON = function toJSON(passphrase) {
|
|||||||
return json;
|
return json;
|
||||||
};
|
};
|
||||||
|
|
||||||
KeyPair.fromJSON = function fromJSON(json, passphrase) {
|
KeyPair._fromJSON = function _fromJSON(json, passphrase) {
|
||||||
var privateKey, publicKey, compressed;
|
var privateKey, publicKey, compressed;
|
||||||
|
|
||||||
assert.equal(json.v, 1);
|
assert.equal(json.v, 1);
|
||||||
@ -228,21 +232,25 @@ KeyPair.fromJSON = function fromJSON(json, passphrase) {
|
|||||||
privateKey = json.privateKey;
|
privateKey = json.privateKey;
|
||||||
if (json.encrypted)
|
if (json.encrypted)
|
||||||
privateKey = utils.decrypt(privateKey, passphrase);
|
privateKey = utils.decrypt(privateKey, passphrase);
|
||||||
return KeyPair.fromSecret(privateKey);
|
return KeyPair._fromSecret(privateKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (json.publicKey) {
|
if (json.publicKey) {
|
||||||
publicKey = utils.fromBase58(json.publicKey);
|
publicKey = utils.fromBase58(json.publicKey);
|
||||||
compressed = publicKey[0] !== 0x04;
|
compressed = publicKey[0] !== 0x04;
|
||||||
return new KeyPair({
|
return {
|
||||||
publicKey: publicKey,
|
publicKey: publicKey,
|
||||||
compressed: compressed
|
compressed: compressed
|
||||||
});
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(false);
|
assert(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
KeyPair.fromJSON = function fromJSON(json, passphrase) {
|
||||||
|
return new KeyPair(KeyPair._fromJSON(json, passphrase));
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expose
|
* Expose
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -20,6 +20,8 @@ function Output(options) {
|
|||||||
if (!(this instanceof Output))
|
if (!(this instanceof Output))
|
||||||
return new Output(options);
|
return new Output(options);
|
||||||
|
|
||||||
|
assert(typeof options.script !== 'string');
|
||||||
|
|
||||||
value = options.value;
|
value = options.value;
|
||||||
|
|
||||||
if (typeof value === 'number' && (value | 0) === value)
|
if (typeof value === 'number' && (value | 0) === value)
|
||||||
@ -259,18 +261,61 @@ Output.prototype.inspect = function inspect() {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
Output.prototype.toFullJSON = function toFullJSON() {
|
Output.prototype.toJSON = function toJSON() {
|
||||||
return {
|
return {
|
||||||
value: utils.btc(this.value),
|
value: utils.btc(this.value),
|
||||||
script: utils.toHex(bcoin.script.encode(this.script))
|
script: utils.toHex(bcoin.script.encode(this.script))
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
Output.fromFullJSON = function fromFullJSON(json) {
|
Output._fromJSON = function _fromJSON(json) {
|
||||||
return new Output({
|
return {
|
||||||
value: utils.satoshi(json.value),
|
value: utils.satoshi(json.value),
|
||||||
script: bcoin.script.decode(new Buffer(json.script, 'hex'))
|
script: bcoin.script.decode(new Buffer(json.script, 'hex'))
|
||||||
});
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
Output.fromJSON = function fromJSON(json) {
|
||||||
|
return new Output(Output._fromJSON(json));
|
||||||
|
};
|
||||||
|
|
||||||
|
Output.prototype.toCompact = function toCompact() {
|
||||||
|
return {
|
||||||
|
type: 'output',
|
||||||
|
output: this.toRaw('hex')
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
Output._fromCompact = function _fromCompact(json) {
|
||||||
|
return Output._fromRaw(json.output, 'hex');
|
||||||
|
};
|
||||||
|
|
||||||
|
Output.fromCompact = function fromCompact(json) {
|
||||||
|
return new Output(Output._fromCompact(json));
|
||||||
|
};
|
||||||
|
|
||||||
|
Output.prototype.toRaw = function toRaw(enc) {
|
||||||
|
var data = bcoin.protocol.framer.output(this);
|
||||||
|
|
||||||
|
if (enc === 'hex')
|
||||||
|
data = utils.toHex(data);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
Output._fromRaw = function _fromRaw(data, enc) {
|
||||||
|
var parser = new bcoin.protocol.parser();
|
||||||
|
|
||||||
|
if (enc === 'hex')
|
||||||
|
data = new Buffer(data, 'hex');
|
||||||
|
|
||||||
|
data = parser.parseOutput(data);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
Output.fromRaw = function fromRaw(data, enc) {
|
||||||
|
return new Output(Output._fromRaw(data, enc));
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -22,6 +22,8 @@ function TX(data, block) {
|
|||||||
if (!data)
|
if (!data)
|
||||||
data = {};
|
data = {};
|
||||||
|
|
||||||
|
assert(typeof data.hash !== 'string');
|
||||||
|
|
||||||
this.type = 'tx';
|
this.type = 'tx';
|
||||||
this.version = data.version || 1;
|
this.version = data.version || 1;
|
||||||
this.inputs = [];
|
this.inputs = [];
|
||||||
@ -1799,7 +1801,7 @@ TX.prototype.inspect = function inspect() {
|
|||||||
return copy;
|
return copy;
|
||||||
};
|
};
|
||||||
|
|
||||||
TX.prototype.toJSON = function toJSON(coins) {
|
TX.prototype.toCompact = function toCompact(coins) {
|
||||||
return {
|
return {
|
||||||
type: 'tx',
|
type: 'tx',
|
||||||
block: this.block,
|
block: this.block,
|
||||||
@ -1816,7 +1818,7 @@ TX.prototype.toJSON = function toJSON(coins) {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
TX.fromJSON = function fromJSON(json) {
|
TX._fromCompact = function _fromCompact(json) {
|
||||||
var raw, data, tx;
|
var raw, data, tx;
|
||||||
|
|
||||||
assert.equal(json.type, 'tx');
|
assert.equal(json.type, 'tx');
|
||||||
@ -1832,21 +1834,23 @@ TX.fromJSON = function fromJSON(json) {
|
|||||||
data.relayedBy = json.relayedBy;
|
data.relayedBy = json.relayedBy;
|
||||||
data.changeIndex = json.changeIndex;
|
data.changeIndex = json.changeIndex;
|
||||||
|
|
||||||
tx = new TX(data);
|
|
||||||
|
|
||||||
if (json.coins) {
|
if (json.coins) {
|
||||||
json.coins.forEach(function(output, i) {
|
json.coins.forEach(function(output, i) {
|
||||||
if (!output)
|
if (!output)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
tx.inputs[i].output = bcoin.coin.fromRaw(output, 'hex');
|
data.inputs[i].output = bcoin.coin._fromRaw(output, 'hex');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return tx;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
TX.prototype.toFullJSON = function toFullJSON() {
|
TX.fromCompact = function fromCompact(json) {
|
||||||
|
return new TX(TX._fromCompact(json));
|
||||||
|
};
|
||||||
|
|
||||||
|
TX.prototype.toJSON = function toJSON() {
|
||||||
return {
|
return {
|
||||||
type: 'tx',
|
type: 'tx',
|
||||||
hash: utils.revHex(this.hash('hex')),
|
hash: utils.revHex(this.hash('hex')),
|
||||||
@ -1859,17 +1863,17 @@ TX.prototype.toFullJSON = function toFullJSON() {
|
|||||||
changeIndex: this.changeIndex,
|
changeIndex: this.changeIndex,
|
||||||
version: this.version,
|
version: this.version,
|
||||||
inputs: this.inputs.map(function(input) {
|
inputs: this.inputs.map(function(input) {
|
||||||
return input.toFullJSON();
|
return input.toJSON();
|
||||||
}),
|
}),
|
||||||
outputs: this.outputs.map(function(output) {
|
outputs: this.outputs.map(function(output) {
|
||||||
return output.toFullJSON();
|
return output.toJSON();
|
||||||
}),
|
}),
|
||||||
locktime: this.locktime
|
locktime: this.locktime
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
TX.fromFullJSON = function fromFullJSON(json) {
|
TX._fromJSON = function fromJSON(json) {
|
||||||
return new TX({
|
return {
|
||||||
block: json.block ? utils.revHex(json.block) : null,
|
block: json.block ? utils.revHex(json.block) : null,
|
||||||
height: json.height,
|
height: json.height,
|
||||||
ts: json.ts,
|
ts: json.ts,
|
||||||
@ -1879,22 +1883,21 @@ TX.fromFullJSON = function fromFullJSON(json) {
|
|||||||
changeIndex: json.changeIndex,
|
changeIndex: json.changeIndex,
|
||||||
version: json.version,
|
version: json.version,
|
||||||
inputs: json.inputs.map(function(input) {
|
inputs: json.inputs.map(function(input) {
|
||||||
return bcoin.input.fromFullJSON(input);
|
return bcoin.input._fromJSON(input);
|
||||||
}),
|
}),
|
||||||
outputs: json.outputs.map(function(output) {
|
outputs: json.outputs.map(function(output) {
|
||||||
return bcoin.output.fromFullJSON(output);
|
return bcoin.output._fromJSON(output);
|
||||||
}),
|
}),
|
||||||
locktime: json.locktime
|
locktime: json.locktime
|
||||||
});
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
TX.fromJSON = function fromJSON(json) {
|
||||||
|
return new TX(TX._fromJSON(json));
|
||||||
};
|
};
|
||||||
|
|
||||||
TX.prototype.toRaw = function toRaw(enc) {
|
TX.prototype.toRaw = function toRaw(enc) {
|
||||||
var data;
|
var data = this.render();
|
||||||
|
|
||||||
if (this.isStatic() && this._raw)
|
|
||||||
data = this._raw;
|
|
||||||
else
|
|
||||||
data = new Buffer(this.render());
|
|
||||||
|
|
||||||
if (enc === 'hex')
|
if (enc === 'hex')
|
||||||
data = utils.toHex(data);
|
data = utils.toHex(data);
|
||||||
@ -1902,13 +1905,17 @@ TX.prototype.toRaw = function toRaw(enc) {
|
|||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
TX.fromRaw = function fromRaw(data, enc) {
|
TX._fromRaw = function _fromRaw(data, enc) {
|
||||||
var parser = new bcoin.protocol.parser();
|
var parser = new bcoin.protocol.parser();
|
||||||
|
|
||||||
if (enc === 'hex')
|
if (enc === 'hex')
|
||||||
data = new Buffer(data, 'hex');
|
data = new Buffer(data, 'hex');
|
||||||
|
|
||||||
return new bcoin.tx(parser.parseTX(data));
|
return parser.parseTX(data);
|
||||||
|
};
|
||||||
|
|
||||||
|
TX.fromRaw = function fromRaw(data, enc) {
|
||||||
|
return new bcoin.tx(TX._fromRaw(data, enc));
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -871,7 +871,7 @@ Wallet.prototype.toJSON = function toJSON(noPool) {
|
|||||||
}),
|
}),
|
||||||
balance: utils.btc(this.getBalance()),
|
balance: utils.btc(this.getBalance()),
|
||||||
txs: noPool ? [] : this.tx.getAll().map(function(tx) {
|
txs: noPool ? [] : this.tx.getAll().map(function(tx) {
|
||||||
return tx.toJSON();
|
return tx.toCompact();
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@ -899,7 +899,7 @@ Wallet._fromJSON = function _fromJSON(json, passphrase) {
|
|||||||
addressMap: json.addressMap,
|
addressMap: json.addressMap,
|
||||||
keys: json.keys,
|
keys: json.keys,
|
||||||
txs: json.txs.map(function(json) {
|
txs: json.txs.map(function(json) {
|
||||||
return bcoin.tx.fromJSON(json);
|
return bcoin.tx.fromCompact(json);
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@ -26,7 +26,7 @@ describe('Block', function() {
|
|||||||
'33825657ba32afe269819f01993bd77baba86379043168c94845d32370e53562' ],
|
'33825657ba32afe269819f01993bd77baba86379043168c94845d32370e53562' ],
|
||||||
flags: [ 245, 90, 0 ]
|
flags: [ 245, 90, 0 ]
|
||||||
}, 'merkleblock');
|
}, 'merkleblock');
|
||||||
var raw = block.toJSON().block;
|
var raw = block.toCompact().block;
|
||||||
|
|
||||||
it('should parse partial merkle tree', function() {
|
it('should parse partial merkle tree', function() {
|
||||||
assert(block.verify());
|
assert(block.verify());
|
||||||
@ -50,10 +50,10 @@ describe('Block', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should be jsonified and unjsonified and still verify', function() {
|
it('should be jsonified and unjsonified and still verify', function() {
|
||||||
var json = block.toJSON();
|
var json = block.toCompact();
|
||||||
assert.equal(json.subtype, 'merkleblock');
|
assert.equal(json.subtype, 'merkleblock');
|
||||||
assert.equal(typeof json.block, 'string');
|
assert.equal(typeof json.block, 'string');
|
||||||
var b = bcoin.block(bcoin.block.fromJSON(json), json.subtype);
|
var b = bcoin.block.fromCompact(json);
|
||||||
assert.equal(bcoin.utils.toHex(b.render()), json.block);
|
assert.equal(bcoin.utils.toHex(b.render()), json.block);
|
||||||
assert(b.verify());
|
assert(b.verify());
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user