diff --git a/lib/http/rpc.js b/lib/http/rpc.js index 6de854fb..fd5c861b 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -1605,7 +1605,7 @@ RPC.prototype.createRawTransaction = co(function* createRawTransaction(args, hel } address = parseAddress(key, this.network); - b58 = address.toBase58(this.network); + b58 = address.toString(this.network); if (addrs[b58]) throw new RPCError(errs.INVALID_PARAMETER, 'Duplicate address'); @@ -1659,7 +1659,7 @@ RPC.prototype.decodeScript = co(function* decodeScript(args, help) { address = Address.fromScripthash(script.hash160()); script = this.scriptToJSON(script); - script.p2sh = address.toBase58(this.network); + script.p2sh = address.toString(this.network); return script; }); @@ -1873,7 +1873,7 @@ RPC.prototype.createMultisig = co(function* createMultisig(args, help) { address = script.getAddress(); return { - address: address.toBase58(this.network), + address: address.toString(this.network), redeemScript: script.toJSON() }; }); @@ -1894,7 +1894,7 @@ RPC.prototype.createWitnessAddress = co(function* createWitnessAddress(args, hel address = program.getAddress(); return { - address: address.toBase58(this.network), + address: address.toString(this.network), witnessScript: program.toJSON() }; }); @@ -1908,7 +1908,7 @@ RPC.prototype.validateAddress = co(function* validateAddress(args, help) { throw new RPCError(errs.MISC_ERROR, 'validateaddress "bitcoinaddress"'); try { - address = Address.fromBase58(b58, this.network); + address = Address.fromString(b58, this.network); } catch (e) { return { isvalid: false @@ -1919,7 +1919,7 @@ RPC.prototype.validateAddress = co(function* validateAddress(args, help) { return { isvalid: true, - address: address.toBase58(this.network), + address: address.toString(this.network), scriptPubKey: script.toJSON(), ismine: false, iswatchonly: false @@ -2548,7 +2548,7 @@ RPC.prototype.scriptToJSON = function scriptToJSON(script, hex) { out.reqSigs = script.getSmall(0); if (address) { - address = address.toBase58(this.network); + address = address.toString(this.network); out.addresses.push(address); } @@ -2683,7 +2683,7 @@ function Nonces(n1, n2) { function parseAddress(raw, network) { try { - return Address.fromBase58(raw, network); + return Address.fromString(raw, network); } catch (e) { throw new RPCError(errs.INVALID_ADDRESS_OR_KEY, 'Invalid address.'); } diff --git a/lib/primitives/address.js b/lib/primitives/address.js index 8fab8695..84c51df4 100644 --- a/lib/primitives/address.js +++ b/lib/primitives/address.js @@ -267,19 +267,20 @@ Address.prototype.fromString = function fromString(addr, network) { * @returns {Address} */ -Address.fromString = function fromString(addr) { - return this.fromString(addr); +Address.fromString = function fromString(addr, network) { + return new Address().fromString(addr, network); }; /** * Convert the Address to a string. + * @param {(Network|NetworkType)?} network * @returns {Base58Address} */ -Address.prototype.toString = function toString() { +Address.prototype.toString = function toString(network) { if (this.version !== -1) - return this.toBech32(); - return this.toBase58(); + return this.toBech32(network); + return this.toBase58(network); }; /** @@ -291,7 +292,7 @@ Address.prototype.inspect = function inspect() { return ''; }; @@ -857,7 +858,7 @@ Address.getHash = function getHash(data, enc) { return enc === 'hex' ? data : new Buffer(data, 'hex'); try { - hash = Address.fromBase58(data).hash; + hash = Address.fromString(data).hash; } catch (e) { return; } diff --git a/lib/primitives/coin.js b/lib/primitives/coin.js index 9a95ce78..02003980 100644 --- a/lib/primitives/coin.js +++ b/lib/primitives/coin.js @@ -241,7 +241,7 @@ Coin.prototype.getJSON = function getJSON(network, minimal) { network = Network.get(network); if (address) - address = address.toBase58(network); + address = address.toString(network); return { version: this.version, diff --git a/lib/primitives/input.js b/lib/primitives/input.js index 45091406..8724f8c3 100644 --- a/lib/primitives/input.js +++ b/lib/primitives/input.js @@ -292,7 +292,7 @@ Input.prototype.getJSON = function getJSON(network, coin) { if (!coin) { address = this.getAddress(); if (address) - address = address.toBase58(network); + address = address.toString(network); } return { diff --git a/lib/primitives/keyring.js b/lib/primitives/keyring.js index c78e1a11..8886ae37 100644 --- a/lib/primitives/keyring.js +++ b/lib/primitives/keyring.js @@ -491,6 +491,9 @@ KeyRing.prototype.getNestedAddress = function getNestedAddress(enc) { if (enc === 'base58') return this._nestedAddress.toBase58(); + if (enc === 'string') + return this._nestedAddress.toString(); + return this._nestedAddress; }; @@ -568,6 +571,9 @@ KeyRing.prototype.getScriptAddress = function getScriptAddress(enc) { if (enc === 'base58') return this._scriptAddress.toBase58(); + if (enc === 'string') + return this._scriptAddress.toString(); + return this._scriptAddress; }; @@ -607,6 +613,9 @@ KeyRing.prototype.getKeyAddress = function getKeyAddress(enc) { if (enc === 'base58') return this._keyAddress.toBase58(); + if (enc === 'string') + return this._keyAddress.toString(); + return this._keyAddress; }; @@ -789,7 +798,7 @@ KeyRing.prototype.toJSON = function toJSON() { script: this.script ? this.script.toRaw().toString('hex') : null, program: this.witness ? this.getProgram().toRaw().toString('hex') : null, type: Address.typesByVal[this.getType()].toLowerCase(), - address: this.getAddress('base58') + address: this.getAddress('string') }; }; diff --git a/lib/primitives/mtx.js b/lib/primitives/mtx.js index 7928d779..977c6753 100644 --- a/lib/primitives/mtx.js +++ b/lib/primitives/mtx.js @@ -1563,7 +1563,7 @@ CoinSelector.prototype.fromOptions = function fromOptions(options) { if (options.changeAddress) { addr = options.changeAddress; if (typeof addr === 'string') { - this.changeAddress = Address.fromBase58(addr); + this.changeAddress = Address.fromString(addr); } else { assert(addr instanceof Address); this.changeAddress = addr; diff --git a/lib/primitives/output.js b/lib/primitives/output.js index 2d37a6f4..89608d4e 100644 --- a/lib/primitives/output.js +++ b/lib/primitives/output.js @@ -81,7 +81,7 @@ Output.fromOptions = function fromOptions(options) { Output.prototype.fromScript = function fromScript(script, value) { if (typeof script === 'string') - script = Address.fromBase58(script); + script = Address.fromString(script); if (script instanceof Address) script = Script.fromAddress(script); @@ -186,7 +186,7 @@ Output.prototype.getJSON = function getJSON(network) { network = Network.get(network); if (address) - address = address.toBase58(network); + address = address.toString(network); return { value: Amount.btc(this.value), diff --git a/lib/script/script.js b/lib/script/script.js index 21eeb47a..344ee5dd 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -1856,7 +1856,7 @@ Script.fromProgram = function fromProgram(version, data) { Script.prototype.fromAddress = function fromAddress(address) { if (typeof address === 'string') - address = Address.fromBase58(address); + address = Address.fromString(address); assert(address instanceof Address, 'Not an address.'); diff --git a/lib/wallet/account.js b/lib/wallet/account.js index 10c20724..f0173628 100644 --- a/lib/wallet/account.js +++ b/lib/wallet/account.js @@ -891,13 +891,13 @@ Account.prototype.toJSON = function toJSON(minimal) { nestedDepth: this.nestedDepth, lookahead: this.lookahead, receiveAddress: this.receive - ? this.receive.getAddress('base58') + ? this.receive.getAddress('string') : null, nestedAddress: this.nested - ? this.nested.getAddress('base58') + ? this.nested.getAddress('string') : null, changeAddress: this.change - ? this.change.getAddress('base58') + ? this.change.getAddress('string') : null, accountKey: this.accountKey.toBase58(), keys: this.keys.map(function(key) { diff --git a/lib/wallet/rpc.js b/lib/wallet/rpc.js index 388b9bba..cf25834d 100644 --- a/lib/wallet/rpc.js +++ b/lib/wallet/rpc.js @@ -286,7 +286,7 @@ RPC.prototype.dumpWallet = co(function* dumpWallet(args, help) { if (!ring) continue; - addr = ring.getAddress('base58'); + addr = ring.getAddress('string'); fmt = '%s %s label= addr=%s'; if (ring.branch === 1) @@ -353,7 +353,7 @@ RPC.prototype.getAccountAddress = co(function* getAccountAddress(args, help) { if (!account) return ''; - return account.receive.getAddress('base58'); + return account.receive.getAddress('string'); }); RPC.prototype.getAccount = co(function* getAccount(args, help) { @@ -396,7 +396,7 @@ RPC.prototype.getAddressesByAccount = co(function* getAddressesByAccount(args, h for (i = 0; i < paths.length; i++) { path = paths[i]; address = path.toAddress(); - addrs.push(address.toBase58(this.network)); + addrs.push(address.toString(this.network)); } return addrs; @@ -448,7 +448,7 @@ RPC.prototype.getNewAddress = co(function* getNewAddress(args, help) { address = yield wallet.createReceive(name); - return address.getAddress('base58'); + return address.getAddress('string'); }); RPC.prototype.getRawChangeAddress = co(function* getRawChangeAddress(args, help) { @@ -460,7 +460,7 @@ RPC.prototype.getRawChangeAddress = co(function* getRawChangeAddress(args, help) address = yield wallet.createChange(); - return address.getAddress('base58'); + return address.getAddress('string'); }); RPC.prototype.getReceivedByAccount = co(function* getReceivedByAccount(args, help) { @@ -578,7 +578,7 @@ RPC.prototype._toWalletTX = co(function* _toWalletTX(wtx) { det.push({ account: member.path.name, - address: member.address.toBase58(this.network), + address: member.address.toString(this.network), category: 'receive', amount: Amount.btc(member.value, true), label: member.path.name, @@ -596,7 +596,7 @@ RPC.prototype._toWalletTX = co(function* _toWalletTX(wtx) { det.push({ account: '', address: member.address - ? member.address.toBase58(this.network) + ? member.address.toString(this.network) : null, category: 'send', amount: -(Amount.btc(member.value, true)), @@ -947,7 +947,7 @@ RPC.prototype._listReceived = co(function* _listReceived(minconf, empty, watchOn address = path.toAddress(); map[path.hash] = { involvesWatchonly: wallet.watchOnly, - address: address.toBase58(this.network), + address: address.toString(this.network), account: path.name, amount: 0, confirmations: -1, @@ -978,7 +978,7 @@ RPC.prototype._listReceived = co(function* _listReceived(minconf, empty, watchOn if (entry) { if (entry.confirmations === -1 || conf < entry.confirmations) entry.confirmations = conf; - entry.address = address.toBase58(this.network); + entry.address = address.toString(this.network); entry.amount += output.value; } } @@ -1142,7 +1142,7 @@ RPC.prototype._toListTX = co(function* _toListTX(wtx) { return { account: member.path ? member.path.name : '', address: member.address - ? member.address.toBase58(this.network) + ? member.address.toString(this.network) : null, category: receive ? 'receive' : 'send', amount: Amount.btc(receive ? received : -sent, true), @@ -1257,7 +1257,7 @@ RPC.prototype.listUnspent = co(function* listUnspent(args, help) { out.push({ txid: coin.txid(), vout: coin.index, - address: address ? address.toBase58(this.network) : null, + address: address ? address.toString(this.network) : null, account: ring ? ring.name : undefined, redeemScript: ring && ring.script ? ring.script.toJSON() @@ -1674,7 +1674,7 @@ RPC.prototype.setLogLevel = co(function* setLogLevel(args, help) { function parseAddress(raw, network) { try { - return Address.fromBase58(raw, network); + return Address.fromString(raw, network); } catch (e) { throw new RPCError(errs.INVALID_ADDRESS_OR_KEY, 'Invalid address.'); } diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index f2320ec6..6b66f735 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -3131,7 +3131,7 @@ DetailsMember.prototype.getJSON = function getJSON(network) { return { value: Amount.btc(this.value), address: this.address - ? this.address.toBase58(network) + ? this.address.toString(network) : null, path: this.path ? this.path.toJSON() diff --git a/lib/wallet/walletkey.js b/lib/wallet/walletkey.js index e94685eb..0c98affb 100644 --- a/lib/wallet/walletkey.js +++ b/lib/wallet/walletkey.js @@ -134,7 +134,7 @@ WalletKey.prototype.toJSON = function toJSON() { script: this.script ? this.script.toRaw().toString('hex') : null, program: this.witness ? this.getProgram().toRaw().toString('hex') : null, type: Script.typesByVal[this.getType()].toLowerCase(), - address: this.getAddress('base58') + address: this.getAddress('string') }; }; diff --git a/test/http-test.js b/test/http-test.js index d0c1573d..d62ea241 100644 --- a/test/http-test.js +++ b/test/http-test.js @@ -59,7 +59,7 @@ describe('HTTP', function() { assert.equal(info.id, 'test'); addr = info.account.receiveAddress; assert.equal(typeof addr, 'string'); - addr = Address.fromBase58(addr); + addr = Address.fromString(addr); })); it('should fill with funds', co(function* () { @@ -114,7 +114,7 @@ describe('HTTP', function() { rate: 10000, outputs: [{ value: 10000, - address: addr.toBase58() + address: addr.toString() }] }; @@ -218,10 +218,10 @@ describe('HTTP', function() { })); it('should validate an address', co(function* () { - var json = yield wallet.client.rpc.execute('validateaddress', [addr.toBase58()]); + var json = yield wallet.client.rpc.execute('validateaddress', [addr.toString()]); assert.deepStrictEqual(json, { isvalid: true, - address: addr.toBase58(), + address: addr.toString(), scriptPubKey: Script.fromAddress(addr).toRaw().toString('hex'), ismine: false, iswatchonly: false diff --git a/test/node-test.js b/test/node-test.js index 2e3238c0..19efc37b 100644 --- a/test/node-test.js +++ b/test/node-test.js @@ -559,12 +559,12 @@ describe('Node', function() { json = yield node.rpc.call({ method: 'validateaddress', - params: [addr.toBase58()] + params: [addr.toString()] }, {}); assert.deepStrictEqual(json.result, { isvalid: true, - address: addr.toBase58(), + address: addr.toString(), scriptPubKey: Script.fromAddress(addr).toJSON(), ismine: false, iswatchonly: false diff --git a/test/wallet-test.js b/test/wallet-test.js index d58cc625..8cfb643c 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -74,18 +74,18 @@ describe('Wallet', function() { it('should generate new key and address', co(function* () { var w = yield walletdb.create(); - var addr = w.getAddress('base58'); + var addr = w.getAddress('string'); assert(addr); - assert(Address.fromBase58(addr)); + assert(Address.fromString(addr)); })); it('should validate existing address', function() { - assert(Address.fromBase58('1KQ1wMNwXHUYj1nV2xzsRcKUH8gVFpTFUc')); + assert(Address.fromString('1KQ1wMNwXHUYj1nV2xzsRcKUH8gVFpTFUc')); }); it('should fail to validate invalid address', function() { assert.throws(function() { - Address.fromBase58('1KQ1wMNwXHUYj1nv2xzsRcKUH8gVFpTFUc'); + Address.fromString('1KQ1wMNwXHUYj1nv2xzsRcKUH8gVFpTFUc'); }); }); @@ -111,7 +111,7 @@ describe('Wallet', function() { w = yield walletdb.create({ witness: witness }); - addr = Address.fromBase58(w.getAddress('base58')); + addr = Address.fromString(w.getAddress('string')); if (witness) assert.equal(addr.type, scriptTypes.WITNESSPUBKEYHASH); @@ -647,8 +647,8 @@ describe('Wallet', function() { yield w3.addSharedKey(w2.account.accountKey); // Our p2sh address - b58 = w1.account[rec].getAddress('base58'); - addr = Address.fromBase58(b58); + b58 = w1.account[rec].getAddress('string'); + addr = Address.fromString(b58); if (witness) { if (bullshitNesting) @@ -659,17 +659,17 @@ describe('Wallet', function() { assert.equal(addr.type, scriptTypes.SCRIPTHASH); } - assert.equal(w1.account[rec].getAddress('base58'), b58); - assert.equal(w2.account[rec].getAddress('base58'), b58); - assert.equal(w3.account[rec].getAddress('base58'), b58); + assert.equal(w1.account[rec].getAddress('string'), b58); + assert.equal(w2.account[rec].getAddress('string'), b58); + assert.equal(w3.account[rec].getAddress('string'), b58); paddr = w1.getNested(); if (witness) { assert(paddr); - assert.equal(w1.getNested('base58'), paddr.toBase58()); - assert.equal(w2.getNested('base58'), paddr.toBase58()); - assert.equal(w3.getNested('base58'), paddr.toBase58()); + assert.equal(w1.getNested('string'), paddr.toString()); + assert.equal(w2.getNested('string'), paddr.toString()); + assert.equal(w3.getNested('string'), paddr.toString()); } // Add a shared unspent transaction to our wallets @@ -689,11 +689,11 @@ describe('Wallet', function() { assert.equal(w1.account.changeDepth, 1); - assert(w1.account[rec].getAddress('base58') !== b58); - b58 = w1.account[rec].getAddress('base58'); - assert.equal(w1.account[rec].getAddress('base58'), b58); - assert.equal(w2.account[rec].getAddress('base58'), b58); - assert.equal(w3.account[rec].getAddress('base58'), b58); + assert(w1.account[rec].getAddress('string') !== b58); + b58 = w1.account[rec].getAddress('string'); + assert.equal(w1.account[rec].getAddress('string'), b58); + assert.equal(w2.account[rec].getAddress('string'), b58); + assert.equal(w3.account[rec].getAddress('string'), b58); // Create a tx requiring 2 signatures send = new MTX(); @@ -713,10 +713,10 @@ describe('Wallet', function() { assert.equal(w1.account.changeDepth, 1); - change = w1.account.change.getAddress('base58'); - assert.equal(w1.account.change.getAddress('base58'), change); - assert.equal(w2.account.change.getAddress('base58'), change); - assert.equal(w3.account.change.getAddress('base58'), change); + change = w1.account.change.getAddress('string'); + assert.equal(w1.account.change.getAddress('string'), change); + assert.equal(w2.account.change.getAddress('string'), change); + assert.equal(w3.account.change.getAddress('string'), change); // Simulate a confirmation block = nextBlock(); @@ -726,12 +726,12 @@ describe('Wallet', function() { assert.equal(w1.account[depth], 2); assert.equal(w1.account.changeDepth, 2); - assert(w1.account[rec].getAddress('base58') === b58); - assert(w1.account.change.getAddress('base58') !== change); - change = w1.account.change.getAddress('base58'); - assert.equal(w1.account.change.getAddress('base58'), change); - assert.equal(w2.account.change.getAddress('base58'), change); - assert.equal(w3.account.change.getAddress('base58'), change); + assert(w1.account[rec].getAddress('string') === b58); + assert(w1.account.change.getAddress('string') !== change); + change = w1.account.change.getAddress('string'); + assert.equal(w1.account.change.getAddress('string'), change); + assert.equal(w2.account.change.getAddress('string'), change); + assert.equal(w3.account.change.getAddress('string'), change); if (witness) { send.inputs[0].witness.set(2, 0); @@ -829,11 +829,11 @@ describe('Wallet', function() { assert(w.account.accountIndex === 0); assert.notEqual( - account.receive.getAddress('base58'), - w.account.receive.getAddress('base58')); + account.receive.getAddress('string'), + w.account.receive.getAddress('string')); - assert.equal(w.getAddress('base58'), - w.account.receive.getAddress('base58')); + assert.equal(w.getAddress('string'), + w.account.receive.getAddress('string')); // Coinbase t1 = new MTX();