Skip to content

Commit 4343137

Browse files
authored
Cosmwasm: Add special interface so the cosmos addresses can be created with the appropriate prefix (#593)
1 parent 12bf68a commit 4343137

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

core/definitions/src/address.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ export interface Address {
3434
toUniversalAddress(): UniversalAddress;
3535
}
3636

37+
export interface ChainSpecificAddress extends Address {
38+
setChain(chain: Chain): void;
39+
}
40+
export function isChainSpecificAddress(thing: any): thing is ChainSpecificAddress {
41+
return typeof thing === "object" && "setChain" in thing;
42+
}
43+
3744
export type MappedPlatforms = keyof WormholeRegistry.PlatformToNativeAddressMapping;
3845

3946
/** Utility type to map platform to its native address implementation */
@@ -87,7 +94,14 @@ export function toNative<C extends Chain>(
8794
`No native address type registered for platform ${platform}, import the platform directly or, if using sdk package, import the addresses conditional export`,
8895
);
8996
try {
90-
return new nativeCtr(ua) as unknown as NativeAddress<C>;
97+
const nativeAddress = new nativeCtr(ua) as unknown as NativeAddress<C>;
98+
99+
if (isChainSpecificAddress(nativeAddress)) {
100+
// idk why but this typeguard doesnt actually work?
101+
(nativeAddress as ChainSpecificAddress).setChain(chain);
102+
}
103+
104+
return nativeAddress;
91105
} catch (_) {
92106
// try to parse it as a universal address
93107
return (UniversalAddress.instanceof(ua) ? ua : new UniversalAddress(ua)).toNative(chain);
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
1-
import { CosmwasmAddress } from './../../src/index.js';
1+
import { toNative } from "@wormhole-foundation/sdk-connect";
2+
import { CosmwasmAddress } from "./../../src/index.js";
23

34
describe("Cosmwasm Address Tests", () => {
45
describe("Parse Address", () => {
56
test("An address parses", () => {
67
let address = new CosmwasmAddress(
7-
"terra1c02vds4uhgtrmcw7ldlg75zumdqxr8hwf7npseuf2h58jzhpgjxsgmwkvk"
8+
"terra1c02vds4uhgtrmcw7ldlg75zumdqxr8hwf7npseuf2h58jzhpgjxsgmwkvk",
89
);
910
expect(address).toBeTruthy();
1011

1112
address = new CosmwasmAddress(
12-
"xpla137w0wfch2dfmz7jl2ap8pcmswasj8kg06ay4dtjzw7tzkn77ufxqfw7acv"
13+
"xpla137w0wfch2dfmz7jl2ap8pcmswasj8kg06ay4dtjzw7tzkn77ufxqfw7acv",
1314
);
1415
expect(address).toBeTruthy();
1516
});
1617

1718
test("An invalid address is rejected", () => {
1819
expect(() => new CosmwasmAddress("bogusybogusybogus")).toThrow();
1920
});
21+
test("A hex address gets the appropriate prefix", () => {
22+
const hexAddress = "0x017ce8aec5af3bb3ac0158d49771d4c8feba2e54a614fa2a1c0c95e9c4c37185";
23+
const address = toNative("Xpla", hexAddress);
24+
expect(address.toString()).toEqual(
25+
"xpla1q97w3tk94uam8tqptr2fwuw5erlt5tj55c2052supj27n3xrwxzsj3f2qj",
26+
);
27+
});
2028
});
2129
});

platforms/cosmwasm/src/address.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Address } from "@wormhole-foundation/sdk-connect";
22
import { UniversalAddress, encoding, registerNative } from "@wormhole-foundation/sdk-connect";
3-
import type { AnyCosmwasmAddress } from "./types.js";
3+
import { chainToAddressPrefix } from "./constants.js";
4+
import type { AnyCosmwasmAddress, CosmwasmChains } from "./types.js";
45
import { _platform } from "./types.js";
56

67
/*
@@ -186,6 +187,11 @@ export class CosmwasmAddress implements Address {
186187
return new UniversalAddress(buff);
187188
}
188189

190+
setChain(chain: CosmwasmChains): void {
191+
// @ts-expect-error -- readonly
192+
this.domain = chainToAddressPrefix.get(chain);
193+
}
194+
189195
static isValidAddress(address: string): boolean {
190196
try {
191197
const maybe = encoding.bech32.decodeToBytes(address);

0 commit comments

Comments
 (0)