Skip to content

Commit f796f33

Browse files
committed
chore: refactored aleo helpers
1 parent 6497bf9 commit f796f33

3 files changed

Lines changed: 44 additions & 57 deletions

File tree

typescript/aleo-sdk/src/clients/base.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
getOrInitConsensusVersionTestHeights,
2424
} from '@provablehq/sdk/testnet.js';
2525

26-
import { assert } from '@hyperlane-xyz/utils';
26+
import { assert, strip0x } from '@hyperlane-xyz/utils';
2727

2828
import { mailbox } from '../artifacts.js';
2929

@@ -66,10 +66,6 @@ export class AleoBase {
6666
return this.chainId ? TestnetPlaintext : MainnetPlaintext;
6767
}
6868

69-
protected get U128() {
70-
return this.chainId ? TestnetU128 : MainnetU128;
71-
}
72-
7369
protected get BHP256() {
7470
return this.chainId ? TestnetBHP256 : MainnetBHP256;
7571
}
@@ -129,4 +125,37 @@ export class AleoBase {
129125
.address()
130126
.to_string();
131127
}
128+
129+
protected stringToU128String(input: string): string {
130+
if (input.length > 16) {
131+
throw new Error(`string "${input}" is too long to convert it into U128`);
132+
}
133+
134+
const encoded = new TextEncoder().encode(input);
135+
const bytes = new Uint8Array(16);
136+
bytes.set(encoded.subarray(0, 16));
137+
138+
const U128 = this.chainId ? TestnetU128 : MainnetU128;
139+
return U128.fromBytesLe(bytes).toString();
140+
}
141+
142+
protected U128StringToString(input: string): string {
143+
const U128 = this.chainId ? TestnetU128 : MainnetU128;
144+
return new TextDecoder().decode(
145+
U128.fromString(input)
146+
.toBytesLe()
147+
.filter((b) => b > 0),
148+
);
149+
}
150+
151+
protected bytes32ToU128String(input: string): string {
152+
const bytes = Buffer.from(strip0x(input), 'hex');
153+
154+
// Split into two 128-bit chunks
155+
const lowBytes = Uint8Array.from(bytes.subarray(0, 16));
156+
const highBytes = Uint8Array.from(bytes.subarray(16, 32));
157+
158+
const U128 = this.chainId ? TestnetU128 : MainnetU128;
159+
return `[${U128.fromBytesLe(lowBytes).toString()},${U128.fromBytesLe(highBytes).toString()}]`;
160+
}
132161
}

typescript/aleo-sdk/src/clients/provider.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@ import { BigNumber } from 'bignumber.js';
22

33
import { AltVM, assert, ensure0x, strip0x } from '@hyperlane-xyz/utils';
44

5-
import {
6-
ALEO_NULL_ADDRESS,
7-
U128ToString,
8-
formatAddress,
9-
splitToU128,
10-
stringToU128,
11-
} from '../utils/helper.js';
5+
import { ALEO_NULL_ADDRESS, formatAddress } from '../utils/helper.js';
126
import { AleoTransaction } from '../utils/types.js';
137

148
import { AleoBase } from './base.js';
@@ -151,12 +145,12 @@ export class AleoProvider extends AleoBase implements AltVM.IProvider {
151145
async isMessageDelivered(req: AltVM.ReqIsMessageDelivered): Promise<boolean> {
152146
try {
153147
// Message key needs to be separated into [u128, u128] using Little Endian.
154-
const messageKey = splitToU128(req.messageId);
148+
const messageKey = this.bytes32ToU128String(req.messageId);
155149

156150
const res = await this.aleoClient.getProgramMappingPlaintext(
157151
req.mailboxAddress,
158152
'deliveries',
159-
`{id:[${messageKey[0].toString()},${messageKey[1].toString()}]}`,
153+
`{id:${messageKey}}`,
160154
);
161155

162156
const obj = res.toObject();
@@ -462,15 +456,11 @@ export class AleoProvider extends AleoBase implements AltVM.IProvider {
462456
const metadata = tokenMetadata.toObject();
463457

464458
return {
465-
name: U128ToString(
466-
this.U128.fromString(
467-
`${tokenMetadata.toObject()['name'].toString()}u128`,
468-
),
459+
name: this.U128StringToString(
460+
`${tokenMetadata.toObject()['name'].toString()}u128`,
469461
),
470-
symbol: U128ToString(
471-
this.U128.fromString(
472-
`${tokenMetadata.toObject()['symbol'].toString()}u128`,
473-
),
462+
symbol: this.U128StringToString(
463+
`${tokenMetadata.toObject()['symbol'].toString()}u128`,
474464
),
475465
decimals: metadata['decimals'],
476466
};
@@ -982,8 +972,8 @@ export class AleoProvider extends AleoBase implements AltVM.IProvider {
982972
priorityFee: 0,
983973
privateFee: false,
984974
inputs: [
985-
stringToU128(req.name).toString(),
986-
stringToU128(req.denom).toString(),
975+
this.stringToU128String(req.name),
976+
this.stringToU128String(req.denom),
987977
`${req.decimals}u8`,
988978
`${req.decimals}u8`,
989979
],
@@ -1097,9 +1087,7 @@ export class AleoProvider extends AleoBase implements AltVM.IProvider {
10971087
);
10981088
}
10991089

1100-
const recipient = `[${splitToU128(req.recipient)
1101-
.map((u) => u.toString())
1102-
.join(',')}]`;
1090+
const recipient = this.bytes32ToU128String(req.recipient);
11031091

11041092
const creditAllowance = Array(4).fill(
11051093
`{spender:${ALEO_NULL_ADDRESS},amount:0u8}`,
Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,6 @@
1-
import { U128 } from '@provablehq/wasm';
2-
3-
import { strip0x } from '@hyperlane-xyz/utils';
4-
51
export const ALEO_NULL_ADDRESS =
62
'aleo1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3ljyzc';
73

8-
export function splitToU128(messageId: string): [U128, U128] {
9-
const bytes = Buffer.from(strip0x(messageId), 'hex');
10-
11-
// Split into two 128-bit chunks
12-
const lowBytes = Uint8Array.from(bytes.subarray(0, 16));
13-
const highBytes = Uint8Array.from(bytes.subarray(16, 32));
14-
15-
return [U128.fromBytesLe(lowBytes), U128.fromBytesLe(highBytes)];
16-
}
17-
184
export function formatAddress(address: string): string {
195
return address === ALEO_NULL_ADDRESS ? '' : address;
206
}
21-
22-
export function stringToU128(input: string): U128 {
23-
if (input.length > 16) {
24-
throw new Error(`string "${input}" is too long to convert it into U128`);
25-
}
26-
27-
const encoded = new TextEncoder().encode(input);
28-
const bytes = new Uint8Array(16);
29-
bytes.set(encoded.subarray(0, 16));
30-
31-
return U128.fromBytesLe(bytes);
32-
}
33-
34-
export function U128ToString(input: U128): string {
35-
return new TextDecoder().decode(input.toBytesLe().filter((b) => b > 0));
36-
}

0 commit comments

Comments
 (0)