Merge pull request #216 from dcousens/addrser

Network: use capitalization
This commit is contained in:
Wei Lu 2014-06-17 22:21:46 +08:00
commit 85e9723bfe
7 changed files with 26 additions and 40 deletions

View file

@ -3,24 +3,19 @@ var base58check = require('./base58check')
var networks = require('./networks') var networks = require('./networks')
var scripts = require('./scripts') var scripts = require('./scripts')
function findScriptTypeByVersion(queryVersion) { function findScriptTypeByVersion(version) {
for (var networkName in networks) { for (var networkName in networks) {
var network = networks[networkName] var network = networks[networkName]
for (var versionName in network) { if (version === network.pubKeyHash) return 'pubkeyhash'
var version = network[versionName] if (version === network.scriptHash) return 'scripthash'
if (version === queryVersion) {
return versionName
}
}
} }
} }
function Address(hash, version) { function Address(hash, version) {
assert(Buffer.isBuffer(hash), 'Expected Buffer, got ' + hash) assert(Buffer.isBuffer(hash), 'Expected Buffer, got ' + hash)
assert.strictEqual(hash.length, 20, 'Invalid hash length') 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.hash = hash
this.version = version this.version = version
@ -40,12 +35,8 @@ Address.fromOutputScript = function(script, network) {
var type = scripts.classifyOutput(script) var type = scripts.classifyOutput(script)
if (type === 'pubkeyhash') { if (type === 'pubkeyhash') return new Address(script.chunks[2], network.pubKeyHash)
return new Address(script.chunks[2], network.pubkeyhash) if (type === 'scripthash') return new Address(script.chunks[1], network.scriptHash)
} else if (type === 'scripthash') {
return new Address(script.chunks[1], network.scripthash)
}
assert(false, type + ' has no matching Address') assert(false, type + ' has no matching Address')
} }
@ -62,13 +53,8 @@ Address.prototype.toBase58Check = function () {
Address.prototype.toOutputScript = function() { Address.prototype.toOutputScript = function() {
var scriptType = findScriptTypeByVersion(this.version) var scriptType = findScriptTypeByVersion(this.version)
if (scriptType === 'pubkeyhash') { if (scriptType === 'pubkeyhash') return scripts.pubKeyHashOutput(this.hash)
return scripts.pubKeyHashOutput(this.hash) if (scriptType === 'scripthash') return scripts.scriptHashOutput(this.hash)
} else if (scriptType === 'scripthash') {
return scripts.scriptHashOutput(this.hash)
}
assert(false, this.toString() + ' has no matching Script') assert(false, this.toString() + ' has no matching Script')
} }

View file

@ -32,7 +32,7 @@ ECPubKey.fromHex = function(hex) {
ECPubKey.prototype.getAddress = function(network) { ECPubKey.prototype.getAddress = function(network) {
network = network || networks.bitcoin 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) { ECPubKey.prototype.verify = function(hash, signature) {

View file

@ -14,7 +14,7 @@ var ecurve = require('ecurve')
var ecparams = ecurve.getCurveByName('secp256k1') var ecparams = ecurve.getCurveByName('secp256k1')
function magicHash(message, network) { function magicHash(message, network) {
var magicPrefix = new Buffer(network.magicprefix) var magicPrefix = new Buffer(network.magicPrefix)
var messageBuffer = new Buffer(message) var messageBuffer = new Buffer(message)
var lengthBuffer = new Buffer(bufferutils.varIntSize(messageBuffer.length)) var lengthBuffer = new Buffer(bufferutils.varIntSize(messageBuffer.length))
bufferutils.writeVarInt(lengthBuffer, messageBuffer.length, 0) bufferutils.writeVarInt(lengthBuffer, messageBuffer.length, 0)

View file

@ -2,43 +2,43 @@
// Dogecoin BIP32 is a proposed standard: https://bitcointalk.org/index.php?topic=409731 // Dogecoin BIP32 is a proposed standard: https://bitcointalk.org/index.php?topic=409731
module.exports = { module.exports = {
bitcoin: { bitcoin: {
magicprefix: '\x18Bitcoin Signed Message:\n', magicPrefix: '\x18Bitcoin Signed Message:\n',
bip32: { bip32: {
public: 0x0488b21e, public: 0x0488b21e,
private: 0x0488ade4 private: 0x0488ade4
}, },
pubkeyhash: 0x00, pubKeyHash: 0x00,
scripthash: 0x05, scriptHash: 0x05,
wif: 0x80 wif: 0x80
}, },
dogecoin: { dogecoin: {
magicprefix: '\x19Dogecoin Signed Message:\n', magicPrefix: '\x19Dogecoin Signed Message:\n',
bip32: { bip32: {
public: 0x02facafd, public: 0x02facafd,
private: 0x02fac398 private: 0x02fac398
}, },
pubkeyhash: 0x1e, pubKeyHash: 0x1e,
scripthash: 0x16, scriptHash: 0x16,
wif: 0x9e wif: 0x9e
}, },
litecoin: { litecoin: {
magicprefix: '\x19Litecoin Signed Message:\n', magicPrefix: '\x19Litecoin Signed Message:\n',
bip32: { bip32: {
public: 0x019da462, public: 0x019da462,
private: 0x019d9cfe private: 0x019d9cfe
}, },
pubkeyhash: 0x30, pubKeyHash: 0x30,
scripthash: 0x05, scriptHash: 0x05,
wif: 0xb0 wif: 0xb0
}, },
testnet: { testnet: {
magicprefix: '\x18Bitcoin Signed Message:\n', magicPrefix: '\x18Bitcoin Signed Message:\n',
bip32: { bip32: {
public: 0x043587cf, public: 0x043587cf,
private: 0x04358394 private: 0x04358394
}, },
pubkeyhash: 0x6f, pubKeyHash: 0x6f,
scripthash: 0xc4, scriptHash: 0xc4,
wif: 0xef wif: 0xef
} }
} }

View file

@ -56,10 +56,10 @@ describe('Bitcoin-core', function() {
assert.equal(address.hash.toString('hex'), hex) assert.equal(address.hash.toString('hex'), hex)
if (params.addrType === 'pubkey') { if (params.addrType === 'pubkey') {
assert.equal(address.version, network.pubkeyhash) assert.equal(address.version, network.pubKeyHash)
} else if (params.addrType === 'script') { } else if (params.addrType === 'script') {
assert.equal(address.version, network.scripthash) assert.equal(address.version, network.scriptHash)
} }
}) })
}) })

View file

@ -72,7 +72,7 @@ describe('ECPubKey', function() {
var pubKey = new ECPubKey(Q) var pubKey = new ECPubKey(Q)
var address = pubKey.getAddress(networks.testnet) 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) assert.equal(address.hash.toString('hex'), fixtures.compressed.hash160)
}) })
}) })

View file

@ -204,7 +204,7 @@ describe('HDNode', function() {
var hd = HDNode.fromBase58(f.master.base58) var hd = HDNode.fromBase58(f.master.base58)
hd.network = networks.testnet hd.network = networks.testnet
assert.equal(hd.getAddress().version, networks.testnet.pubkeyhash) assert.equal(hd.getAddress().version, networks.testnet.pubKeyHash)
}) })
}) })