Skip to content

Commit

Permalink
feat: handle non-Cosmos in makeChainAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Feb 18, 2025
1 parent a688f5b commit e7aebd1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
14 changes: 10 additions & 4 deletions packages/orchestration/src/exos/chain-hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
IBCChannelIDShape,
IBCConnectionInfoShape,
} from '../typeGuards.js';
import { getBech32Prefix } from '../utils/address.js';
import { getBech32Prefix, parseAccountId } from '../utils/address.js';

/**
* @import {NameHub} from '@agoric/vats';
Expand Down Expand Up @@ -495,8 +495,14 @@ export const makeChainHub = (zone, agoricNames, vowTools) => {
* @throws {Error} if chain info not found for bech32Prefix
*/
makeChainAddress(accountId) {
// FIXME if accountId has colons, parse it into chainId and value,
// otherwise the chainId comes from the bech32 prefix of the value
const parsed = parseAccountId(accountId);
if (parsed.chainId) {
return harden({
chainId: parsed.chainId,
value: parsed.accountAddress,
});
}
// Infer it from the bech32 prefix
const prefix = getBech32Prefix(accountId);
if (!bech32PrefixToChainName.has(prefix)) {
throw makeError(`Chain info not found for bech32Prefix ${q(prefix)}`);
Expand All @@ -505,7 +511,7 @@ export const makeChainHub = (zone, agoricNames, vowTools) => {
const { chainId } = chainInfos.get(chainName);
return harden({
chainId,
value: accountId,
value: parsed.accountAddress,
});
},
// TODO document whether this is limited to IBC
Expand Down
21 changes: 21 additions & 0 deletions packages/orchestration/src/utils/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,24 @@ export const getBech32Prefix = address => {
if (split === 0) return Fail`Missing prefix for ${q(address)}`;
return address.slice(0, split);
};

/**
* @param {string} accountId
* @see {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-10.md}
*/
export const parseAccountId = accountId => {
const parts = accountId.split(':');

if (parts.length >= 3) {
return {
chainId: `${parts[0]}:${parts[1]}`,
accountAddress: parts.slice(2).join(':'), // Handles cases where the address contains colons
};
} else {
return {
chainId: null,
accountAddress: accountId,
};
}
};
harden(parseAccountId);

0 comments on commit e7aebd1

Please sign in to comment.