btc-script
Advanced tools
Comparing version 0.0.2 to 0.1.1
@@ -1,5 +0,16 @@ | ||
Next Milestone | ||
-------------- | ||
0.1.1 / 2014-03-13 | ||
------------------ | ||
* Made script type setting an explicit parameter passed to `createOutputScript()` rather than guessing based on version | ||
* removed method `simpleInHash()`. Closes #1 | ||
* removed method `simpleInPubkeyHash()` | ||
* fixed correct output type in `getOutType()` Closes #6 | ||
* added `defaultNetworkType`, implicitly sets `Address.defaultNetworkType` | ||
* replaced `convert-hex` for `binstring` | ||
* (straight to 0.1.1, bug in NPM: https://github.com/npm/npm/issues/4653) | ||
0.0.2 / 2014-02-04 | ||
------------------ | ||
* added fromChunks method to produce a script object from chunks array | ||
0.0.1 / 2014-01-12 | ||
@@ -9,4 +20,2 @@ ------------------ | ||
0.0.2 / 2014-02-04 | ||
------------------ | ||
* added fromChunks method to produce a script object from chunks array | ||
var Opcode = require('btc-opcode'); | ||
var conv = require('convert-hex'); | ||
var conv = require('binstring'); | ||
var Address = require('btc-address'); | ||
var cryptoHash = require('crypto-hashing') | ||
module.exports = Script | ||
var _defaultNetwork = 'mainnet'; | ||
Address.defaultNetwork = _defaultNetwork; | ||
module.exports = Script; | ||
Object.defineProperty(Script, 'defaultNetwork', { | ||
set: function(val) { | ||
_defaultNetwork = val; | ||
Address.defaultNetwork = val; | ||
}, | ||
get: function() { | ||
return _defaultNetwork; | ||
} | ||
}) | ||
function Script(data) { | ||
@@ -11,3 +24,3 @@ if (!data) { | ||
} else if ("string" == typeof data) { | ||
this.buffer = conv.hexToBytes(data); | ||
this.buffer = conv(data, {in: 'hex', out: 'bytes'}); | ||
} else if (Array.isArray(data)) { | ||
@@ -22,3 +35,3 @@ this.buffer = data; | ||
this.parse(); | ||
}; | ||
} | ||
@@ -32,3 +45,3 @@ Script.fromPubKey = function(str) { | ||
} else { | ||
script.writeBytes(conv.hexToBytes(s[i])); | ||
script.writeBytes(conv(s[i], {in: 'hex', out: 'bytes'})); | ||
} | ||
@@ -46,3 +59,3 @@ } | ||
} else { | ||
script.writeBytes(conv.hexToBytes(s[i])); | ||
script.writeBytes(conv(s[i], {in: 'hex', out: 'bytes'})); | ||
} | ||
@@ -125,5 +138,6 @@ } | ||
* Currently supported are: | ||
* Address: | ||
* Pubkeyhash | ||
* Paying to a Bitcoin address which is the hash of a pubkey. | ||
* OP_DUP OP_HASH160 [pubKeyHash] OP_EQUALVERIFY OP_CHECKSIG | ||
* Example: | ||
* | ||
@@ -133,13 +147,41 @@ * Pubkey: | ||
* [pubKey] OP_CHECKSIG | ||
* Example: txid 0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098 | ||
* | ||
* Scripthash (P2SH) | ||
* Paying to an address which is the hash of a script | ||
* OP_HASH160 [Scripthash] OP_EQUAL | ||
* Example: | ||
* | ||
* Multisig | ||
* Paying to multiple pubkeys and require a number of the signatures | ||
* m [pubkey] [pubkey] [pubkey] n OP_CHECKMULTISIG | ||
* Example: | ||
* | ||
* Strange: | ||
* Any other script (no template matched). | ||
*/ | ||
// Below is the current standard set of out types | ||
/* const char* GetTxnOutputType(txnouttype t) | ||
{ | ||
switch (t) | ||
{ | ||
case TX_NONSTANDARD: return "nonstandard"; | ||
case TX_PUBKEY: return "pubkey"; | ||
case TX_PUBKEYHASH: return "pubkeyhash"; | ||
case TX_SCRIPTHASH: return "scripthash"; | ||
case TX_MULTISIG: return "multisig"; | ||
case TX_NULL_DATA: return "nulldata"; | ||
} | ||
return NULL; | ||
}*/ | ||
// todo: analyze escrow transactions | ||
// https://blockchain.info/tx/09dd94f2c85262173da87a745a459007bb1eed6eeb6bfa238a0cd91a16cf7790?show_adv=true | ||
// https://github.com/bitcoin/bitcoin/blob/19e5b9d2dfcac4efadba636745485d9660fb1abe/src/script.cpp#L75 | ||
// supporting tx_null_data https://github.com/bitcoin/bitcoin/pull/3128 | ||
// https://helloblock.io/mainnet/transactions/ebc9fa1196a59e192352d76c0f6e73167046b9d37b8302b6bb6968dfd279b767 | ||
Script.prototype.getOutType = function() { | ||
if (this.chunks[this.chunks.length - 1] == Opcode.map.OP_EQUAL && | ||
this.chunks[0] == Opcode.map.OP_HASH160 && | ||
this.chunks.length == 3) { | ||
// Transfer to M-OF-N | ||
return 'P2SH'; | ||
} else if (this.chunks.length == 5 && | ||
if (this.chunks.length == 5 && | ||
this.chunks[0] == Opcode.map.OP_DUP && | ||
@@ -150,5 +192,18 @@ this.chunks[1] == Opcode.map.OP_HASH160 && | ||
// Transfer to Bitcoin address | ||
return 'Pubkey'; | ||
return 'pubkeyhash'; | ||
} else if (this.chunks.length === 2 && | ||
Array.isArray(this.chunks[0]) && | ||
this.chunks[1] === Opcode.map.OP_CHECKSIG) { | ||
// [pubkey] OP_CHECKSIG | ||
return 'pubkey'; | ||
} else if (this.chunks[this.chunks.length - 1] == Opcode.map.OP_EQUAL && | ||
this.chunks[0] == Opcode.map.OP_HASH160 && | ||
this.chunks.length == 3) { | ||
// Transfer to M-OF-N | ||
return 'scripthash'; | ||
} else if (this.chunks[this.chunks.length - 1] === Opcode.map.OP_CHECKMULTISIG) { | ||
// if script ends with OP_CHECKMULTISIG | ||
return 'multisig' | ||
} else { | ||
return 'Strange'; | ||
return 'nonstandard'; | ||
} | ||
@@ -169,5 +224,18 @@ } | ||
Script.prototype.toAddress = function() { | ||
Script.prototype.toAddress = function(network) { | ||
var outType = this.getOutType(); | ||
return outType == 'Pubkey' ? new Address(this.chunks[2]) : outType == 'P2SH' ? new Address(this.chunks[1], 5) : new Address(this.chunks[1], 5) | ||
if (outType == 'pubkeyhash') { | ||
return new Address(this.chunks[2], 'pubkeyhash', network || Script.defaultNetwork) | ||
} else if (outType == 'pubkey') { | ||
// convert pubkey into a pubkeyhash and do address | ||
return new Address(cryptoHash.ripemd160(cryptoHash.sha256(this.chunks[0], { | ||
out: 'bytes' | ||
}), { | ||
out: 'bytes' | ||
}), 'pubkeyhash', network || Script.defaultNetwork) | ||
} else if (outType == 'scripthash') { | ||
return new Address(this.chunks[1], 'scripthash', network || Script.defaultNetwork) | ||
} else { | ||
return false | ||
} | ||
} | ||
@@ -205,7 +273,7 @@ | ||
// TODO: We could also check that the length of the data is correct. | ||
return 'Pubkey'; | ||
return 'pubkey'; | ||
} else if (this.chunks.length == 2 && | ||
Array.isArray(this.chunks[0]) && | ||
Array.isArray(this.chunks[1])) { | ||
return 'Address'; | ||
return 'pubkeyhash'; | ||
} else if (this.chunks[0] == Opcode.map.OP_0 && | ||
@@ -215,5 +283,5 @@ this.chunks.slice(1).reduce(function(t, chunk, i) { | ||
}, true)) { | ||
return 'Multisig'; | ||
return 'multisig'; | ||
} else { | ||
return 'Strange'; | ||
return 'nonstandard'; | ||
} | ||
@@ -237,5 +305,5 @@ }; | ||
switch (this.getInType()) { | ||
case 'Address': | ||
case 'pubkeyhash': | ||
return this.chunks[1]; | ||
case 'Pubkey': | ||
case 'pubkey': | ||
// TODO: Theoretically, we could recover the pubkey from the sig here. | ||
@@ -249,27 +317,3 @@ // See https://bitcointalk.org/?topic=6430.0 | ||
/** | ||
* Returns the affected address hash for this input. | ||
* | ||
* For standard transactions, this will return the hash of the pubKey that | ||
* can spend this output. | ||
* | ||
* In the future, for standard payToScriptHash inputs, this will return the | ||
* scriptHash. | ||
* | ||
* Note: This function provided for convenience. If you have the corresponding | ||
* scriptPubKey available, you are urged to use Script#simpleOutHash instead | ||
* as it is more reliable for non-standard payToScriptHash transactions. | ||
* | ||
* This method is useful for indexing transactions. | ||
*/ | ||
Script.prototype.simpleInHash = function() { | ||
return util.sha256ripe160(this.simpleInPubKey()); | ||
}; | ||
/** | ||
* Old name for Script#simpleInHash. | ||
* | ||
* @deprecated | ||
*/ | ||
Script.prototype.simpleInPubKeyHash = Script.prototype.simpleInHash; | ||
@@ -318,13 +362,13 @@ /** | ||
*/ | ||
Script.createOutputScript = function(address, type) { | ||
Script.createOutputScript = function(address, network) { | ||
var script = new Script(); | ||
if ('string' === typeof address) { | ||
address = new Address(address); | ||
// null as we have a string address | ||
var address = new Address(address, null, network || Script.defaultNetwork); | ||
} | ||
if ('undefined' == typeof type) { | ||
type = 'Pubkey' | ||
} | ||
var type = address.getType(network || Script.defaultNetwork); | ||
// Standard pay-to-pubkey-hash | ||
if (type === 'Pubkey') { | ||
if (type === 'pubkeyhash') { | ||
script.writeOp(Opcode.map.OP_DUP); | ||
@@ -336,7 +380,11 @@ script.writeOp(Opcode.map.OP_HASH160); | ||
} | ||
// Standard pay-to-script-hash | ||
else if (type === 'P2SH') { | ||
else if (type === 'scripthash') { | ||
script.writeOp(Opcode.map.OP_HASH160); | ||
script.writeBytes(address.hash); | ||
script.writeOp(Opcode.map.OP_EQUAL); | ||
} else { | ||
// address can only be a pubkeyhash address or a scripthash address | ||
return false | ||
} | ||
@@ -343,0 +391,0 @@ |
{ | ||
"name": "btc-script", | ||
"version": "0.0.2", | ||
"version": "0.1.1", | ||
"description": "Script support for Bitcoin.", | ||
@@ -14,3 +14,4 @@ "keywords": [ | ||
"mocha": "1.*", | ||
"terst": "0.0.1" | ||
"terst": "0.0.1", | ||
"binstring": "~0.2.0" | ||
}, | ||
@@ -24,5 +25,6 @@ "repository": { | ||
"btc-opcode": "0.0.1", | ||
"convert-hex": "~0.1.0", | ||
"btc-address": "~0.2.0" | ||
"btc-address": "~0.4.0", | ||
"crypto-hashing": "~0.1.0", | ||
"binstring": "~0.2.0" | ||
} | ||
} |
@@ -16,7 +16,4 @@ btc-script | ||
### Script | ||
(TODO) | ||
Usage | ||
@@ -23,0 +20,0 @@ ----- |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
14152
394
0
4
3
38
+ Addedbinstring@~0.2.0
+ Addedcrypto-hashing@~0.1.0
+ Addedbinstring@0.2.1(transitive)
+ Addedbtc-address@0.4.0(transitive)
+ Addedcrypto-hashing@0.1.0(transitive)
- Removedconvert-hex@~0.1.0
- Removedbtc-address@0.2.0(transitive)
- Removedconvert-hex@0.1.0(transitive)
- Removedconvert-string@0.1.0(transitive)
- Removedsha256@0.1.1(transitive)
Updatedbtc-address@~0.4.0