Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fusdc uses bech32 address hook encoding #10682

Open
wants to merge 2 commits into
base: mfig-no-future-address-hooks
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/cosmic-proto/src/address-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ harden(encodeAddressHook);
* @param {string} addressHook
* @param {number} [charLimit]
* @returns {{ baseAddress: string; query: HookQuery }}
* @throws {Error} if no hook string or hook string does not start with `?`
*/
export const decodeAddressHook = (addressHook, charLimit) => {
const { baseAddress, hookData } = splitHookedAddress(addressHook, charLimit);
Expand Down
1 change: 1 addition & 0 deletions packages/fast-usdc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"dependencies": {
"@agoric/client-utils": "^0.1.0",
"@agoric/cosmic-proto": "^0.4.0",
"@agoric/ertp": "^0.16.2",
"@agoric/internal": "^0.3.2",
"@agoric/notifier": "^0.6.2",
Expand Down
13 changes: 7 additions & 6 deletions packages/fast-usdc/src/cli/transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
makeVStorage,
pickEndpoint,
} from '@agoric/client-utils';
import { encodeAddressHook } from '@agoric/cosmic-proto/address-hooks.js';
import { queryFastUSDCLocalChainAccount } from '../util/agoric.js';
import { depositForBurn, makeProvider } from '../util/cctp.js';
import {
Expand All @@ -22,7 +23,7 @@ import {
const transfer = async (
/** @type {File} */ configFile,
/** @type {string} */ amount,
/** @type {string} */ destination,
/** @type {string} */ EUD,
out = console,
fetch = globalThis.fetch,
/** @type {VStorage | undefined} */ vstorage,
Expand All @@ -39,13 +40,13 @@ const transfer = async (
{ chainName: 'agoric', rpcAddrs: [pickEndpoint(netConfig)] },
);
const agoricAddr = await queryFastUSDCLocalChainAccount(vstorage, out);
const appendedAddr = `${agoricAddr}?EUD=${destination}`;
out.log(`forwarding destination ${appendedAddr}`);
const encodedAddr = encodeAddressHook(agoricAddr, { EUD });
out.log(`forwarding destination ${encodedAddr}`);

const { exists, address } = await queryForwardingAccount(
config.nobleApi,
config.nobleToAgoricChannel,
appendedAddr,
encodedAddr,
out,
fetch,
);
Expand All @@ -58,13 +59,13 @@ const transfer = async (
signer,
signerAddress,
config.nobleToAgoricChannel,
appendedAddr,
encodedAddr,
out,
);
out.log(res);
} catch (e) {
out.error(
`Error registering noble forwarding account for ${appendedAddr} on channel ${config.nobleToAgoricChannel}`,
`Error registering noble forwarding account for ${encodedAddr} on channel ${config.nobleToAgoricChannel}`,
);
throw e;
}
Expand Down
17 changes: 8 additions & 9 deletions packages/fast-usdc/src/exos/advancer.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { decodeAddressHook } from '@agoric/cosmic-proto/address-hooks.js';
import { AmountMath } from '@agoric/ertp';
import { assertAllDefined, makeTracer } from '@agoric/internal';
import { AnyNatAmountShape, ChainAddressShape } from '@agoric/orchestration';
import { pickFacet } from '@agoric/vat-data';
import { VowShape } from '@agoric/vow';
import { q } from '@endo/errors';
import { E } from '@endo/far';
import { M } from '@endo/patterns';
import { M, mustMatch } from '@endo/patterns';
import {
CctpTxEvidenceShape,
EudParamShape,
AddressHookShape,
EvmHashShape,
} from '../type-guards.js';
import { addressTools } from '../utils/address.js';
import { makeFeeTools } from '../utils/fees.js';

/**
Expand All @@ -22,7 +22,7 @@ import { makeFeeTools } from '../utils/fees.js';
* @import {ZoeTools} from '@agoric/orchestration/src/utils/zoe-tools.js';
* @import {VowTools} from '@agoric/vow';
* @import {Zone} from '@agoric/zone';
* @import {CctpTxEvidence, EvmHash, FeeConfig, LogFn, NobleAddress} from '../types.js';
* @import {CctpTxEvidence, AddressHook, EvmHash, FeeConfig, LogFn, NobleAddress} from '../types.js';
* @import {StatusManager} from './status-manager.js';
* @import {LiquidityPoolKit} from './liquidity-pool.js';
*/
Expand Down Expand Up @@ -148,11 +148,10 @@ export const prepareAdvancerKit = (

const { borrowerFacet, poolAccount } = this.state;
const { recipientAddress } = evidence.aux;
// throws if EUD is not found
const { EUD } = addressTools.getQueryParams(
recipientAddress,
EudParamShape,
);
const decoded = decodeAddressHook(recipientAddress);
mustMatch(decoded, AddressHookShape);
Copy link
Member Author

@0xpatrickdev 0xpatrickdev Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we check that cctpTxEvidence contains the correct settlementAccountAddress anywhere. At one point I thought this was a concern of the EventFeed, but maybe this is the better place to do it.

To avoid increasing scope of this PR, I'll tackle this in a follow up

const { EUD } = /** @type {AddressHook['query']} */ (decoded.query);
log(`decoded EUD: ${EUD}`);
// throws if the bech32 prefix is not found
const destination = chainHub.makeChainAddress(EUD);

Expand Down
23 changes: 14 additions & 9 deletions packages/fast-usdc/src/exos/settler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { atob } from '@endo/base64';
import { E } from '@endo/far';
import { M } from '@endo/patterns';

import { decodeAddressHook } from '@agoric/cosmic-proto/address-hooks.js';
import { PendingTxStatus } from '../constants.js';
import { addressTools } from '../utils/address.js';
import { makeFeeTools } from '../utils/fees.js';
import { EvmHashShape } from '../type-guards.js';

Expand Down Expand Up @@ -151,14 +151,19 @@ export const prepareSettler = (
return;
}

if (!addressTools.hasQueryParams(tx.receiver)) {
console.log('not query params', tx.receiver);
return;
}

const { EUD } = addressTools.getQueryParams(tx.receiver);
if (!EUD) {
console.log('no EUD parameter', tx.receiver);
let EUD;
try {
({ EUD } = decodeAddressHook(tx.receiver).query);
if (!EUD) {
log('no EUD parameter', tx.receiver);
return;
}
if (typeof EUD !== 'string') {
log('EUD is not a string', EUD);
return;
}
} catch (e) {
log('no query params', tx.receiver);
return;
}

Expand Down
10 changes: 6 additions & 4 deletions packages/fast-usdc/src/type-guards.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { PendingTxStatus } from './constants.js';
* @import {TypedPattern} from '@agoric/internal';
* @import {FastUsdcTerms} from './fast-usdc.contract.js';
* @import {USDCProposalShapes} from './pool-share-math.js';
* @import {CctpTxEvidence, FeeConfig, PendingTx, PoolMetrics, ChainPolicy, FeedPolicy} from './types.js';
* @import {CctpTxEvidence, FeeConfig, PendingTx, PoolMetrics, ChainPolicy, FeedPolicy, AddressHook} from './types.js';
*/

/**
Expand Down Expand Up @@ -67,10 +67,12 @@ export const PendingTxShape = {
};
harden(PendingTxShape);

export const EudParamShape = {
EUD: M.string(),
/** @type {TypedPattern<AddressHook>} */
export const AddressHookShape = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AddressHookShape sounds like it's for any address hook. But I can't think of something better. I guess it's not too hard to read this as "fast usdc's address hook shape".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I struggled with a good name here as well

baseAddress: M.string(),
query: { EUD: M.string() },
};
harden(EudParamShape);
harden(AddressHookShape);

const NatAmountShape = { brand: BrandShape, value: M.nat() };
/** @type {TypedPattern<FeeConfig>} */
Expand Down
9 changes: 9 additions & 0 deletions packages/fast-usdc/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,14 @@ export type FastUSDCConfig = {
assetInfo: [Denom, DenomDetail & { brandKey?: string }][];
} & CopyRecord;

/** decoded address hook parameters */
export type AddressHook = {
baseAddress: string;
query: {
/** end user destination address */
EUD: string;
};
};

export type * from './constants.js';
export type { LiquidityPoolKit } from './exos/liquidity-pool.js';
71 changes: 0 additions & 71 deletions packages/fast-usdc/src/utils/address.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Generated by [AVA](https://avajs.dev).
typeUrl: '/noble.forwarding.v1.MsgRegisterAccount',
value: {
channel: 'channel-test-7',
recipient: 'agoric123456?EUD=dydx1234',
recipient: 'agoric10rchp4vc53apxn32q42c3zryml8xq3xshyzuhjk6405wtxy7tl3d7e0f8az423pav3ukg7p3xgengqpq4066gy',
signer: 'noble09876',
},
},
Expand Down
Binary file modified packages/fast-usdc/test/cli/snapshots/transfer.test.ts.snap
Binary file not shown.
25 changes: 17 additions & 8 deletions packages/fast-usdc/test/cli/transfer.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import test from 'ava';
import { encodeAddressHook } from '@agoric/cosmic-proto/address-hooks.js';
import transfer from '../../src/cli/transfer.js';
import {
mockOut,
Expand Down Expand Up @@ -63,14 +64,18 @@ test('Transfer registers the noble forwarding account if it does not exist', asy
};
const out = mockOut();
const file = mockFile(path, JSON.stringify(config));
const agoricSettlementAccount = 'agoric123456';
const agoricSettlementAccount =
'agoric16kv2g7snfc4q24vg3pjdlnnqgngtjpwtetd2h689nz09lcklvh5s8u37ek';
const settlementAccountVstoragePath = 'published.fastUsdc.settlementAccount';
const vstorageMock = makeVstorageMock({
[settlementAccountVstoragePath]: agoricSettlementAccount,
});
const amount = '150';
const destination = 'dydx1234';
const nobleFwdAccountQuery = `${nobleApi}/noble/forwarding/v1/address/${nobleToAgoricChannel}/${agoricSettlementAccount}${encodeURIComponent('?EUD=')}${destination}/`;
const EUD = 'dydx1234';
const nobleFwdAccountQuery = `${nobleApi}/noble/forwarding/v1/address/${nobleToAgoricChannel}/${encodeAddressHook(
agoricSettlementAccount,
{ EUD },
)}/`;
const fetchMock = makeFetchMock({
[nobleFwdAccountQuery]: {
address: 'noble14lwerrcfzkzrv626w49pkzgna4dtga8c5x479h',
Expand All @@ -84,7 +89,7 @@ test('Transfer registers the noble forwarding account if it does not exist', asy
await transfer.transfer(
file,
amount,
destination,
EUD,
// @ts-expect-error mocking console
out,
fetchMock.fetch,
Expand Down Expand Up @@ -114,14 +119,18 @@ test('Transfer signs and broadcasts the depositForBurn message on Ethereum', asy
};
const out = mockOut();
const file = mockFile(path, JSON.stringify(config));
const agoricSettlementAccount = 'agoric123456';
const agoricSettlementAccount =
'agoric16kv2g7snfc4q24vg3pjdlnnqgngtjpwtetd2h689nz09lcklvh5s8u37ek';
const settlementAccountVstoragePath = 'published.fastUsdc.settlementAccount';
const vstorageMock = makeVstorageMock({
[settlementAccountVstoragePath]: agoricSettlementAccount,
});
const amount = '150';
const destination = 'dydx1234';
const nobleFwdAccountQuery = `${nobleApi}/noble/forwarding/v1/address/${nobleToAgoricChannel}/${agoricSettlementAccount}${encodeURIComponent('?EUD=')}${destination}/`;
const EUD = 'dydx1234';
const nobleFwdAccountQuery = `${nobleApi}/noble/forwarding/v1/address/${nobleToAgoricChannel}/${encodeAddressHook(
agoricSettlementAccount,
{ EUD },
)}/`;
const fetchMock = makeFetchMock({
[nobleFwdAccountQuery]: {
address: 'noble14lwerrcfzkzrv626w49pkzgna4dtga8c5x479h',
Expand All @@ -135,7 +144,7 @@ test('Transfer signs and broadcasts the depositForBurn message on Ethereum', asy
await transfer.transfer(
file,
amount,
destination,
EUD,
// @ts-expect-error mocking console
out,
fetchMock.fetch,
Expand Down
Loading
Loading