Address: use script ASM over hex

This commit is contained in:
Daniel Cousens 2014-11-28 14:02:19 +11:00
parent 4fe0b34f27
commit 7fbc397141
2 changed files with 24 additions and 28 deletions

View file

@ -10,22 +10,22 @@ describe('Address', function() {
describe('Constructor', function() { describe('Constructor', function() {
it('does not mutate the input', function() { it('does not mutate the input', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
var hash = new Buffer(f.hex, 'hex') var hash = new Buffer(f.hash, 'hex')
var addr = new Address(hash, f.version) var addr = new Address(hash, f.version)
assert.equal(addr.version, f.version) assert.equal(addr.version, f.version)
assert.equal(addr.hash.toString('hex'), f.hex) assert.equal(addr.hash.toString('hex'), f.hash)
}) })
}) })
}) })
describe('fromBase58Check', function() { describe('fromBase58Check', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('imports ' + f.description + '(' + f.network + ') correctly', function() { it('imports ' + f.script + ' (' + f.network + ') correctly', function() {
var addr = Address.fromBase58Check(f.base58check) var addr = Address.fromBase58Check(f.base58check)
assert.equal(addr.version, f.version) assert.equal(addr.version, f.version)
assert.equal(addr.hash.toString('hex'), f.hex) assert.equal(addr.hash.toString('hex'), f.hash)
}) })
}) })
@ -40,18 +40,18 @@ describe('Address', function() {
describe('fromOutputScript', function() { describe('fromOutputScript', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('imports ' + f.description + '(' + f.network + ') correctly', function() { it('imports ' + f.script + ' (' + f.network + ') correctly', function() {
var script = Script.fromHex(f.script) var script = Script.fromASM(f.script)
var addr = Address.fromOutputScript(script, networks[f.network]) var addr = Address.fromOutputScript(script, networks[f.network])
assert.equal(addr.version, f.version) assert.equal(addr.version, f.version)
assert.equal(addr.hash.toString('hex'), f.hex) assert.equal(addr.hash.toString('hex'), f.hash)
}) })
}) })
fixtures.invalid.fromOutputScript.forEach(function(f) { fixtures.invalid.fromOutputScript.forEach(function(f) {
it('throws when ' + f.description, function() { it('throws when ' + f.description, function() {
var script = Script.fromHex(f.hex) var script = Script.fromASM(f.script)
assert.throws(function() { assert.throws(function() {
Address.fromOutputScript(script) Address.fromOutputScript(script)
@ -62,7 +62,7 @@ describe('Address', function() {
describe('toBase58Check', function() { describe('toBase58Check', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('exports ' + f.description + '(' + f.network + ') correctly', function() { it('exports ' + f.script + ' (' + f.network + ') correctly', function() {
var addr = Address.fromBase58Check(f.base58check) var addr = Address.fromBase58Check(f.base58check)
var result = addr.toBase58Check() var result = addr.toBase58Check()
@ -73,17 +73,17 @@ describe('Address', function() {
describe('toOutputScript', function() { describe('toOutputScript', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('imports ' + f.description + '(' + f.network + ') correctly', function() { it('imports ' + f.script + ' (' + f.network + ') correctly', function() {
var addr = Address.fromBase58Check(f.base58check) var addr = Address.fromBase58Check(f.base58check)
var script = addr.toOutputScript() var script = addr.toOutputScript()
assert.equal(script.toHex(), f.script) assert.equal(script.toASM(), f.script)
}) })
}) })
fixtures.invalid.toOutputScript.forEach(function(f) { fixtures.invalid.toOutputScript.forEach(function(f) {
it('throws when ' + f.description, function() { it('throws when ' + f.description, function() {
var addr = new Address(new Buffer(f.hex, 'hex'), f.version) var addr = new Address(new Buffer(f.hash, 'hex'), f.version)
assert.throws(function() { assert.throws(function() {
addr.toOutputScript() addr.toOutputScript()

View file

@ -1,36 +1,32 @@
{ {
"valid": [ "valid": [
{ {
"description": "pubKeyHash",
"network": "bitcoin", "network": "bitcoin",
"version": 0, "version": 0,
"hex": "751e76e8199196d454941c45d1b3a323f1433bd6", "hash": "751e76e8199196d454941c45d1b3a323f1433bd6",
"base58check": "1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH", "base58check": "1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH",
"script": "76a914751e76e8199196d454941c45d1b3a323f1433bd688ac" "script": "OP_DUP OP_HASH160 751e76e8199196d454941c45d1b3a323f1433bd6 OP_EQUALVERIFY OP_CHECKSIG"
}, },
{ {
"description": "scriptHash",
"network": "bitcoin", "network": "bitcoin",
"version": 5, "version": 5,
"hex": "cd7b44d0b03f2d026d1e586d7ae18903b0d385f6", "hash": "cd7b44d0b03f2d026d1e586d7ae18903b0d385f6",
"base58check": "3LRW7jeCvQCRdPF8S3yUCfRAx4eqXFmdcr", "base58check": "3LRW7jeCvQCRdPF8S3yUCfRAx4eqXFmdcr",
"script": "a914cd7b44d0b03f2d026d1e586d7ae18903b0d385f687" "script": "OP_HASH160 cd7b44d0b03f2d026d1e586d7ae18903b0d385f6 OP_EQUAL"
}, },
{ {
"description": "pubKeyHash",
"network": "testnet", "network": "testnet",
"version": 111, "version": 111,
"hex": "751e76e8199196d454941c45d1b3a323f1433bd6", "hash": "751e76e8199196d454941c45d1b3a323f1433bd6",
"base58check": "mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r", "base58check": "mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r",
"script": "76a914751e76e8199196d454941c45d1b3a323f1433bd688ac" "script": "OP_DUP OP_HASH160 751e76e8199196d454941c45d1b3a323f1433bd6 OP_EQUALVERIFY OP_CHECKSIG"
}, },
{ {
"description": "scriptHash",
"network": "testnet", "network": "testnet",
"version": 196, "version": 196,
"hex": "cd7b44d0b03f2d026d1e586d7ae18903b0d385f6", "hash": "cd7b44d0b03f2d026d1e586d7ae18903b0d385f6",
"base58check": "2NByiBUaEXrhmqAsg7BbLpcQSAQs1EDwt5w", "base58check": "2NByiBUaEXrhmqAsg7BbLpcQSAQs1EDwt5w",
"script": "a914cd7b44d0b03f2d026d1e586d7ae18903b0d385f687" "script": "OP_HASH160 cd7b44d0b03f2d026d1e586d7ae18903b0d385f6 OP_EQUAL"
} }
], ],
"invalid": { "invalid": {
@ -49,21 +45,21 @@
"fromOutputScript": [ "fromOutputScript": [
{ {
"description": "has no matching Address", "description": "has no matching Address",
"hex": "21031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95ac" "script": "031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95 OP_CHECKSIG"
}, },
{ {
"description": "has no matching Address", "description": "has no matching Address",
"hex": "5121032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca330162102308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a52ae" "script": "OP_TRUE 032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca33016 02308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a OP_2 OP_CHECKMULTISIG"
}, },
{ {
"description": "has no matching Address", "description": "has no matching Address",
"hex": "6a2606deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474" "script": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474"
} }
], ],
"toOutputScript": [ "toOutputScript": [
{ {
"description": "24kPZCmVgzfkpGdXExy56234MRHrsqQxNWE has no matching Script", "description": "24kPZCmVgzfkpGdXExy56234MRHrsqQxNWE has no matching Script",
"hex": "751e76e8199196d454941c45d1b3a323f1433bd6", "hash": "751e76e8199196d454941c45d1b3a323f1433bd6",
"version": 153 "version": 153
} }
] ]