Skip to content

Commit f081d9f

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

14 files changed

+146
-210
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

+9-5
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 {
@@ -39,13 +40,16 @@ 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, {
44+
EUD: destination,
45+
});
46+
47+
out.log(`forwarding destination ${encodedAddr}`);
4448

4549
const { exists, address } = await queryForwardingAccount(
4650
config.nobleApi,
4751
config.nobleToAgoricChannel,
48-
appendedAddr,
52+
encodedAddr,
4953
out,
5054
fetch,
5155
);
@@ -58,13 +62,13 @@ const transfer = async (
5862
signer,
5963
signerAddress,
6064
config.nobleToAgoricChannel,
61-
appendedAddr,
65+
encodedAddr,
6266
out,
6367
);
6468
out.log(res);
6569
} catch (e) {
6670
out.error(
67-
`Error registering noble forwarding account for ${appendedAddr} on channel ${config.nobleToAgoricChannel}`,
71+
`Error registering noble forwarding account for ${encodedAddr} on channel ${config.nobleToAgoricChannel}`,
6872
);
6973
throw e;
7074
}

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

+21-10
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { assertAllDefined, makeTracer } from '@agoric/internal';
33
import { ChainAddressShape } from '@agoric/orchestration';
44
import { atob } from '@endo/base64';
55
import { E } from '@endo/far';
6-
import { M } from '@endo/patterns';
6+
import { M, matches } 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,17 +151,23 @@ export const prepareSettler = (
151151
return;
152152
}
153153

154-
if (!addressTools.hasQueryParams(tx.receiver)) {
154+
let endUserDestination;
155+
try {
156+
const { EUD } = decodeAddressHook(tx.receiver).query;
157+
if (!EUD) {
158+
console.log('no EUD parameter', tx.receiver);
159+
return;
160+
}
161+
if (!matches(EUD, M.string())) {
162+
console.log('EUD is not a string', EUD);
163+
return;
164+
}
165+
endUserDestination = /** @type {string} */ (EUD);
166+
} catch (e) {
155167
console.log('not query params', tx.receiver);
156168
return;
157169
}
158170

159-
const { EUD } = addressTools.getQueryParams(tx.receiver);
160-
if (!EUD) {
161-
console.log('no EUD parameter', tx.receiver);
162-
return;
163-
}
164-
165171
const amount = BigInt(tx.amount); // TODO: what if this throws?
166172

167173
const { self } = this.facets;
@@ -177,7 +183,12 @@ export const prepareSettler = (
177183

178184
case PendingTxStatus.Observed:
179185
case PendingTxStatus.AdvanceFailed:
180-
return self.forward(found.txHash, sender, amount, EUD);
186+
return self.forward(
187+
found.txHash,
188+
sender,
189+
amount,
190+
endUserDestination,
191+
);
181192

182193
case undefined:
183194
default:

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, AddressHookParameters} 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<AddressHookParameters>} */
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-4
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,20 @@ 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';
7274
const destination = 'dydx1234';
73-
const nobleFwdAccountQuery = `${nobleApi}/noble/forwarding/v1/address/${nobleToAgoricChannel}/${agoricSettlementAccount}${encodeURIComponent('?EUD=')}${destination}/`;
75+
const nobleFwdAccountQuery = `${nobleApi}/noble/forwarding/v1/address/${nobleToAgoricChannel}/${encodeAddressHook(
76+
agoricSettlementAccount,
77+
{
78+
EUD: destination,
79+
},
80+
)}/`;
7481
const fetchMock = makeFetchMock({
7582
[nobleFwdAccountQuery]: {
7683
address: 'noble14lwerrcfzkzrv626w49pkzgna4dtga8c5x479h',
@@ -114,14 +121,20 @@ test('Transfer signs and broadcasts the depositForBurn message on Ethereum', asy
114121
};
115122
const out = mockOut();
116123
const file = mockFile(path, JSON.stringify(config));
117-
const agoricSettlementAccount = 'agoric123456';
124+
const agoricSettlementAccount =
125+
'agoric16kv2g7snfc4q24vg3pjdlnnqgngtjpwtetd2h689nz09lcklvh5s8u37ek';
118126
const settlementAccountVstoragePath = 'published.fastUsdc.settlementAccount';
119127
const vstorageMock = makeVstorageMock({
120128
[settlementAccountVstoragePath]: agoricSettlementAccount,
121129
});
122130
const amount = '150';
123131
const destination = 'dydx1234';
124-
const nobleFwdAccountQuery = `${nobleApi}/noble/forwarding/v1/address/${nobleToAgoricChannel}/${agoricSettlementAccount}${encodeURIComponent('?EUD=')}${destination}/`;
132+
const nobleFwdAccountQuery = `${nobleApi}/noble/forwarding/v1/address/${nobleToAgoricChannel}/${encodeAddressHook(
133+
agoricSettlementAccount,
134+
{
135+
EUD: destination,
136+
},
137+
)}/`;
125138
const fetchMock = makeFetchMock({
126139
[nobleFwdAccountQuery]: {
127140
address: 'noble14lwerrcfzkzrv626w49pkzgna4dtga8c5x479h',

0 commit comments

Comments
 (0)