tx refactor. etc.
This commit is contained in:
parent
8087601a7f
commit
6b10fabc5f
@ -50,33 +50,30 @@ function Chain(options) {
|
|||||||
|
|
||||||
this.request = new utils.RequestCache();
|
this.request = new utils.RequestCache();
|
||||||
|
|
||||||
this.fromJSON(require('./protocol/preload-full'));
|
if (process.env.BCOIN_START_HEIGHT) {
|
||||||
this.storage = null;
|
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);
|
if (!this.options.fullNode)
|
||||||
// this.resetHeight(145000);
|
this.fromJSON(network.preload);
|
||||||
// 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);
|
|
||||||
|
|
||||||
this.tip = this.index.entries[this.index.entries.length - 1];
|
this.tip = this.index.entries[this.index.entries.length - 1];
|
||||||
|
|
||||||
|
|||||||
@ -213,10 +213,10 @@ Output.getData = function getData(output) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bcoin.script.isColored(s)) {
|
if (bcoin.script.isNulldata(s)) {
|
||||||
data = bcoin.script.colored(s);
|
data = bcoin.script.nulldata(s);
|
||||||
return utils.merge(def, {
|
return utils.merge(def, {
|
||||||
type: 'colored',
|
type: 'nulldata',
|
||||||
flags: data,
|
flags: data,
|
||||||
text: utils.array2utf8(data),
|
text: utils.array2utf8(data),
|
||||||
none: true
|
none: true
|
||||||
|
|||||||
@ -900,7 +900,7 @@ script.standard = function standard(s) {
|
|||||||
|| (script.isPubkeyhash(s) && 'pubkeyhash')
|
|| (script.isPubkeyhash(s) && 'pubkeyhash')
|
||||||
|| (script.isMultisig(s) && 'multisig')
|
|| (script.isMultisig(s) && 'multisig')
|
||||||
|| (script.isScripthash(s) && 'scripthash')
|
|| (script.isScripthash(s) && 'scripthash')
|
||||||
|| (script.isColored(s) && 'colored')
|
|| (script.isNulldata(s) && 'nulldata')
|
||||||
|| null;
|
|| null;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -918,7 +918,7 @@ script.isStandard = function isStandard(s) {
|
|||||||
return false;
|
return false;
|
||||||
if (m < 1 || m > n)
|
if (m < 1 || m > n)
|
||||||
return false;
|
return false;
|
||||||
} else if (type === 'colored') {
|
} else if (type === 'nulldata') {
|
||||||
if (script.size(s) > constants.script.maxOpReturnBytes)
|
if (script.size(s) > constants.script.maxOpReturnBytes)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -1089,7 +1089,7 @@ script.isScripthash = function isScripthash(s, hash) {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
script.isColored = function isColored(s) {
|
script.isNulldata = function isNulldata(s) {
|
||||||
s = script.subscript(s);
|
s = script.subscript(s);
|
||||||
|
|
||||||
if (s.length !== 2)
|
if (s.length !== 2)
|
||||||
@ -1100,8 +1100,8 @@ script.isColored = function isColored(s) {
|
|||||||
&& s[1].length <= constants.script.maxOpReturn;
|
&& s[1].length <= constants.script.maxOpReturn;
|
||||||
};
|
};
|
||||||
|
|
||||||
script.colored = function colored(s) {
|
script.nulldata = function nulldata(s) {
|
||||||
if (!script.isColored(s))
|
if (!script.isNulldata(s))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return script.subscript(s)[1];
|
return script.subscript(s)[1];
|
||||||
@ -1548,7 +1548,7 @@ script.args = function args(s) {
|
|||||||
if (this.type === 'scripthash')
|
if (this.type === 'scripthash')
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
if (this.type === 'colored')
|
if (this.type === 'nulldata')
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
@ -371,24 +371,55 @@ TX.prototype.output = function output(obj, value) {
|
|||||||
TX.prototype.out = TX.prototype.output;
|
TX.prototype.out = TX.prototype.output;
|
||||||
|
|
||||||
TX.prototype.scriptOutput = function scriptOutput(output, options) {
|
TX.prototype.scriptOutput = function scriptOutput(output, options) {
|
||||||
|
var script = output.script;
|
||||||
|
var keys, m, n, hash, locktime;
|
||||||
|
|
||||||
options = options || output;
|
options = options || output;
|
||||||
|
|
||||||
var script = output.script;
|
if (options instanceof bcoin.output) {
|
||||||
var keys, m, n, hash, color;
|
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
|
// Raw multisig transaction
|
||||||
// https://github.com/bitcoin/bips/blob/master/bip-0010.mediawiki
|
// 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-0011.mediawiki
|
||||||
// https://github.com/bitcoin/bips/blob/master/bip-0019.mediawiki
|
// https://github.com/bitcoin/bips/blob/master/bip-0019.mediawiki
|
||||||
// [required-sigs] [pubkey-hash1] [pubkey-hash2] ... [number-of-keys] checkmultisig
|
// [required-sigs] [pubkey-hash1] [pubkey-hash2] ... [number-of-keys] checkmultisig
|
||||||
keys = options.keys || options.address;
|
keys = options.keys;
|
||||||
|
|
||||||
if (keys === options.address) {
|
|
||||||
keys = keys.map(function(address) {
|
|
||||||
return bcoin.wallet.addr2hash(address, 'pubkeyhash');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
keys = keys.map(function(key) {
|
keys = keys.map(function(key) {
|
||||||
if (typeof key === 'string')
|
if (typeof key === 'string')
|
||||||
@ -396,8 +427,6 @@ TX.prototype.scriptOutput = function scriptOutput(output, options) {
|
|||||||
return key;
|
return key;
|
||||||
});
|
});
|
||||||
|
|
||||||
// compat:
|
|
||||||
options.m = options.minSignatures || options.m;
|
|
||||||
m = options.m || keys.length;
|
m = options.m || keys.length;
|
||||||
n = options.n || keys.length;
|
n = options.n || keys.length;
|
||||||
|
|
||||||
@ -437,17 +466,20 @@ TX.prototype.scriptOutput = function scriptOutput(output, options) {
|
|||||||
'eqverify',
|
'eqverify',
|
||||||
'checksig'
|
'checksig'
|
||||||
];
|
];
|
||||||
} else if (options.color) {
|
} else if (options.nulldata) {
|
||||||
color = options.color;
|
nulldata = options.nulldata;
|
||||||
if (typeof color === 'string')
|
if (typeof nulldata === 'string')
|
||||||
color = utils.ascii2array(color);
|
nulldata = utils.ascii2array(nulldata);
|
||||||
assert(color.length <= 40);
|
assert(nulldata.length <= constants.script.maxOpReturn);
|
||||||
script = [
|
script = [
|
||||||
'ret',
|
'ret',
|
||||||
color
|
nulldata
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (locktime)
|
||||||
|
script = locktime.concat(script);
|
||||||
|
|
||||||
output.script = script;
|
output.script = script;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -896,7 +928,7 @@ TX.prototype.sigops = function sigops(scripthash, accurate) {
|
|||||||
|
|
||||||
TX.prototype.isStandard = function isStandard() {
|
TX.prototype.isStandard = function isStandard() {
|
||||||
var i, input, output, type;
|
var i, input, output, type;
|
||||||
var colored = 0;
|
var nulldata = 0;
|
||||||
|
|
||||||
if (this.version > constants.tx.version || this.version < 1)
|
if (this.version > constants.tx.version || this.version < 1)
|
||||||
return false;
|
return false;
|
||||||
@ -924,8 +956,8 @@ TX.prototype.isStandard = function isStandard() {
|
|||||||
if (!type)
|
if (!type)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (type === 'colored') {
|
if (type === 'nulldata') {
|
||||||
colored++;
|
nulldata++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -936,7 +968,7 @@ TX.prototype.isStandard = function isStandard() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (colored > 1)
|
if (nulldata > 1)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -1080,6 +1112,7 @@ TX.prototype.toJSON = function toJSON() {
|
|||||||
block: this.block,
|
block: this.block,
|
||||||
network: this.network,
|
network: this.network,
|
||||||
relayedBy: this.relayedBy,
|
relayedBy: this.relayedBy,
|
||||||
|
changeIndex: this.outputs.indexOf(this.changeOutput),
|
||||||
tx: utils.toHex(this.render())
|
tx: utils.toHex(this.render())
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@ -1104,6 +1137,11 @@ TX.fromJSON = function fromJSON(json) {
|
|||||||
tx.block = json.block || null;
|
tx.block = json.block || null;
|
||||||
tx.ps = json.ps;
|
tx.ps = json.ps;
|
||||||
|
|
||||||
|
if (data.changeIndex >= 0) {
|
||||||
|
tx.changeOutput = tx.outputs[data.changeIndex];
|
||||||
|
assert(tx.changeOutput);
|
||||||
|
}
|
||||||
|
|
||||||
return tx;
|
return tx;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -282,7 +282,7 @@ Wallet.prototype.getOwnHash = function getOwnHash() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Wallet.prototype.getOwnAddress = function getOwnAddress() {
|
Wallet.prototype.getOwnAddress = function getOwnAddress() {
|
||||||
return Wallet.hash2addr(this.getOwnHash(), this.type);
|
return Wallet.hash2addr(this.getOwnHash(), 'pubkeyhash');
|
||||||
};
|
};
|
||||||
|
|
||||||
Wallet.prototype.getHash = function getHash() {
|
Wallet.prototype.getHash = function getHash() {
|
||||||
@ -442,21 +442,10 @@ Wallet.prototype.ownInput = function ownInput(tx, index) {
|
|||||||
return inputs;
|
return inputs;
|
||||||
};
|
};
|
||||||
|
|
||||||
Wallet.prototype.scriptOutputs = function scriptOutputs(tx, options, outputs) {
|
Wallet.prototype.scriptOutputs = function scriptOutputs(tx, options) {
|
||||||
options = options || {};
|
|
||||||
|
|
||||||
if (this.n > 1) {
|
|
||||||
options.keys = this.keys;
|
|
||||||
options.m = this.m || 1;
|
|
||||||
options.n = this.n || 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
outputs = outputs || tx.outputs;
|
|
||||||
|
|
||||||
outputs.forEach(function(output) {
|
outputs.forEach(function(output) {
|
||||||
tx.scriptOutput(output, options);
|
tx.scriptOutput(output, output);
|
||||||
});
|
});
|
||||||
|
|
||||||
return outputs.length;
|
return outputs.length;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user