Skip to content

Commit 8057464

Browse files
Update offline example in create-leo-app
1 parent 59989dc commit 8057464

File tree

3 files changed

+75
-47
lines changed

3 files changed

+75
-47
lines changed

create-leo-app/template-offline-public-transaction-ts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"type": "module",
66
"scripts": {
77
"build": "rimraf dist/js && rollup --config",
8-
"dev": "npm run build && node dist/index.js"
8+
"dev": "npm run build && node --trace-uncaught dist/index.js"
99
},
1010
"dependencies": {
1111
"@provablehq/sdk": "^0.9.4"

create-leo-app/template-offline-public-transaction-ts/src/helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async function preDownloadTransferKeys() {
2828
const keysDirPath = path.join(__dirname, "keys");
2929
await fsPromises.mkdir(keysDirPath, { recursive: true });
3030

31-
for (const keyData of [CREDITS_PROGRAM_KEYS.transfer_public, CREDITS_PROGRAM_KEYS.fee_public, CREDITS_PROGRAM_KEYS.transfer_public_as_signer]) {
31+
for (const keyData of [CREDITS_PROGRAM_KEYS.transfer_public, CREDITS_PROGRAM_KEYS.fee_public, CREDITS_PROGRAM_KEYS.transfer_public_as_signer, CREDITS_PROGRAM_KEYS.inclusion]) {
3232
try {
3333
keyPaths[keyData.locator] = await downloadAndSaveKey(keyData, keysDirPath);
3434
} catch (error) {
@@ -45,7 +45,7 @@ async function preDownloadBondingKeys() {
4545
const keysDirPath = path.join(__dirname, "keys");
4646
await fsPromises.mkdir(keysDirPath, { recursive: true });
4747

48-
for (const keyData of [CREDITS_PROGRAM_KEYS.bond_public, CREDITS_PROGRAM_KEYS.fee_public, CREDITS_PROGRAM_KEYS.unbond_public, CREDITS_PROGRAM_KEYS.claim_unbond_public]) {
48+
for (const keyData of [CREDITS_PROGRAM_KEYS.bond_public, CREDITS_PROGRAM_KEYS.fee_public, CREDITS_PROGRAM_KEYS.unbond_public, CREDITS_PROGRAM_KEYS.claim_unbond_public, CREDITS_PROGRAM_KEYS.inclusion]) {
4949
try {
5050
keyPaths[keyData.locator] = await downloadAndSaveKey(keyData, keysDirPath);
5151
} catch (error) {

create-leo-app/template-offline-public-transaction-ts/src/index.ts

Lines changed: 72 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
import {Account, Address, CREDITS_PROGRAM_KEYS, initThreadPool, ProgramManager, OfflineQuery, OfflineKeyProvider, OfflineSearchParams, ProvingKey, Transaction} from "@provablehq/sdk";
1+
import {
2+
Account,
3+
Address,
4+
CREDITS_PROGRAM_KEYS,
5+
initThreadPool,
6+
ProgramManager,
7+
OfflineQuery,
8+
OfflineKeyProvider,
9+
OfflineSearchParams,
10+
ProvingKey,
11+
Transaction,
12+
VerifyingKey
13+
} from "@provablehq/sdk";
214
import { getLocalKey, preDownloadBondingKeys, preDownloadTransferKeys } from "./helpers";
315

416
await initThreadPool();
@@ -19,6 +31,9 @@ async function buildTransferPublicTxOffline(recipientAddress: Address, amount: n
1931
const transferPublicProvingKey = ProvingKey.fromBytes(
2032
await getLocalKey(<string>keyPaths[CREDITS_PROGRAM_KEYS.transfer_public.locator])
2133
);
34+
const inclusionKey = ProvingKey.fromBytes(
35+
await getLocalKey(<string>keyPaths[CREDITS_PROGRAM_KEYS.inclusion.locator])
36+
);
2237

2338
// Create an offline key provider
2439
console.log("Creating offline key provider");
@@ -28,25 +43,25 @@ async function buildTransferPublicTxOffline(recipientAddress: Address, amount: n
2843
// keys into the key manager.
2944
console.log("Inserting proving keys into key provider");
3045
offlineKeyProvider.insertFeePublicKeys(feePublicProvingKey);
46+
offlineKeyProvider.cacheKeys(CREDITS_PROGRAM_KEYS.inclusion.locator, [inclusionKey, VerifyingKey.inclusionVerifier()]);
3147

32-
try {
33-
offlineKeyProvider.insertTransferPublicKeys(transferPublicProvingKey);
34-
console.log("Successfully inserted proving key");
35-
} catch (err) {
36-
console.error("Failed to insert proving key:", err);
37-
}
38-
48+
try {
49+
offlineKeyProvider.insertTransferPublicKeys(transferPublicProvingKey);
50+
console.log("Successfully inserted proving key");
51+
} catch (err) {
52+
console.error("Failed to insert proving key:", err);
53+
}
3954

4055
// Create an offline query to complete the inclusion proof
4156
let offlineQuery: OfflineQuery;
4257
const blockHeight = 0;
4358
// TODO this is a placeholder block height for now, which offlineQuery now requires
44-
try {
45-
const offlineQuery = new OfflineQuery(blockHeight, latestStateRoot);
46-
console.log("Successfully created OfflineQuery", offlineQuery);
47-
} catch (err) {
48-
console.error("Failed to create OfflineQuery:", err);
49-
}
59+
try {
60+
offlineQuery = new OfflineQuery(blockHeight, latestStateRoot);
61+
console.log("Successfully created OfflineQuery", offlineQuery);
62+
} catch (err) {
63+
console.error("Failed to create OfflineQuery:", err);
64+
}
5065

5166
// Insert the key provider into the program manager
5267
programManager.setKeyProvider(offlineKeyProvider);
@@ -83,10 +98,12 @@ async function buildBondingTxOffline(
8398
const bondPublicKeyBytes = await getLocalKey(<string>keyPaths[CREDITS_PROGRAM_KEYS.bond_public.locator]);
8499
const unbondPublicKeyBytes = await getLocalKey(<string>keyPaths[CREDITS_PROGRAM_KEYS.unbond_public.locator]);
85100
const claimUnbondPublicKeyBytes = await getLocalKey(<string>keyPaths[CREDITS_PROGRAM_KEYS.claim_unbond_public.locator]);
101+
const inclusionKeys = await getLocalKey(<string>keyPaths[CREDITS_PROGRAM_KEYS.inclusion.locator]);
86102
const feePublicProvingKey = ProvingKey.fromBytes(feePublicKeyBytes);
87103
const bondPublicProvingKey = ProvingKey.fromBytes(bondPublicKeyBytes);
88104
const unBondPublicProvingKey = ProvingKey.fromBytes(unbondPublicKeyBytes);
89105
const claimUnbondPublicProvingKey = ProvingKey.fromBytes(claimUnbondPublicKeyBytes);
106+
const inclusionProvingKey = ProvingKey.fromBytes(inclusionKeys);
90107

91108
// Create an offline key provider to fetch keys without connection to the internet
92109
console.log("Creating offline key provider");
@@ -98,6 +115,7 @@ async function buildBondingTxOffline(
98115
offlineKeyProvider.insertBondPublicKeys(bondPublicProvingKey);
99116
offlineKeyProvider.insertUnbondPublicKeys(unBondPublicProvingKey);
100117
offlineKeyProvider.insertClaimUnbondPublicKeys(claimUnbondPublicProvingKey);
118+
offlineKeyProvider.cacheKeys(CREDITS_PROGRAM_KEYS.inclusion.locator, [inclusionProvingKey, VerifyingKey.inclusionVerifier()]);
101119

102120
// Insert the key provider into the program manager
103121
programManager.setKeyProvider(offlineKeyProvider);
@@ -114,7 +132,7 @@ async function buildBondingTxOffline(
114132
offlineQuery: new OfflineQuery(0, latestStateRoot)
115133
};
116134

117-
135+
console.log("Build bond public transaction");
118136
const bondTx = <Transaction>await programManager.buildBondPublicTransaction(
119137
validatorAddress.to_string(),
120138
withdrawalAddress.to_string(),
@@ -150,38 +168,48 @@ async function buildBondingTxOffline(
150168
return [bondTx, unBondTx, claimUnbondTx];
151169
}
152170

153-
// -------------------ONLINE COMPONENT---------------------
154-
// (Do this part on an internet connected machine)
155-
156-
// Download the needed keys for the functions we want to execute offline
157-
const transferKeyPaths = await preDownloadTransferKeys();
158-
const bondingKeyPaths = await preDownloadBondingKeys();
159-
//---------------------------------------------------------
160-
161-
// ------------------OFFLINE COMPONENT---------------------
162-
// (Do this part on an offline machine)
163-
// Get the latest state root from an online machine and enter it into an offline machine
164-
const latestStateRoot = "sr1p93gpsezrjzdhcd2wujznx5s07k8qa39t6vfcej35zew8vn2jyrs46te8q";
165-
166-
// Build a transfer_public transaction
167171
const stakerAddress = new Account().address();
168172
const validatorAddress = new Account().address();
169173
const withdrawalAddress = new Account().address();
170-
const transferTx = await buildTransferPublicTxOffline(stakerAddress, 10000, latestStateRoot, transferKeyPaths);
171-
console.log("Transfer transaction built offline!");
172-
console.log(`\n---------------transfer_public transaction---------------\n${transferTx}`);
173-
console.log(`---------------------------------------------------------`);
174-
175-
// Build bonding & unbonding transactions
176-
const bondTransactions = await buildBondingTxOffline(validatorAddress, withdrawalAddress, 100, latestStateRoot, bondingKeyPaths);
177-
console.log("Bonding transactions built offline!");
178-
console.log(`\n-----------------bond_public transaction-----------------\n${bondTransactions[0]}`);
179-
console.log(`---------------------------------------------------------`);
180-
console.log(`\n----------------unbond_public transaction:---------------\n${bondTransactions[1]}`);
181-
console.log(`---------------------------------------------------------`);
182-
console.log(`\n-----------------claim_unbond_public transaction:---------------\n${bondTransactions[2]}`);
183-
console.log(`---------------------------------------------------------`);
184-
//---------------------------------------------------------
174+
175+
async function main() {
176+
try {
177+
// -------------------ONLINE COMPONENT---------------------
178+
// (Do this part on an internet connected machine)
179+
180+
// Download the needed keys for the functions we want to execute offline
181+
const transferKeyPaths = await preDownloadTransferKeys();
182+
const bondingKeyPaths = await preDownloadBondingKeys();
183+
//---------------------------------------------------------
184+
185+
// ------------------OFFLINE COMPONENT---------------------
186+
// (Do this part on an offline machine)
187+
// Get the latest state root from an online machine and enter it into an offline machine
188+
const latestStateRoot = "sr1p93gpsezrjzdhcd2wujznx5s07k8qa39t6vfcej35zew8vn2jyrs46te8q";
189+
190+
// Build a transfer_public transaction
191+
const transferTx = await buildTransferPublicTxOffline(stakerAddress, 10000, latestStateRoot, transferKeyPaths);
192+
console.log("Transfer transaction built offline!");
193+
console.log(`\n---------------transfer_public transaction---------------\n${transferTx}`);
194+
console.log(`---------------------------------------------------------`);
195+
196+
// Build bonding & unbonding transactions
197+
const bondTransactions = await buildBondingTxOffline(validatorAddress, withdrawalAddress, 100, latestStateRoot, bondingKeyPaths);
198+
console.log("Bonding transactions built offline!");
199+
console.log(`\n-----------------bond_public transaction-----------------\n${bondTransactions[0]}`);
200+
console.log(`---------------------------------------------------------`);
201+
console.log(`\n----------------unbond_public transaction:---------------\n${bondTransactions[1]}`);
202+
console.log(`---------------------------------------------------------`);
203+
console.log(`\n-----------------claim_unbond_public transaction:---------------\n${bondTransactions[2]}`);
204+
console.log(`---------------------------------------------------------`);
205+
//---------------------------------------------------------
206+
} catch (e) {
207+
console.log(e);
208+
process.exit(1);
209+
}
210+
}
211+
212+
await main();
185213

186214
// -------------------ONLINE COMPONENT---------------------
187215
// (Do this part on an internet connected machine)

0 commit comments

Comments
 (0)