Skip to content

Commit

Permalink
Minor refactoring. Additional tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Feb 16, 2024
1 parent db67192 commit 42498c3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/address.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,16 @@ describe("test address", () => {
assert.isFalse(Address.isValid("xerd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2fsmsgldz"));
assert.isFalse(Address.isValid("erd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2"));
});

it("should check whether isSmartContract", () => {
assert.isFalse(
Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th").isSmartContract(),
);
assert.isTrue(
Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqplllst77y4l").isSmartContract(),
);
assert.isTrue(
Address.fromBech32("erd1qqqqqqqqqqqqqpgqxwakt2g7u9atsnr03gqcgmhcv38pt7mkd94q6shuwt").isSmartContract(),
);
});
});
22 changes: 15 additions & 7 deletions src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ export class Address {
throw new errors.ErrAddressCannotCreate(value, err);
}

let prefix = decoded.prefix;
const prefix = decoded.prefix;
if (prefix != HRP) {
throw new errors.ErrAddressBadHrp(HRP, prefix);
}

let pubkey = Buffer.from(bech32.fromWords(decoded.words));
const pubkey = Buffer.from(bech32.fromWords(decoded.words));
if (pubkey.length != PUBKEY_LENGTH) {
throw new errors.ErrAddressCannotCreate(value);
}
Expand Down Expand Up @@ -194,6 +194,14 @@ export class Address {
return Buffer.from(this.valueHex, "hex");
}

/**
* Returns the human-readable-part of the bech32 addresses.
* The HRP is currently hardcoded to "erd".
*/
getHrp(): string {
return HRP;
}

/**
* Returns whether the address is empty.
*/
Expand All @@ -216,16 +224,16 @@ export class Address {
* Returns the bech32 representation of the address
*/
toString(): string {
return this.bech32();
return this.toBech32();
}

/**
* Converts the address to a pretty, plain JavaScript object.
*/
toJSON(): object {
return {
bech32: this.bech32(),
pubkey: this.hex(),
bech32: this.toBech32(),
pubkey: this.toHex(),
};
}

Expand All @@ -241,13 +249,13 @@ export class Address {
* Use {@link isSmartContract} instead.
*/
isContractAddress(): boolean {
return this.hex().startsWith(SMART_CONTRACT_HEX_PUBKEY_PREFIX);
return this.isSmartContract();
}

/**
* Returns whether the address is a smart contract address.
*/
isSmartContract(): boolean {
return this.isContractAddress();
return this.toHex().startsWith(SMART_CONTRACT_HEX_PUBKEY_PREFIX);
}
}

0 comments on commit 42498c3

Please sign in to comment.