Skip to content

Commit 0370cf4

Browse files
committed
fix: deploy aleo programs
1 parent ef39d4f commit 0370cf4

3 files changed

Lines changed: 96 additions & 54 deletions

File tree

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

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
Plaintext,
66
Program,
77
ProgramManager,
8+
getOrInitConsensusVersionTestHeights,
9+
initThreadPool,
810
} from '@provablehq/sdk';
911
import { BigNumber } from 'bignumber.js';
1012

@@ -14,12 +16,8 @@ import { mailbox } from '../artifacts.js';
1416
import { formatAddress, getMessageKey } from '../utils/helper.js';
1517
import { AleoTransaction } from '../utils/types.js';
1618

17-
// TODO: make denom in AltVM optional
18-
// TODO: add remove destination gas config method in AltVM
19-
// TODO: only allow domainId in createMailox in AltVM
20-
// TODO: add createNoopHook method in AltVM
21-
// TODO: add getTokenMetadata method in AltVM
22-
// TODO: don't allow routes in create routing ism
19+
getOrInitConsensusVersionTestHeights('0,1,2,3,4,5,6,7,8,9,10');
20+
await initThreadPool();
2321

2422
export class AleoProvider implements AltVM.IProvider {
2523
protected readonly aleoClient: AleoNetworkClient;
@@ -39,6 +37,16 @@ export class AleoProvider implements AltVM.IProvider {
3937
this.aleoClient = new AleoNetworkClient(rpcUrls[0]);
4038
}
4139

40+
protected getProgramSalt(address: string): string {
41+
// get the program salt by hashing the address with bhp256 and take the first 12
42+
// characters from the hex hash
43+
return new BHP256()
44+
.hash(Plaintext.fromString(address).toBitsLe())
45+
.toBytesLe()
46+
.reduce((acc, b) => acc + b.toString(16).padStart(2, '0'), '')
47+
.slice(0, 12);
48+
}
49+
4250
// ### QUERY BASE ###
4351

4452
async isHealthy() {
@@ -120,7 +128,7 @@ export class AleoProvider implements AltVM.IProvider {
120128
let res;
121129
try {
122130
res = await this.aleoClient.getProgramMappingPlaintext(
123-
req.mailboxAddress,
131+
`mailbox_${this.getProgramSalt(req.mailboxAddress)}.aleo`,
124132
'mailbox',
125133
'true',
126134
);
@@ -584,7 +592,7 @@ export class AleoProvider implements AltVM.IProvider {
584592
req: AltVM.ReqCreateMailbox,
585593
): Promise<AleoTransaction> {
586594
return {
587-
programName: 'mailbox.aleo',
595+
programName: '',
588596
functionName: 'init',
589597
priorityFee: 0,
590598
privateFee: false,
@@ -596,7 +604,7 @@ export class AleoProvider implements AltVM.IProvider {
596604
req: AltVM.ReqSetDefaultIsm,
597605
): Promise<AleoTransaction> {
598606
return {
599-
programName: 'mailbox.aleo',
607+
programName: `mailbox_${this.getProgramSalt(req.mailboxAddress)}.aleo`,
600608
functionName: 'set_default_ism',
601609
priorityFee: 0,
602610
privateFee: false,
@@ -608,7 +616,7 @@ export class AleoProvider implements AltVM.IProvider {
608616
req: AltVM.ReqSetDefaultHook,
609617
): Promise<AleoTransaction> {
610618
return {
611-
programName: 'mailbox.aleo',
619+
programName: `mailbox_${this.getProgramSalt(req.mailboxAddress)}.aleo`,
612620
functionName: 'set_default_hook',
613621
priorityFee: 0,
614622
privateFee: false,
@@ -620,7 +628,7 @@ export class AleoProvider implements AltVM.IProvider {
620628
req: AltVM.ReqSetRequiredHook,
621629
): Promise<AleoTransaction> {
622630
return {
623-
programName: 'mailbox.aleo',
631+
programName: `mailbox_${this.getProgramSalt(req.mailboxAddress)}.aleo`,
624632
functionName: 'set_required_hook',
625633
priorityFee: 0,
626634
privateFee: false,
@@ -632,7 +640,7 @@ export class AleoProvider implements AltVM.IProvider {
632640
req: AltVM.ReqSetMailboxOwner,
633641
): Promise<AleoTransaction> {
634642
return {
635-
programName: 'mailbox.aleo',
643+
programName: `mailbox_${this.getProgramSalt(req.mailboxAddress)}.aleo`,
636644
functionName: 'set_owner',
637645
priorityFee: 0,
638646
privateFee: false,
@@ -743,15 +751,23 @@ export class AleoProvider implements AltVM.IProvider {
743751
}
744752

745753
async getCreateMerkleTreeHookTransaction(
746-
_req: AltVM.ReqCreateMerkleTreeHook,
754+
req: AltVM.ReqCreateMerkleTreeHook,
747755
): Promise<AleoTransaction> {
748-
// TODO: replace default mailbox id with real one
749756
return {
750757
programName: 'hook_manager.aleo',
751758
functionName: 'init_merkle_tree',
752759
priorityFee: 0,
753760
privateFee: false,
754-
inputs: [Program.fromString(mailbox).address().to_string()],
761+
inputs: [
762+
Program.fromString(
763+
mailbox.replaceAll(
764+
'mailbox.aleo',
765+
`mailbox_${this.getProgramSalt(req.mailboxAddress)}.aleo`,
766+
),
767+
)
768+
.address()
769+
.to_string(),
770+
],
755771
};
756772
}
757773

@@ -803,12 +819,19 @@ export class AleoProvider implements AltVM.IProvider {
803819
});
804820

805821
return {
806-
programName: 'validator_announce.aleo',
822+
programName: '',
807823
functionName: 'init',
808824
priorityFee: 0,
809825
privateFee: false,
810826
inputs: [
811-
Program.fromString(mailbox).address().to_string(),
827+
Program.fromString(
828+
mailbox.replaceAll(
829+
'mailbox.aleo',
830+
`mailbox_${this.getProgramSalt(req.mailboxAddress)}.aleo`,
831+
),
832+
)
833+
.address()
834+
.to_string(),
812835
`${localDomain}u32`,
813836
],
814837
};

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

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
NetworkRecordProvider,
55
Program,
66
ProgramManager,
7-
ProgramManagerBase,
87
} from '@provablehq/sdk';
98

109
import { AltVM } from '@hyperlane-xyz/utils';
@@ -54,8 +53,25 @@ export class AleoSigner
5453
this.programManager.setAccount(this.aleoAccount);
5554
}
5655

57-
private async deployProgram(programName: string): Promise<Program[]> {
58-
const programs = loadProgramsInDeployOrder(programName);
56+
private async deployProgram(programName: string): Promise<{
57+
address: string;
58+
program: Program;
59+
}> {
60+
let programs = loadProgramsInDeployOrder(programName);
61+
62+
const address = new Account().address().to_string();
63+
const salt = this.getProgramSalt(address);
64+
65+
programs = programs.map((p) => {
66+
return Program.fromString(
67+
p
68+
.toString()
69+
.replaceAll(
70+
/(mailbox|dispatch_proxy|validator_announce)\.aleo/g,
71+
(_, p1) => `${p1}_${salt}.aleo`,
72+
),
73+
);
74+
});
5975

6076
for (const program of programs) {
6177
const isDeployed = await this.isProgramDeployed(program);
@@ -66,20 +82,19 @@ export class AleoSigner
6682
continue;
6783
}
6884

69-
const fee = await ProgramManagerBase.estimateDeploymentFee(
70-
program.toString(),
71-
);
72-
7385
const txId = await this.programManager.deploy(
7486
program.toString(),
75-
Math.ceil(Number(fee) / 10 ** 6),
87+
0,
7688
false,
7789
);
7890

7991
await this.aleoClient.waitForTransactionConfirmation(txId);
8092
}
8193

82-
return programs;
94+
return {
95+
address,
96+
program: programs[programs.length - 1],
97+
};
8398
}
8499

85100
getSignerAddress(): string {
@@ -123,19 +138,20 @@ export class AleoSigner
123138
async createMailbox(
124139
req: Omit<AltVM.ReqCreateMailbox, 'signer'>,
125140
): Promise<AltVM.ResCreateMailbox> {
126-
await this.deployProgram('dispatch_proxy');
141+
const { address } = await this.deployProgram('dispatch_proxy');
127142

128-
// TODO: init with correct mailbox id
129143
const tx = await this.getCreateMailboxTransaction({
130144
signer: this.getSignerAddress(),
131145
...req,
132146
});
133147

148+
tx.programName = `mailbox_${this.getProgramSalt(address)}.aleo`;
149+
134150
const txId = await this.programManager.execute(tx);
135151
await this.aleoClient.waitForTransactionConfirmation(txId);
136152

137153
return {
138-
mailboxAddress: 'mailbox.aleo',
154+
mailboxAddress: address,
139155
};
140156
}
141157

@@ -482,19 +498,20 @@ export class AleoSigner
482498
async createValidatorAnnounce(
483499
req: Omit<AltVM.ReqCreateValidatorAnnounce, 'signer'>,
484500
): Promise<AltVM.ResCreateValidatorAnnounce> {
485-
await this.deployProgram('validator_announce');
501+
const { address } = await this.deployProgram('validator_announce');
486502

487-
// TODO: init with correct validator announce and mailbox id
488503
const tx = await this.getCreateValidatorAnnounceTransaction({
489504
signer: this.getSignerAddress(),
490505
...req,
491506
});
492507

508+
tx.programName = `validator_announce_${this.getProgramSalt(address)}.aleo`;
509+
493510
const txId = await this.programManager.execute(tx);
494511
await this.aleoClient.waitForTransactionConfirmation(txId);
495512

496513
return {
497-
validatorAnnounceId: 'validator_announce.aleo',
514+
validatorAnnounceId: address,
498515
};
499516
}
500517

typescript/aleo-sdk/src/deploy.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
1-
import { AleoProvider } from './clients/provider.js';
21
import { AleoSigner } from './clients/signer.js';
32

43
const main = async () => {
5-
const localnetRpc = 'http://localhost:3030';
6-
const provider = await AleoProvider.connect([localnetRpc], '');
4+
try {
5+
const localnetRpc = 'http://localhost:3030';
76

8-
const latestBlockHeight = await provider.getHeight();
9-
console.log('latestBlockHeight: ', latestBlockHeight);
7+
// test private key with funds
8+
const privateKey =
9+
'APrivateKey1zkp8CZNn3yeCseEtxuVPbDCwSyhGW6yZKUYKfgXmcpoGPWH';
10+
const signer = await AleoSigner.connectWithSigner(
11+
[localnetRpc],
12+
privateKey,
13+
);
1014

11-
// test private key with funds
12-
const privateKey =
13-
'APrivateKey1zkp8CZNn3yeCseEtxuVPbDCwSyhGW6yZKUYKfgXmcpoGPWH';
14-
const signer = await AleoSigner.connectWithSigner([localnetRpc], privateKey);
15+
const address = signer.getSignerAddress();
16+
console.log('signer address: ', address);
1517

16-
const address = signer.getSignerAddress();
17-
console.log('signer address: ', address);
18+
const balance = await signer.getBalance({
19+
address,
20+
denom: '',
21+
});
22+
console.log('signer credits balance: ', balance);
1823

19-
const balance = await signer.getBalance({
20-
address,
21-
denom: '',
22-
});
23-
console.log('signer credits balance: ', balance);
24-
25-
const { mailboxAddress } = await signer.createMailbox({
26-
domainId: 1337,
27-
defaultIsmAddress: '',
28-
});
29-
console.log('mailboxAddress', mailboxAddress);
24+
const { mailboxAddress } = await signer.createMailbox({
25+
domainId: 1337,
26+
defaultIsmAddress: '',
27+
});
28+
console.log('mailboxAddress', mailboxAddress);
29+
} catch (err) {
30+
console.log(err);
31+
}
3032
};
3133

3234
main();

0 commit comments

Comments
 (0)