Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address: derive network property from string and return correct string for network #720

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
address: derive network
  • Loading branch information
pinheadmz committed Sep 16, 2019
commit a669afa162a6a547f738a7b7ccc8634e0736b9f0
29 changes: 22 additions & 7 deletions lib/primitives/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ class Address {

const {hash, type, version} = options;

return this.fromHash(hash, type, version);
if (options.network)
network = options.network;

return this.fromHash(hash, type, version, network);
}

/**
Expand Down Expand Up @@ -208,6 +211,9 @@ class Address {
*/

toBase58(network) {
if (!network && this.network)
network = this.network;

return base58.encode(this.toRaw(network));
}

Expand All @@ -222,6 +228,9 @@ class Address {
const version = this.version;
const hash = this.hash;

if (!network && this.network)
network = this.network;

assert(version !== -1,
'Cannot convert non-program address to bech32.');

Expand Down Expand Up @@ -276,6 +285,9 @@ class Address {
*/

toString(network) {
if(!network && this.network)
network = this.network;

if (this.version !== -1)
return this.toBech32(network);
return this.toBase58(network);
Expand All @@ -290,7 +302,7 @@ class Address {
return '<Address:'
+ ` type=${this.getType()}`
+ ` version=${this.version}`
+ ` str=${this.toString()}`
+ ` str=${this.toString(this.network)}`
+ '>';
}

Expand All @@ -305,6 +317,7 @@ class Address {
const br = bio.read(data, true);
const prefix = br.readU8();

// will throw if `network` param doesn't match network derived from `data`
network = Network.fromAddress(prefix, network);

const type = Address.getType(prefix, network);
Expand All @@ -327,7 +340,7 @@ class Address {

br.verifyChecksum(hash256.digest);

return this.fromHash(hash, type, version);
return this.fromHash(hash, type, version, network);
}

/**
Expand Down Expand Up @@ -385,10 +398,10 @@ class Address {

const addr = bech32.decode(data);

// make sure HRP is correct.
Network.fromBech32(addr.hrp, network);
// make sure HRP is correct and add property to object.
network = Network.fromBech32(addr.hrp, network);

return this.fromHash(addr.hash, type, addr.version);
return this.fromHash(addr.hash, type, addr.version, network);
}

/**
Expand Down Expand Up @@ -561,7 +574,7 @@ class Address {
* @throws on bad hash size
*/

fromHash(hash, type, version) {
fromHash(hash, type, version, network) {
if (typeof type === 'string') {
type = Address.types[type.toUpperCase()];
assert(type != null, 'Not a valid address type.');
Expand Down Expand Up @@ -596,6 +609,8 @@ class Address {
this.hash = hash;
this.type = type;
this.version = version;
if (network)
this.network = network;

return this;
}
Expand Down