Skip to content

Commit eb209de

Browse files
committed
chore: created helpers
1 parent 9408ce0 commit eb209de

3 files changed

Lines changed: 37 additions & 41 deletions

File tree

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

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

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

5-
import { ALEO_NULL_ADDRESS, formatAddress } from '../utils/helper.js';
5+
import {
6+
ALEO_NULL_ADDRESS,
7+
arrayToPlaintext,
8+
fillArray,
9+
formatAddress,
10+
} from '../utils/helper.js';
611
import { AleoTransaction } from '../utils/types.js';
712

813
import { AleoBase } from './base.js';
@@ -750,23 +755,21 @@ export class AleoProvider extends AleoBase implements AltVM.IProvider {
750755
throw new Error(`maximum ${MAXIMUM_VALIDATORS} validators allowed`);
751756
}
752757

753-
const validators = Array(MAXIMUM_VALIDATORS).fill({
754-
bytes: Array(20).fill(`0u8`),
755-
});
756-
757-
req.validators
758-
.map((v) => ({
758+
const validators = fillArray(
759+
req.validators.map((v) => ({
759760
bytes: [...Buffer.from(strip0x(v), 'hex')].map((b) => `${b}u8`),
760-
}))
761-
.forEach((v, i) => (validators[i] = v));
761+
})),
762+
MAXIMUM_VALIDATORS,
763+
Array(20).fill(`0u8`),
764+
);
762765

763766
return {
764767
programName: 'ism_manager.aleo',
765768
functionName: 'init_message_id_multisig',
766769
priorityFee: 0,
767770
privateFee: false,
768771
inputs: [
769-
JSON.stringify(validators).replaceAll('"', ''),
772+
arrayToPlaintext(validators),
770773
`${req.validators.length}u8`,
771774
`${req.threshold}u8`,
772775
],
@@ -1026,16 +1029,14 @@ export class AleoProvider extends AleoBase implements AltVM.IProvider {
10261029
...Buffer.from(strip0x(req.remoteRouter.receiverAddress), 'hex'),
10271030
].map((b) => `${b}u8`);
10281031

1029-
console.log(JSON.stringify(bytes).replaceAll('"', ''));
1030-
10311032
return {
10321033
programName: req.tokenAddress,
10331034
functionName: 'enroll_remote_router',
10341035
priorityFee: 0,
10351036
privateFee: false,
10361037
inputs: [
10371038
`${req.remoteRouter.receiverDomainId}u32`,
1038-
JSON.stringify(bytes).replaceAll('"', ''),
1039+
arrayToPlaintext(bytes),
10391040
`${req.remoteRouter.gas}u128`,
10401041
],
10411042
// skipProof: this.skipProof,
@@ -1153,7 +1154,7 @@ export class AleoProvider extends AleoBase implements AltVM.IProvider {
11531154
const hookMetadata = Array(256).fill(`0u8`);
11541155

11551156
if (req.customHookMetadata) {
1156-
Buffer.from(req.customHookMetadata, 'hex').forEach((b, i) => {
1157+
Buffer.from(strip0x(req.customHookMetadata), 'hex').forEach((b, i) => {
11571158
hookMetadata[i] = `${b}u8`;
11581159
});
11591160
}
@@ -1170,9 +1171,9 @@ export class AleoProvider extends AleoBase implements AltVM.IProvider {
11701171
`${req.destinationDomainId}u8`,
11711172
recipient,
11721173
`${req.amount}u128`,
1173-
JSON.stringify(creditAllowance).replaceAll('"', ''),
1174+
arrayToPlaintext(creditAllowance),
11741175
req.customHookAddress,
1175-
JSON.stringify(hookMetadata).replaceAll('"', ''),
1176+
arrayToPlaintext(hookMetadata),
11761177
],
11771178
// skipProof: this.skipProof,
11781179
};
@@ -1190,7 +1191,7 @@ export class AleoProvider extends AleoBase implements AltVM.IProvider {
11901191
`${req.destinationDomainId}u8`,
11911192
recipient,
11921193
`${req.amount}u128`,
1193-
JSON.stringify(creditAllowance).replaceAll('"', ''),
1194+
arrayToPlaintext(creditAllowance),
11941195
],
11951196
// skipProof: this.skipProof,
11961197
};

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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AltVM, assert } from '@hyperlane-xyz/utils';
22

33
import { loadProgramsInDeployOrder } from '../artifacts.js';
4+
import { programIdToPlaintext } from '../utils/helper.js';
45
import { AleoReceipt, AleoTransaction } from '../utils/types.js';
56

67
import { AnyProgramManager } from './base.js';
@@ -587,14 +588,7 @@ export class AleoSigner
587588
const tokenAddress = programs[programs.length - 1];
588589
tx.programName = tokenAddress;
589590

590-
let programId = Array(128).fill(`0u8`);
591-
Array.from(tokenAddress)
592-
.map((c) => `${c.charCodeAt(0)}u8`)
593-
.forEach((c, i) => {
594-
programId[i] = c;
595-
});
596-
597-
tx.inputs = [JSON.stringify(programId).replaceAll('"', ''), ...tx.inputs];
591+
tx.inputs = [programIdToPlaintext(tokenAddress), ...tx.inputs];
598592

599593
const txId = await this.programManager.execute(tx);
600594
await this.aleoClient.waitForTransactionConfirmation(txId);
@@ -626,14 +620,7 @@ export class AleoSigner
626620
const tokenAddress = programs[programs.length - 1];
627621
tx.programName = tokenAddress;
628622

629-
let programId = Array(128).fill(`0u8`);
630-
Array.from(tokenAddress)
631-
.map((c) => `${c.charCodeAt(0)}u8`)
632-
.forEach((c, i) => {
633-
programId[i] = c;
634-
});
635-
636-
tx.inputs = [JSON.stringify(programId).replaceAll('"', ''), ...tx.inputs];
623+
tx.inputs = [programIdToPlaintext(tokenAddress), ...tx.inputs];
637624

638625
const txId = await this.programManager.execute(tx);
639626
await this.aleoClient.waitForTransactionConfirmation(txId);
@@ -663,14 +650,7 @@ export class AleoSigner
663650
const tokenAddress = programs[programs.length - 1];
664651
tx.programName = tokenAddress;
665652

666-
let programId = Array(128).fill(`0u8`);
667-
Array.from(tokenAddress)
668-
.map((c) => `${c.charCodeAt(0)}u8`)
669-
.forEach((c, i) => {
670-
programId[i] = c;
671-
});
672-
673-
tx.inputs = [JSON.stringify(programId).replaceAll('"', ''), ...tx.inputs];
653+
tx.inputs = [programIdToPlaintext(tokenAddress), ...tx.inputs];
674654

675655
const txId = await this.programManager.execute(tx);
676656
await this.aleoClient.waitForTransactionConfirmation(txId);

typescript/aleo-sdk/src/utils/helper.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,18 @@ export const ALEO_NULL_ADDRESS =
44
export function formatAddress(address: string): string {
55
return address === ALEO_NULL_ADDRESS ? '' : address;
66
}
7+
8+
export function fillArray(array: any[], length: number, fillValue: any): any[] {
9+
return array.length < length
10+
? [...array, ...Array(length - array.length).fill(fillValue)]
11+
: array.slice(0, length);
12+
}
13+
14+
export function arrayToPlaintext(array: string[]): string {
15+
return `[${array.join(',')}]`;
16+
}
17+
18+
export function programIdToPlaintext(programId: string): string {
19+
const bytes = Array.from(programId).map((c) => `${c.charCodeAt(0)}u8`);
20+
return arrayToPlaintext(fillArray(bytes, 128, `u8`));
21+
}

0 commit comments

Comments
 (0)