* Fixes address.js to be able to deal with testnet P2SH addresses

* Enhanced address.js testsuite...now also verifies address versions
* Enhances README to show how to run test suite
This commit is contained in:
xnova 2014-03-05 16:53:29 -05:00
parent db8b38803c
commit 786198f130
4 changed files with 45 additions and 31 deletions

View file

@ -12,7 +12,7 @@ var Address = function (bytes, version) {
}
else if (typeof bytes === 'string') {
this.hash =
bytes.length <= 34 ? base58.checkDecode(bytes)
bytes.length <= 35 ? base58.checkDecode(bytes)
: bytes.length <= 40 ? conv.hexToBytes(bytes)
: util.error('Bad input');

View file

@ -2,6 +2,7 @@ var Opcode = require('./opcode');
var util = require('./util');
var conv = require('./convert');
var Address = require('./address');
var network = require('./network');
var Script = function(data) {
this.buffer = data || [];
@ -295,20 +296,20 @@ Script.prototype.writeBytes = function(data) {
Script.createOutputScript = function(address) {
var script = new Script();
address = new Address(address);
// Standard pay-to-pubkey-hash
if (!address.version) {
if (address.version == network.mainnet.p2shVersion || address.version == network.testnet.p2shVersion) {
// Standard pay-to-script-hash
script.writeOp(Opcode.map.OP_HASH160);
script.writeBytes(address.hash);
script.writeOp(Opcode.map.OP_EQUAL);
}
else {
// Standard pay-to-pubkey-hash
script.writeOp(Opcode.map.OP_DUP);
script.writeOp(Opcode.map.OP_HASH160);
script.writeBytes(address.hash);
script.writeOp(Opcode.map.OP_EQUALVERIFY);
script.writeOp(Opcode.map.OP_CHECKSIG);
}
// Standard pay-to-script-hash
else {
script.writeOp(Opcode.map.OP_HASH160);
script.writeBytes(address.hash);
script.writeOp(Opcode.map.OP_EQUAL);
}
return script;
};