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

Fix: add new type to allow toBase58Check to get string in browser #2069

Closed
Closed
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
2 changes: 1 addition & 1 deletion src/address.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export declare function fromBech32(address: string): Bech32Result;
/**
* encode address hash to base58 address with version
*/
export declare function toBase58Check(hash: Buffer, version: number): string;
export declare function toBase58Check(hash: Buffer | string, version: number): string;
/**
* encode address hash to bech32 address with version and prefix
*/
Expand Down
3 changes: 2 additions & 1 deletion src/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ exports.fromBech32 = fromBech32;
* encode address hash to base58 address with version
*/
function toBase58Check(hash, version) {
hash = Buffer.isBuffer(hash) ? hash : Buffer.from(hash, 'hex');
(0, types_1.typeforce)(
(0, types_1.tuple)(types_1.Hash160bit, types_1.UInt8),
arguments,
[hash, version],
);
const payload = Buffer.allocUnsafe(21);
payload.writeUInt8(version, 0);
Expand Down
6 changes: 6 additions & 0 deletions test/address.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ describe('address', () => {

assert.strictEqual(address, f.base58check);
});

it('encodes ' + f.hash + ' (' + f.network + ')', () => {
const address = baddress.toBase58Check(f.hash, f.version);

assert.strictEqual(address, f.base58check);
});
});
});

Expand Down
5 changes: 3 additions & 2 deletions ts_src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ export function fromBech32(address: string): Bech32Result {
/**
* encode address hash to base58 address with version
*/
export function toBase58Check(hash: Buffer, version: number): string {
typeforce(tuple(Hash160bit, UInt8), arguments);
export function toBase58Check(hash: Buffer | string, version: number): string {
hash = Buffer.isBuffer(hash) ? hash : Buffer.from(hash, 'hex');
typeforce(tuple(Hash160bit, UInt8), [hash, version]);

const payload = Buffer.allocUnsafe(21);
payload.writeUInt8(version, 0);
Expand Down