Skip to content

Commit ba4fce5

Browse files
committed
chore: use address-hook encoding
1 parent d527d9c commit ba4fce5

14 files changed

+155
-233
lines changed

packages/fast-usdc/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
},
3434
"dependencies": {
3535
"@agoric/client-utils": "^0.1.0",
36+
"@agoric/cosmic-proto": "^0.4.0",
3637
"@agoric/ertp": "^0.16.2",
3738
"@agoric/internal": "^0.3.2",
3839
"@agoric/notifier": "^0.6.2",

packages/fast-usdc/src/cli/transfer.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
makeVStorage,
77
pickEndpoint,
88
} from '@agoric/client-utils';
9+
import { encodeAddressHook } from '@agoric/cosmic-proto/address-hooks.js';
910
import { queryFastUSDCLocalChainAccount } from '../util/agoric.js';
1011
import { depositForBurn, makeProvider } from '../util/cctp.js';
1112
import {
@@ -22,7 +23,7 @@ import {
2223
const transfer = async (
2324
/** @type {File} */ configFile,
2425
/** @type {string} */ amount,
25-
/** @type {string} */ destination,
26+
/** @type {string} */ EUD,
2627
out = console,
2728
fetch = globalThis.fetch,
2829
/** @type {VStorage | undefined} */ vstorage,
@@ -39,13 +40,13 @@ const transfer = async (
3940
{ chainName: 'agoric', rpcAddrs: [pickEndpoint(netConfig)] },
4041
);
4142
const agoricAddr = await queryFastUSDCLocalChainAccount(vstorage, out);
42-
const appendedAddr = `${agoricAddr}?EUD=${destination}`;
43-
out.log(`forwarding destination ${appendedAddr}`);
43+
const encodedAddr = encodeAddressHook(agoricAddr, { EUD });
44+
out.log(`forwarding destination ${encodedAddr}`);
4445

4546
const { exists, address } = await queryForwardingAccount(
4647
config.nobleApi,
4748
config.nobleToAgoricChannel,
48-
appendedAddr,
49+
encodedAddr,
4950
out,
5051
fetch,
5152
);
@@ -58,13 +59,13 @@ const transfer = async (
5859
signer,
5960
signerAddress,
6061
config.nobleToAgoricChannel,
61-
appendedAddr,
62+
encodedAddr,
6263
out,
6364
);
6465
out.log(res);
6566
} catch (e) {
6667
out.error(
67-
`Error registering noble forwarding account for ${appendedAddr} on channel ${config.nobleToAgoricChannel}`,
68+
`Error registering noble forwarding account for ${encodedAddr} on channel ${config.nobleToAgoricChannel}`,
6869
);
6970
throw e;
7071
}

packages/fast-usdc/src/exos/advancer.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1+
import { decodeAddressHook } from '@agoric/cosmic-proto/address-hooks.js';
12
import { AmountMath } from '@agoric/ertp';
23
import { assertAllDefined, makeTracer } from '@agoric/internal';
34
import { AnyNatAmountShape, ChainAddressShape } from '@agoric/orchestration';
45
import { pickFacet } from '@agoric/vat-data';
56
import { VowShape } from '@agoric/vow';
67
import { q } from '@endo/errors';
78
import { E } from '@endo/far';
8-
import { M } from '@endo/patterns';
9+
import { M, mustMatch } from '@endo/patterns';
910
import {
1011
CctpTxEvidenceShape,
11-
EudParamShape,
12+
AddressHookShape,
1213
EvmHashShape,
1314
} from '../type-guards.js';
14-
import { addressTools } from '../utils/address.js';
1515
import { makeFeeTools } from '../utils/fees.js';
1616

1717
/**
@@ -22,7 +22,7 @@ import { makeFeeTools } from '../utils/fees.js';
2222
* @import {ZoeTools} from '@agoric/orchestration/src/utils/zoe-tools.js';
2323
* @import {VowTools} from '@agoric/vow';
2424
* @import {Zone} from '@agoric/zone';
25-
* @import {CctpTxEvidence, EvmHash, FeeConfig, LogFn, NobleAddress} from '../types.js';
25+
* @import {CctpTxEvidence, AddressHook, EvmHash, FeeConfig, LogFn, NobleAddress} from '../types.js';
2626
* @import {StatusManager} from './status-manager.js';
2727
* @import {LiquidityPoolKit} from './liquidity-pool.js';
2828
*/
@@ -148,11 +148,10 @@ export const prepareAdvancerKit = (
148148

149149
const { borrowerFacet, poolAccount } = this.state;
150150
const { recipientAddress } = evidence.aux;
151-
// throws if EUD is not found
152-
const { EUD } = addressTools.getQueryParams(
153-
recipientAddress,
154-
EudParamShape,
155-
);
151+
const decoded = decodeAddressHook(recipientAddress);
152+
mustMatch(decoded, AddressHookShape);
153+
const { EUD } = /** @type {AddressHook['query']} */ (decoded.query);
154+
log(`decoded EUD: ${EUD}`);
156155
// throws if the bech32 prefix is not found
157156
const destination = chainHub.makeChainAddress(EUD);
158157

packages/fast-usdc/src/exos/settler.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { atob } from '@endo/base64';
55
import { E } from '@endo/far';
66
import { M } from '@endo/patterns';
77

8+
import { decodeAddressHook } from '@agoric/cosmic-proto/address-hooks.js';
89
import { PendingTxStatus } from '../constants.js';
9-
import { addressTools } from '../utils/address.js';
1010
import { makeFeeTools } from '../utils/fees.js';
1111
import { EvmHashShape } from '../type-guards.js';
1212

@@ -151,14 +151,19 @@ export const prepareSettler = (
151151
return;
152152
}
153153

154-
if (!addressTools.hasQueryParams(tx.receiver)) {
155-
console.log('not query params', tx.receiver);
156-
return;
157-
}
158-
159-
const { EUD } = addressTools.getQueryParams(tx.receiver);
160-
if (!EUD) {
161-
console.log('no EUD parameter', tx.receiver);
154+
let EUD;
155+
try {
156+
({ EUD } = decodeAddressHook(tx.receiver).query);
157+
if (!EUD) {
158+
log('no EUD parameter', tx.receiver);
159+
return;
160+
}
161+
if (typeof EUD !== 'string') {
162+
log('EUD is not a string', EUD);
163+
return;
164+
}
165+
} catch (e) {
166+
log('no query params', tx.receiver);
162167
return;
163168
}
164169

packages/fast-usdc/src/type-guards.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { PendingTxStatus } from './constants.js';
66
* @import {TypedPattern} from '@agoric/internal';
77
* @import {FastUsdcTerms} from './fast-usdc.contract.js';
88
* @import {USDCProposalShapes} from './pool-share-math.js';
9-
* @import {CctpTxEvidence, FeeConfig, PendingTx, PoolMetrics, ChainPolicy, FeedPolicy} from './types.js';
9+
* @import {CctpTxEvidence, FeeConfig, PendingTx, PoolMetrics, ChainPolicy, FeedPolicy, AddressHook} from './types.js';
1010
*/
1111

1212
/**
@@ -67,10 +67,12 @@ export const PendingTxShape = {
6767
};
6868
harden(PendingTxShape);
6969

70-
export const EudParamShape = {
71-
EUD: M.string(),
70+
/** @type {TypedPattern<AddressHook>} */
71+
export const AddressHookShape = {
72+
baseAddress: M.string(),
73+
query: { EUD: M.string() },
7274
};
73-
harden(EudParamShape);
75+
harden(AddressHookShape);
7476

7577
const NatAmountShape = { brand: BrandShape, value: M.nat() };
7678
/** @type {TypedPattern<FeeConfig>} */

packages/fast-usdc/src/types.ts

+9
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,14 @@ export type FastUSDCConfig = {
8282
assetInfo: [Denom, DenomDetail & { brandKey?: string }][];
8383
} & CopyRecord;
8484

85+
/** decoded address hook parameters */
86+
export type AddressHook = {
87+
baseAddress: string;
88+
query: {
89+
/** end user destination address */
90+
EUD: string;
91+
};
92+
};
93+
8594
export type * from './constants.js';
8695
export type { LiquidityPoolKit } from './exos/liquidity-pool.js';

packages/fast-usdc/src/utils/address.js

-71
This file was deleted.

packages/fast-usdc/test/cli/snapshots/transfer.test.ts.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Generated by [AVA](https://avajs.dev).
1515
typeUrl: '/noble.forwarding.v1.MsgRegisterAccount',
1616
value: {
1717
channel: 'channel-test-7',
18-
recipient: 'agoric123456?EUD=dydx1234',
18+
recipient: 'agoric10rchp4vc53apxn32q42c3zryml8xq3xshyzuhjk6405wtxy7tl3d7e0f8az423pav3ukg7p3xgengqpq4066gy',
1919
signer: 'noble09876',
2020
},
2121
},
Binary file not shown.

packages/fast-usdc/test/cli/transfer.test.ts

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import test from 'ava';
2+
import { encodeAddressHook } from '@agoric/cosmic-proto/address-hooks.js';
23
import transfer from '../../src/cli/transfer.js';
34
import {
45
mockOut,
@@ -63,14 +64,18 @@ test('Transfer registers the noble forwarding account if it does not exist', asy
6364
};
6465
const out = mockOut();
6566
const file = mockFile(path, JSON.stringify(config));
66-
const agoricSettlementAccount = 'agoric123456';
67+
const agoricSettlementAccount =
68+
'agoric16kv2g7snfc4q24vg3pjdlnnqgngtjpwtetd2h689nz09lcklvh5s8u37ek';
6769
const settlementAccountVstoragePath = 'published.fastUsdc.settlementAccount';
6870
const vstorageMock = makeVstorageMock({
6971
[settlementAccountVstoragePath]: agoricSettlementAccount,
7072
});
7173
const amount = '150';
72-
const destination = 'dydx1234';
73-
const nobleFwdAccountQuery = `${nobleApi}/noble/forwarding/v1/address/${nobleToAgoricChannel}/${agoricSettlementAccount}${encodeURIComponent('?EUD=')}${destination}/`;
74+
const EUD = 'dydx1234';
75+
const nobleFwdAccountQuery = `${nobleApi}/noble/forwarding/v1/address/${nobleToAgoricChannel}/${encodeAddressHook(
76+
agoricSettlementAccount,
77+
{ EUD },
78+
)}/`;
7479
const fetchMock = makeFetchMock({
7580
[nobleFwdAccountQuery]: {
7681
address: 'noble14lwerrcfzkzrv626w49pkzgna4dtga8c5x479h',
@@ -84,7 +89,7 @@ test('Transfer registers the noble forwarding account if it does not exist', asy
8489
await transfer.transfer(
8590
file,
8691
amount,
87-
destination,
92+
EUD,
8893
// @ts-expect-error mocking console
8994
out,
9095
fetchMock.fetch,
@@ -114,14 +119,18 @@ test('Transfer signs and broadcasts the depositForBurn message on Ethereum', asy
114119
};
115120
const out = mockOut();
116121
const file = mockFile(path, JSON.stringify(config));
117-
const agoricSettlementAccount = 'agoric123456';
122+
const agoricSettlementAccount =
123+
'agoric16kv2g7snfc4q24vg3pjdlnnqgngtjpwtetd2h689nz09lcklvh5s8u37ek';
118124
const settlementAccountVstoragePath = 'published.fastUsdc.settlementAccount';
119125
const vstorageMock = makeVstorageMock({
120126
[settlementAccountVstoragePath]: agoricSettlementAccount,
121127
});
122128
const amount = '150';
123-
const destination = 'dydx1234';
124-
const nobleFwdAccountQuery = `${nobleApi}/noble/forwarding/v1/address/${nobleToAgoricChannel}/${agoricSettlementAccount}${encodeURIComponent('?EUD=')}${destination}/`;
129+
const EUD = 'dydx1234';
130+
const nobleFwdAccountQuery = `${nobleApi}/noble/forwarding/v1/address/${nobleToAgoricChannel}/${encodeAddressHook(
131+
agoricSettlementAccount,
132+
{ EUD },
133+
)}/`;
125134
const fetchMock = makeFetchMock({
126135
[nobleFwdAccountQuery]: {
127136
address: 'noble14lwerrcfzkzrv626w49pkzgna4dtga8c5x479h',
@@ -135,7 +144,7 @@ test('Transfer signs and broadcasts the depositForBurn message on Ethereum', asy
135144
await transfer.transfer(
136145
file,
137146
amount,
138-
destination,
147+
EUD,
139148
// @ts-expect-error mocking console
140149
out,
141150
fetchMock.fetch,

0 commit comments

Comments
 (0)