Skip to content
This repository was archived by the owner on Apr 3, 2019. It is now read-only.
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
12 changes: 10 additions & 2 deletions lib/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,11 @@ Address.prototype.toCashBuffer = function() {
*/


Address.prototype.toCashAddress = function() {
Address.prototype.toCashAddress = function(withPrefix) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets validate that withPrefix is a boolean if it exists

if(withPrefix === undefined) {
withPrefix = true;
}
$.checkArgumentType(withPrefix, 'boolean', 'withPrefix');
function getTypeBits(type) {
switch (type) {
case 'pubkeyhash':
Expand Down Expand Up @@ -677,7 +681,11 @@ Address.prototype.toCashAddress = function() {
var payloadData = convertBits([versionByte].concat(arr), 8, 5);
var checksumData = prefixData.concat(payloadData).concat(eight0);
var payload = payloadData.concat(checksumToArray(polymod(checksumData)));
return this.network.prefix+ ':' + base32.encode(payload);
if(withPrefix) {
return this.network.prefix+ ':' + base32.encode(payload);
} else {
return base32.encode(payload);
}
};


Expand Down
11 changes: 11 additions & 0 deletions test/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ describe('Address', function() {
a.toCashAddress().should.equal('bchtest:qry5cr6h2qe25pzwwfrz8m653fh2tf6nusj9dl0ujc');
});


it('should be able to convert a testnet address to a cashaddr without prefix', function() {
var a = new Address('mysKEM9kN86Nkcqwb4gw7RqtDyc552LQoq');
a.toCashAddress(false).should.equal('qry5cr6h2qe25pzwwfrz8m653fh2tf6nusj9dl0ujc');
});

it('should be able to convert a testnet address to a cashaddr with prefix', function() {
var a = new Address('mysKEM9kN86Nkcqwb4gw7RqtDyc552LQoq');
a.toCashAddress(true).should.equal('bchtest:qry5cr6h2qe25pzwwfrz8m653fh2tf6nusj9dl0ujc');
});

it('should fail convert no prefix addresses bad checksum ', function() {
(function() {
var a = new Address('qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx7');
Expand Down