tx: .maxSize() method

This commit is contained in:
Fedor Indutny 2014-05-09 15:51:58 +04:00
parent 17d0903a35
commit cf2e0042ff
2 changed files with 33 additions and 1 deletions

View File

@ -70,7 +70,7 @@ TX.prototype.input = function input(i, index) {
var input = {
out: {
tx: i.tx || null,
tx: (i.out ? i.out.tx : i.tx) || null,
hash: hash,
index: i.out ? i.out.index : i.index,
},
@ -180,6 +180,36 @@ TX.prototype.verify = function verify() {
}, this);
};
TX.prototype.maxSize = function maxSize() {
// Create copy with 0-script inputs
var copy = this.clone();
copy.inputs.forEach(function(input) {
input.script = [];
});
var size = copy.render().length;
// Add size for signatures and public keys
copy.inputs.forEach(function(input) {
var s = input.out.tx.outputs[input.out.index].script;
if (bcoin.script.isPubkeyhash(s)) {
// Signature + len
size += 74;
// Pub key + len
size += 34;
return;
}
// Multisig
// Empty byte
size += 1;
// Signature + len
size += 74;
});
return size;
};
TX.prototype.toJSON = function toJSON() {
// Compact representation
var ts = new Array(4);

View File

@ -67,7 +67,9 @@ describe('Wallet', function() {
.input(src, 0)
.out(w.getAddress(), 5460);
var maxSize = tx.maxSize();
w.sign(tx);
assert(tx.render().length <= maxSize);
assert(tx.verify());
});