Skip to content

Commit 4e8aec0

Browse files
committed
migrate to @scure/base for base64 & bech32 encoding
Return types are narrowed in a way that's still backwards-compatible.
1 parent 9327a82 commit 4e8aec0

File tree

14 files changed

+45
-25
lines changed

14 files changed

+45
-25
lines changed

.pnp.cjs

Lines changed: 14 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:ff70e636d314a19a3e74a4c0ceb19bc152bca24939efc3d41976cb1e0a1b1c02
3+
size 167308

packages/amino/src/addresses.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ export function pubkeyToRawAddress(pubkey: Pubkey): Uint8Array {
3737
}
3838
}
3939

40-
export function pubkeyToAddress(pubkey: Pubkey, prefix: string): string {
40+
export function pubkeyToAddress(pubkey: Pubkey, prefix: string): `${string}1${string}` {
4141
return toBech32(prefix, pubkeyToRawAddress(pubkey));
4242
}

packages/amino/src/secp256k1wallet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class Secp256k1Wallet implements OfflineAminoSigner {
3333
this.prefix = prefix;
3434
}
3535

36-
private get address(): string {
36+
private get address(): `${string}1${string}` {
3737
return toBech32(this.prefix, rawSecp256k1PubkeyToRawAddress(this.pubkey));
3838
}
3939

packages/cosmwasm-stargate/src/instantiate2.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function _instantiate2AddressIntermediate(
2929
salt: Uint8Array,
3030
msg: string | null,
3131
prefix: string,
32-
): { key: Uint8Array; addressData: Uint8Array; address: string } {
32+
): { key: Uint8Array; addressData: Uint8Array; address: `${string}1${string}` } {
3333
assert(checksum.length === 32);
3434
const creatorData = fromBech32(creator).data;
3535

@@ -68,7 +68,7 @@ export function instantiate2Address(
6868
creator: string,
6969
salt: Uint8Array,
7070
bech32Prefix: string,
71-
): string {
71+
): `${string}1${string}` {
7272
// Non-empty msg values are discouraged.
7373
// See https://medium.com/cosmwasm/dev-note-3-limitations-of-instantiate2-and-how-to-deal-with-them-a3f946874230.
7474
const msg = null;

packages/cosmwasm-stargate/src/testutils.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function getHackatom(): ContractUploadInstructions {
6565
};
6666
}
6767

68-
export function makeRandomAddress(): string {
68+
export function makeRandomAddress(): `${string}1${string}` {
6969
return toBech32("wasm", Random.getBytes(20));
7070
}
7171

packages/encoding/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
"pack-web": "yarn build-or-skip && webpack --mode development --config webpack.web.config.js"
4141
},
4242
"dependencies": {
43-
"base64-js": "^1.3.0",
44-
"bech32": "^1.1.4",
43+
"@scure/base": "^1.2.6",
4544
"readonly-date": "^1.0.0"
4645
},
4746
"devDependencies": {

packages/encoding/src/base64.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import * as base64js from "base64-js";
1+
import { base64 } from "@scure/base";
22

33
export function toBase64(data: Uint8Array): string {
4-
return base64js.fromByteArray(data);
4+
return base64.encode(data);
55
}
66

77
export function fromBase64(base64String: string): Uint8Array {
88
if (!base64String.match(/^[a-zA-Z0-9+/]*={0,2}$/)) {
99
throw new Error("Invalid base64 string format");
1010
}
11-
return base64js.toByteArray(base64String);
11+
return base64.decode(base64String);
1212
}

packages/encoding/src/bech32.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import * as bech32 from "bech32";
1+
import { bech32 } from "@scure/base";
22

3-
export function toBech32(prefix: string, data: Uint8Array, limit?: number): string {
4-
const address = bech32.encode(prefix, bech32.toWords(data), limit);
3+
export function toBech32(prefix: string, data: Uint8Array, limit?: number): `${string}1${string}` {
4+
const address = bech32.encode(prefix, data, limit);
55
return address;
66
}
77

88
export function fromBech32(
99
address: string,
1010
limit = Infinity,
1111
): { readonly prefix: string; readonly data: Uint8Array } {
12-
const decodedAddress = bech32.decode(address, limit);
12+
const decodedAddress = bech32.decode(address as `${string}1${string}`, limit);
1313
return {
1414
prefix: decodedAddress.prefix,
1515
data: new Uint8Array(bech32.fromWords(decodedAddress.words)),
@@ -22,7 +22,7 @@ export function fromBech32(
2222
* The input is validated along the way, which makes this significantly safer than
2323
* using `address.toLowerCase()`.
2424
*/
25-
export function normalizeBech32(address: string): string {
26-
const { prefix, data } = fromBech32(address);
25+
export function normalizeBech32(address: string): `${string}1${string}` {
26+
const { prefix, data } = fromBech32(address.toLowerCase() as `${string}1${string}`);
2727
return toBech32(prefix, data);
2828
}

packages/encoding/src/rfc3339.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ReadonlyDate } from "readonly-date";
1+
import type { ReadonlyDate } from "readonly-date";
22

33
const rfc3339Matcher =
44
/^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2})(\.\d{1,9})?((?:[+-]\d{2}:\d{2})|Z)$/;

0 commit comments

Comments
 (0)