remove Address constructor
This commit is contained in:
parent
340e7fb917
commit
13f414c122
6 changed files with 51 additions and 68 deletions
|
@ -1,4 +1,3 @@
|
||||||
var assert = require('assert')
|
|
||||||
var base58check = require('bs58check')
|
var base58check = require('bs58check')
|
||||||
var typeForce = require('typeforce')
|
var typeForce = require('typeforce')
|
||||||
var networks = require('./networks')
|
var networks = require('./networks')
|
||||||
|
@ -13,50 +12,52 @@ function findScriptTypeByVersion (version) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Address (hash, version) {
|
function fromBase58Check (string) {
|
||||||
typeForce('Buffer', hash)
|
|
||||||
|
|
||||||
assert.strictEqual(hash.length, 20, 'Invalid hash length')
|
|
||||||
assert.strictEqual(version & 0xff, version, 'Invalid version byte')
|
|
||||||
|
|
||||||
this.hash = hash
|
|
||||||
this.version = version
|
|
||||||
}
|
|
||||||
|
|
||||||
Address.fromBase58Check = function (string) {
|
|
||||||
var payload = base58check.decode(string)
|
var payload = base58check.decode(string)
|
||||||
var version = payload.readUInt8(0)
|
var version = payload.readUInt8(0)
|
||||||
var hash = payload.slice(1)
|
var hash = payload.slice(1)
|
||||||
|
|
||||||
return new Address(hash, version)
|
if (hash.length !== 20) throw new TypeError('Invalid hash length')
|
||||||
|
if (version & ~0xff) throw new TypeError('Invalid version byte')
|
||||||
|
|
||||||
|
return { hash: hash, version: version }
|
||||||
}
|
}
|
||||||
|
|
||||||
Address.fromOutputScript = function (script, network) {
|
function fromOutputScript (script, network) {
|
||||||
network = network || networks.bitcoin
|
network = network || networks.bitcoin
|
||||||
|
|
||||||
if (scripts.isPubKeyHashOutput(script)) return new Address(script.chunks[2], network.pubKeyHash)
|
if (scripts.isPubKeyHashOutput(script)) return toBase58Check(script.chunks[2], network.pubKeyHash)
|
||||||
if (scripts.isScriptHashOutput(script)) return new Address(script.chunks[1], network.scriptHash)
|
if (scripts.isScriptHashOutput(script)) return toBase58Check(script.chunks[1], network.scriptHash)
|
||||||
|
|
||||||
throw new Error(script.toASM() + ' has no matching Address')
|
throw new Error(script.toASM() + ' has no matching Address')
|
||||||
}
|
}
|
||||||
|
|
||||||
Address.prototype.toBase58Check = function () {
|
function toBase58Check (hash, version) {
|
||||||
|
typeForce('Buffer', hash)
|
||||||
|
|
||||||
|
if (hash.length !== 20) throw new TypeError('Invalid hash length')
|
||||||
|
if (version & ~0xff) throw new TypeError('Invalid version byte')
|
||||||
|
|
||||||
var payload = new Buffer(21)
|
var payload = new Buffer(21)
|
||||||
payload.writeUInt8(this.version, 0)
|
payload.writeUInt8(version, 0)
|
||||||
this.hash.copy(payload, 1)
|
hash.copy(payload, 1)
|
||||||
|
|
||||||
return base58check.encode(payload)
|
return base58check.encode(payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
Address.prototype.toOutputScript = function () {
|
function toOutputScript (address) {
|
||||||
var scriptType = findScriptTypeByVersion(this.version)
|
var decode = fromBase58Check(address)
|
||||||
|
var scriptType = findScriptTypeByVersion(decode.version)
|
||||||
|
|
||||||
if (scriptType === 'pubkeyhash') return scripts.pubKeyHashOutput(this.hash)
|
if (scriptType === 'pubkeyhash') return scripts.pubKeyHashOutput(decode.hash)
|
||||||
if (scriptType === 'scripthash') return scripts.scriptHashOutput(this.hash)
|
if (scriptType === 'scripthash') return scripts.scriptHashOutput(decode.hash)
|
||||||
|
|
||||||
throw new Error(this.toString() + ' has no matching Script')
|
throw new Error(address + ' has no matching Script')
|
||||||
}
|
}
|
||||||
|
|
||||||
Address.prototype.toString = Address.prototype.toBase58Check
|
module.exports = {
|
||||||
|
fromBase58Check: fromBase58Check,
|
||||||
module.exports = Address
|
fromOutputScript: fromOutputScript,
|
||||||
|
toBase58Check: toBase58Check,
|
||||||
|
toOutputScript: toOutputScript
|
||||||
|
}
|
||||||
|
|
|
@ -37,7 +37,7 @@ function extractInput (txIn) {
|
||||||
hashType = parsed.hashType
|
hashType = parsed.hashType
|
||||||
pubKeys = scriptSig.chunks.slice(1)
|
pubKeys = scriptSig.chunks.slice(1)
|
||||||
signatures = [parsed.signature]
|
signatures = [parsed.signature]
|
||||||
prevOutScript = Address.fromBase58Check(ECPair.fromPublicKeyBuffer(pubKeys[0]).getAddress()).toOutputScript()
|
prevOutScript = Address.toOutputScript(ECPair.fromPublicKeyBuffer(pubKeys[0]).getAddress())
|
||||||
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -188,7 +188,7 @@ TransactionBuilder.prototype.addOutput = function (scriptPubKey, value) {
|
||||||
|
|
||||||
// Attempt to get a valid address if it's a base58 address string
|
// Attempt to get a valid address if it's a base58 address string
|
||||||
if (typeof scriptPubKey === 'string') {
|
if (typeof scriptPubKey === 'string') {
|
||||||
scriptPubKey = Address.fromBase58Check(scriptPubKey).toOutputScript()
|
scriptPubKey = Address.toOutputScript(scriptPubKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.tx.addOutput(scriptPubKey, value)
|
return this.tx.addOutput(scriptPubKey, value)
|
||||||
|
@ -358,7 +358,7 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
|
||||||
|
|
||||||
// we know nothin' Jon Snow, assume pubKeyHash
|
// we know nothin' Jon Snow, assume pubKeyHash
|
||||||
} else {
|
} else {
|
||||||
input.prevOutScript = Address.fromBase58Check(keyPair.getAddress()).toOutputScript()
|
input.prevOutScript = Address.toOutputScript(keyPair.getAddress())
|
||||||
input.prevOutType = 'pubkeyhash'
|
input.prevOutType = 'pubkeyhash'
|
||||||
input.pubKeys = [kpPubKey]
|
input.pubKeys = [kpPubKey]
|
||||||
input.scriptType = input.prevOutType
|
input.scriptType = input.prevOutType
|
||||||
|
|
|
@ -9,25 +9,13 @@ var Script = require('../src/script')
|
||||||
var fixtures = require('./fixtures/address.json')
|
var fixtures = require('./fixtures/address.json')
|
||||||
|
|
||||||
describe('Address', function () {
|
describe('Address', function () {
|
||||||
describe('Constructor', function () {
|
|
||||||
it('does not mutate the input', function () {
|
|
||||||
fixtures.valid.forEach(function (f) {
|
|
||||||
var hash = new Buffer(f.hash, 'hex')
|
|
||||||
var addr = new Address(hash, f.version)
|
|
||||||
|
|
||||||
assert.strictEqual(addr.version, f.version)
|
|
||||||
assert.strictEqual(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.script + ' (' + f.network + ') correctly', function () {
|
it('decodes ' + f.base58check, function () {
|
||||||
var addr = Address.fromBase58Check(f.base58check)
|
var decode = Address.fromBase58Check(f.base58check)
|
||||||
|
|
||||||
assert.strictEqual(addr.version, f.version)
|
assert.strictEqual(decode.version, f.version)
|
||||||
assert.strictEqual(addr.hash.toString('hex'), f.hash)
|
assert.strictEqual(decode.hash.toString('hex'), f.hash)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -42,12 +30,11 @@ describe('Address', function () {
|
||||||
|
|
||||||
describe('fromOutputScript', function () {
|
describe('fromOutputScript', function () {
|
||||||
fixtures.valid.forEach(function (f) {
|
fixtures.valid.forEach(function (f) {
|
||||||
it('imports ' + f.script + ' (' + f.network + ') correctly', function () {
|
it('parses ' + f.script + ' (' + f.network + ')', function () {
|
||||||
var script = Script.fromASM(f.script)
|
var script = Script.fromASM(f.script)
|
||||||
var addr = Address.fromOutputScript(script, networks[f.network])
|
var address = Address.fromOutputScript(script, networks[f.network])
|
||||||
|
|
||||||
assert.strictEqual(addr.version, f.version)
|
assert.strictEqual(address, f.base58check)
|
||||||
assert.strictEqual(addr.hash.toString('hex'), f.hash)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -64,20 +51,18 @@ describe('Address', function () {
|
||||||
|
|
||||||
describe('toBase58Check', function () {
|
describe('toBase58Check', function () {
|
||||||
fixtures.valid.forEach(function (f) {
|
fixtures.valid.forEach(function (f) {
|
||||||
it('exports ' + f.script + ' (' + f.network + ') correctly', function () {
|
it('formats ' + f.hash + ' (' + f.network + ')', function () {
|
||||||
var addr = Address.fromBase58Check(f.base58check)
|
var address = Address.toBase58Check(new Buffer(f.hash, 'hex'), f.version)
|
||||||
var result = addr.toBase58Check()
|
|
||||||
|
|
||||||
assert.strictEqual(result, f.base58check)
|
assert.strictEqual(address, f.base58check)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('toOutputScript', function () {
|
describe('toOutputScript', function () {
|
||||||
fixtures.valid.forEach(function (f) {
|
fixtures.valid.forEach(function (f) {
|
||||||
it('imports ' + f.script + ' (' + f.network + ') correctly', function () {
|
it('exports ' + f.script + '(' + f.network + ')', function () {
|
||||||
var addr = Address.fromBase58Check(f.base58check)
|
var script = Address.toOutputScript(f.base58check)
|
||||||
var script = addr.toOutputScript()
|
|
||||||
|
|
||||||
assert.strictEqual(script.toASM(), f.script)
|
assert.strictEqual(script.toASM(), f.script)
|
||||||
})
|
})
|
||||||
|
@ -85,10 +70,8 @@ describe('Address', function () {
|
||||||
|
|
||||||
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.hash, 'hex'), f.version)
|
|
||||||
|
|
||||||
assert.throws(function () {
|
assert.throws(function () {
|
||||||
addr.toOutputScript()
|
Address.toOutputScript(f.address)
|
||||||
}, new RegExp(f.description))
|
}, new RegExp(f.description))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -65,10 +65,10 @@ describe('Bitcoin-core', function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
it('can import ' + string, function () {
|
it('can import ' + string, function () {
|
||||||
var address = Address.fromBase58Check(string)
|
var decode = Address.fromBase58Check(string)
|
||||||
|
|
||||||
assert.strictEqual(address.hash.toString('hex'), hex)
|
assert.strictEqual(decode.hash.toString('hex'), hex)
|
||||||
assert.strictEqual(address.version, network[typeMap[params.addrType]])
|
assert.strictEqual(decode.version, network[typeMap[params.addrType]])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
3
test/fixtures/address.json
vendored
3
test/fixtures/address.json
vendored
|
@ -59,8 +59,7 @@
|
||||||
"toOutputScript": [
|
"toOutputScript": [
|
||||||
{
|
{
|
||||||
"description": "24kPZCmVgzfkpGdXExy56234MRHrsqQxNWE has no matching Script",
|
"description": "24kPZCmVgzfkpGdXExy56234MRHrsqQxNWE has no matching Script",
|
||||||
"hash": "751e76e8199196d454941c45d1b3a323f1433bd6",
|
"address": "24kPZCmVgzfkpGdXExy56234MRHrsqQxNWE"
|
||||||
"version": 153
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,13 +65,13 @@ describe('TransactionBuilder', function () {
|
||||||
txb = new TransactionBuilder()
|
txb = new TransactionBuilder()
|
||||||
|
|
||||||
prevTx = new Transaction()
|
prevTx = new Transaction()
|
||||||
prevTx.addOutput(Address.fromBase58Check('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH').toOutputScript(), 0)
|
prevTx.addOutput(Address.toOutputScript('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH'), 0)
|
||||||
prevTx.addOutput(Address.fromBase58Check('1cMh228HTCiwS8ZsaakH8A8wze1JR5ZsP').toOutputScript(), 1)
|
prevTx.addOutput(Address.toOutputScript('1cMh228HTCiwS8ZsaakH8A8wze1JR5ZsP'), 1)
|
||||||
prevTxHash = prevTx.getHash()
|
prevTxHash = prevTx.getHash()
|
||||||
|
|
||||||
keyPair = new ECPair(BigInteger.ONE)
|
keyPair = new ECPair(BigInteger.ONE)
|
||||||
privAddress = keyPair.getAddress()
|
privAddress = keyPair.getAddress()
|
||||||
privScript = Address.fromBase58Check(privAddress).toOutputScript()
|
privScript = Address.toOutputScript(privAddress)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('addInput', function () {
|
describe('addInput', function () {
|
||||||
|
|
Loading…
Add table
Reference in a new issue