tx refactor. etc.

This commit is contained in:
Christopher Jeffrey 2016-01-08 19:27:00 -08:00
parent 8087601a7f
commit 6b10fabc5f
5 changed files with 95 additions and 71 deletions

View File

@ -50,33 +50,30 @@ function Chain(options) {
this.request = new utils.RequestCache();
this.fromJSON(require('./protocol/preload-full'));
this.storage = null;
if (process.env.BCOIN_START_HEIGHT) {
this.storage = null;
this.fromJSON(require('./protocol/preload-full'));
this.resetHeight(+process.env.BCOIN_START_HEIGHT);
} else {
this.fromJSON({
v: 2,
type: 'chain',
network: network.type,
entries: [
{
hash: utils.toHex(network.genesis._hash),
version: network.genesis.version,
prevBlock: utils.toHex(network.genesis.prevBlock),
ts: network.genesis.ts,
bits: network.genesis.bits,
height: 0
}
]
});
// this.resetHeight(133000);
// this.resetHeight(145000);
// this.resetHeight(350000);
// if (0)
this.fromJSON({
v: 2,
type: 'chain',
network: network.type,
entries: [
{
hash: utils.toHex(network.genesis._hash),
version: network.genesis.version,
prevBlock: utils.toHex(network.genesis.prevBlock),
ts: network.genesis.ts,
bits: network.genesis.bits,
height: 0
}
]
});
// if (0)
if (!this.options.fullNode)
this.fromJSON(network.preload);
if (!this.options.fullNode)
this.fromJSON(network.preload);
}
this.tip = this.index.entries[this.index.entries.length - 1];

View File

@ -213,10 +213,10 @@ Output.getData = function getData(output) {
});
}
if (bcoin.script.isColored(s)) {
data = bcoin.script.colored(s);
if (bcoin.script.isNulldata(s)) {
data = bcoin.script.nulldata(s);
return utils.merge(def, {
type: 'colored',
type: 'nulldata',
flags: data,
text: utils.array2utf8(data),
none: true

View File

@ -900,7 +900,7 @@ script.standard = function standard(s) {
|| (script.isPubkeyhash(s) && 'pubkeyhash')
|| (script.isMultisig(s) && 'multisig')
|| (script.isScripthash(s) && 'scripthash')
|| (script.isColored(s) && 'colored')
|| (script.isNulldata(s) && 'nulldata')
|| null;
};
@ -918,7 +918,7 @@ script.isStandard = function isStandard(s) {
return false;
if (m < 1 || m > n)
return false;
} else if (type === 'colored') {
} else if (type === 'nulldata') {
if (script.size(s) > constants.script.maxOpReturnBytes)
return false;
}
@ -1089,7 +1089,7 @@ script.isScripthash = function isScripthash(s, hash) {
return true;
};
script.isColored = function isColored(s) {
script.isNulldata = function isNulldata(s) {
s = script.subscript(s);
if (s.length !== 2)
@ -1100,8 +1100,8 @@ script.isColored = function isColored(s) {
&& s[1].length <= constants.script.maxOpReturn;
};
script.colored = function colored(s) {
if (!script.isColored(s))
script.nulldata = function nulldata(s) {
if (!script.isNulldata(s))
return false;
return script.subscript(s)[1];
@ -1548,7 +1548,7 @@ script.args = function args(s) {
if (this.type === 'scripthash')
return 1;
if (this.type === 'colored')
if (this.type === 'nulldata')
return -1;
return -1;

View File

@ -371,24 +371,55 @@ TX.prototype.output = function output(obj, value) {
TX.prototype.out = TX.prototype.output;
TX.prototype.scriptOutput = function scriptOutput(output, options) {
var script = output.script;
var keys, m, n, hash, locktime;
options = options || output;
var script = output.script;
var keys, m, n, hash, color;
if (options instanceof bcoin.output) {
options = Object.keys(options).reduce(function(out, key) {
out[key] = options[key];
return out;
}, {});
}
if (Array.isArray(options.keys || options.address)) {
if (options.addr) {
options.address = options.addr;
delete options.addr;
}
if (Array.isArray(options.address)) {
options.keys = options.address.map(function(address) {
return bcoin.wallet.addr2hash(address, 'pubkeyhash');
});
delete options.address;
}
if (options.minSignatures) {
options.m = options.minSignatures;
delete options.minSignatures;
}
if (options.color) {
options.nulldata = options.color;
delete options.color;
}
if (options.lock != null) {
locktime = [
new bn(options.lock).toArray(),
'checklocktimeverify',
'drop'
];
}
if (Array.isArray(options.keys)) {
// Raw multisig transaction
// https://github.com/bitcoin/bips/blob/master/bip-0010.mediawiki
// https://github.com/bitcoin/bips/blob/master/bip-0011.mediawiki
// https://github.com/bitcoin/bips/blob/master/bip-0019.mediawiki
// [required-sigs] [pubkey-hash1] [pubkey-hash2] ... [number-of-keys] checkmultisig
keys = options.keys || options.address;
if (keys === options.address) {
keys = keys.map(function(address) {
return bcoin.wallet.addr2hash(address, 'pubkeyhash');
});
}
keys = options.keys;
keys = keys.map(function(key) {
if (typeof key === 'string')
@ -396,8 +427,6 @@ TX.prototype.scriptOutput = function scriptOutput(output, options) {
return key;
});
// compat:
options.m = options.minSignatures || options.m;
m = options.m || keys.length;
n = options.n || keys.length;
@ -437,17 +466,20 @@ TX.prototype.scriptOutput = function scriptOutput(output, options) {
'eqverify',
'checksig'
];
} else if (options.color) {
color = options.color;
if (typeof color === 'string')
color = utils.ascii2array(color);
assert(color.length <= 40);
} else if (options.nulldata) {
nulldata = options.nulldata;
if (typeof nulldata === 'string')
nulldata = utils.ascii2array(nulldata);
assert(nulldata.length <= constants.script.maxOpReturn);
script = [
'ret',
color
nulldata
];
}
if (locktime)
script = locktime.concat(script);
output.script = script;
};
@ -896,7 +928,7 @@ TX.prototype.sigops = function sigops(scripthash, accurate) {
TX.prototype.isStandard = function isStandard() {
var i, input, output, type;
var colored = 0;
var nulldata = 0;
if (this.version > constants.tx.version || this.version < 1)
return false;
@ -924,8 +956,8 @@ TX.prototype.isStandard = function isStandard() {
if (!type)
return false;
if (type === 'colored') {
colored++;
if (type === 'nulldata') {
nulldata++;
continue;
}
@ -936,7 +968,7 @@ TX.prototype.isStandard = function isStandard() {
return false;
}
if (colored > 1)
if (nulldata > 1)
return false;
return true;
@ -1080,6 +1112,7 @@ TX.prototype.toJSON = function toJSON() {
block: this.block,
network: this.network,
relayedBy: this.relayedBy,
changeIndex: this.outputs.indexOf(this.changeOutput),
tx: utils.toHex(this.render())
};
};
@ -1104,6 +1137,11 @@ TX.fromJSON = function fromJSON(json) {
tx.block = json.block || null;
tx.ps = json.ps;
if (data.changeIndex >= 0) {
tx.changeOutput = tx.outputs[data.changeIndex];
assert(tx.changeOutput);
}
return tx;
};

View File

@ -282,7 +282,7 @@ Wallet.prototype.getOwnHash = function getOwnHash() {
};
Wallet.prototype.getOwnAddress = function getOwnAddress() {
return Wallet.hash2addr(this.getOwnHash(), this.type);
return Wallet.hash2addr(this.getOwnHash(), 'pubkeyhash');
};
Wallet.prototype.getHash = function getHash() {
@ -442,21 +442,10 @@ Wallet.prototype.ownInput = function ownInput(tx, index) {
return inputs;
};
Wallet.prototype.scriptOutputs = function scriptOutputs(tx, options, outputs) {
options = options || {};
if (this.n > 1) {
options.keys = this.keys;
options.m = this.m || 1;
options.n = this.n || 1;
}
outputs = outputs || tx.outputs;
Wallet.prototype.scriptOutputs = function scriptOutputs(tx, options) {
outputs.forEach(function(output) {
tx.scriptOutput(output, options);
tx.scriptOutput(output, output);
});
return outputs.length;
};