Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 8 additions & 22 deletions src/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,19 @@ var base58check = require('./base58check')
var networks = require('./networks')
var scripts = require('./scripts')

function findScriptTypeByVersion(queryVersion) {
function findScriptTypeByVersion(version) {
for (var networkName in networks) {
var network = networks[networkName]

for (var versionName in network) {
var version = network[versionName]

if (version === queryVersion) {
return versionName
}
}
if (version === network.pubKeyHash) return 'pubkeyhash'
if (version === network.scriptHash) return 'scripthash'
}
}

function Address(hash, version) {
assert(Buffer.isBuffer(hash), 'Expected Buffer, got ' + hash)
assert.strictEqual(hash.length, 20, 'Invalid hash length')
assert.strictEqual(version & 0xFF, version, 'Invalid version byte')
assert.strictEqual(version & 0xff, version, 'Invalid version byte')

this.hash = hash
this.version = version
Expand All @@ -40,12 +35,8 @@ Address.fromOutputScript = function(script, network) {

var type = scripts.classifyOutput(script)

if (type === 'pubkeyhash') {
return new Address(script.chunks[2], network.pubkeyhash)

} else if (type === 'scripthash') {
return new Address(script.chunks[1], network.scripthash)
}
if (type === 'pubkeyhash') return new Address(script.chunks[2], network.pubKeyHash)
if (type === 'scripthash') return new Address(script.chunks[1], network.scriptHash)

assert(false, type + ' has no matching Address')
}
Expand All @@ -62,13 +53,8 @@ Address.prototype.toBase58Check = function () {
Address.prototype.toOutputScript = function() {
var scriptType = findScriptTypeByVersion(this.version)

if (scriptType === 'pubkeyhash') {
return scripts.pubKeyHashOutput(this.hash)

} else if (scriptType === 'scripthash') {
return scripts.scriptHashOutput(this.hash)

}
if (scriptType === 'pubkeyhash') return scripts.pubKeyHashOutput(this.hash)
if (scriptType === 'scripthash') return scripts.scriptHashOutput(this.hash)

assert(false, this.toString() + ' has no matching Script')
}
Expand Down
2 changes: 1 addition & 1 deletion src/ecpubkey.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ECPubKey.fromHex = function(hex) {
ECPubKey.prototype.getAddress = function(network) {
network = network || networks.bitcoin

return new Address(crypto.hash160(this.toBuffer()), network.pubkeyhash)
return new Address(crypto.hash160(this.toBuffer()), network.pubKeyHash)
}

ECPubKey.prototype.verify = function(hash, signature) {
Expand Down
2 changes: 1 addition & 1 deletion src/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var ecurve = require('ecurve')
var ecparams = ecurve.getCurveByName('secp256k1')

function magicHash(message, network) {
var magicPrefix = new Buffer(network.magicprefix)
var magicPrefix = new Buffer(network.magicPrefix)
var messageBuffer = new Buffer(message)
var lengthBuffer = new Buffer(bufferutils.varIntSize(messageBuffer.length))
bufferutils.writeVarInt(lengthBuffer, messageBuffer.length, 0)
Expand Down
24 changes: 12 additions & 12 deletions src/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,43 @@
// Dogecoin BIP32 is a proposed standard: https://bitcointalk.org/index.php?topic=409731
module.exports = {
bitcoin: {
magicprefix: '\x18Bitcoin Signed Message:\n',
magicPrefix: '\x18Bitcoin Signed Message:\n',
bip32: {
public: 0x0488b21e,
private: 0x0488ade4
},
pubkeyhash: 0x00,
scripthash: 0x05,
pubKeyHash: 0x00,
scriptHash: 0x05,
wif: 0x80
},
dogecoin: {
magicprefix: '\x19Dogecoin Signed Message:\n',
magicPrefix: '\x19Dogecoin Signed Message:\n',
bip32: {
public: 0x02facafd,
private: 0x02fac398
},
pubkeyhash: 0x1e,
scripthash: 0x16,
pubKeyHash: 0x1e,
scriptHash: 0x16,
wif: 0x9e
},
litecoin: {
magicprefix: '\x19Litecoin Signed Message:\n',
magicPrefix: '\x19Litecoin Signed Message:\n',
bip32: {
public: 0x019da462,
private: 0x019d9cfe
},
pubkeyhash: 0x30,
scripthash: 0x05,
pubKeyHash: 0x30,
scriptHash: 0x05,
wif: 0xb0
},
testnet: {
magicprefix: '\x18Bitcoin Signed Message:\n',
magicPrefix: '\x18Bitcoin Signed Message:\n',
bip32: {
public: 0x043587cf,
private: 0x04358394
},
pubkeyhash: 0x6f,
scripthash: 0xc4,
pubKeyHash: 0x6f,
scriptHash: 0xc4,
wif: 0xef
}
}
4 changes: 2 additions & 2 deletions test/bitcoin.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ describe('Bitcoin-core', function() {

assert.equal(address.hash.toString('hex'), hex)
if (params.addrType === 'pubkey') {
assert.equal(address.version, network.pubkeyhash)
assert.equal(address.version, network.pubKeyHash)

} else if (params.addrType === 'script') {
assert.equal(address.version, network.scripthash)
assert.equal(address.version, network.scriptHash)
}
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/ecpubkey.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('ECPubKey', function() {
var pubKey = new ECPubKey(Q)
var address = pubKey.getAddress(networks.testnet)

assert.equal(address.version, networks.testnet.pubkeyhash)
assert.equal(address.version, networks.testnet.pubKeyHash)
assert.equal(address.hash.toString('hex'), fixtures.compressed.hash160)
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/hdnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ describe('HDNode', function() {
var hd = HDNode.fromBase58(f.master.base58)
hd.network = networks.testnet

assert.equal(hd.getAddress().version, networks.testnet.pubkeyhash)
assert.equal(hd.getAddress().version, networks.testnet.pubKeyHash)
})
})

Expand Down