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" ;
2
14
import { getLocalKey , preDownloadBondingKeys , preDownloadTransferKeys } from "./helpers" ;
3
15
4
16
await initThreadPool ( ) ;
@@ -19,6 +31,9 @@ async function buildTransferPublicTxOffline(recipientAddress: Address, amount: n
19
31
const transferPublicProvingKey = ProvingKey . fromBytes (
20
32
await getLocalKey ( < string > keyPaths [ CREDITS_PROGRAM_KEYS . transfer_public . locator ] )
21
33
) ;
34
+ const inclusionKey = ProvingKey . fromBytes (
35
+ await getLocalKey ( < string > keyPaths [ CREDITS_PROGRAM_KEYS . inclusion . locator ] )
36
+ ) ;
22
37
23
38
// Create an offline key provider
24
39
console . log ( "Creating offline key provider" ) ;
@@ -28,25 +43,25 @@ async function buildTransferPublicTxOffline(recipientAddress: Address, amount: n
28
43
// keys into the key manager.
29
44
console . log ( "Inserting proving keys into key provider" ) ;
30
45
offlineKeyProvider . insertFeePublicKeys ( feePublicProvingKey ) ;
46
+ offlineKeyProvider . cacheKeys ( CREDITS_PROGRAM_KEYS . inclusion . locator , [ inclusionKey , VerifyingKey . inclusionVerifier ( ) ] ) ;
31
47
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
+ }
39
54
40
55
// Create an offline query to complete the inclusion proof
41
56
let offlineQuery : OfflineQuery ;
42
57
const blockHeight = 0 ;
43
58
// 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
+ }
50
65
51
66
// Insert the key provider into the program manager
52
67
programManager . setKeyProvider ( offlineKeyProvider ) ;
@@ -83,10 +98,12 @@ async function buildBondingTxOffline(
83
98
const bondPublicKeyBytes = await getLocalKey ( < string > keyPaths [ CREDITS_PROGRAM_KEYS . bond_public . locator ] ) ;
84
99
const unbondPublicKeyBytes = await getLocalKey ( < string > keyPaths [ CREDITS_PROGRAM_KEYS . unbond_public . locator ] ) ;
85
100
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 ] ) ;
86
102
const feePublicProvingKey = ProvingKey . fromBytes ( feePublicKeyBytes ) ;
87
103
const bondPublicProvingKey = ProvingKey . fromBytes ( bondPublicKeyBytes ) ;
88
104
const unBondPublicProvingKey = ProvingKey . fromBytes ( unbondPublicKeyBytes ) ;
89
105
const claimUnbondPublicProvingKey = ProvingKey . fromBytes ( claimUnbondPublicKeyBytes ) ;
106
+ const inclusionProvingKey = ProvingKey . fromBytes ( inclusionKeys ) ;
90
107
91
108
// Create an offline key provider to fetch keys without connection to the internet
92
109
console . log ( "Creating offline key provider" ) ;
@@ -98,6 +115,7 @@ async function buildBondingTxOffline(
98
115
offlineKeyProvider . insertBondPublicKeys ( bondPublicProvingKey ) ;
99
116
offlineKeyProvider . insertUnbondPublicKeys ( unBondPublicProvingKey ) ;
100
117
offlineKeyProvider . insertClaimUnbondPublicKeys ( claimUnbondPublicProvingKey ) ;
118
+ offlineKeyProvider . cacheKeys ( CREDITS_PROGRAM_KEYS . inclusion . locator , [ inclusionProvingKey , VerifyingKey . inclusionVerifier ( ) ] ) ;
101
119
102
120
// Insert the key provider into the program manager
103
121
programManager . setKeyProvider ( offlineKeyProvider ) ;
@@ -114,7 +132,7 @@ async function buildBondingTxOffline(
114
132
offlineQuery : new OfflineQuery ( 0 , latestStateRoot )
115
133
} ;
116
134
117
-
135
+ console . log ( "Build bond public transaction" ) ;
118
136
const bondTx = < Transaction > await programManager . buildBondPublicTransaction (
119
137
validatorAddress . to_string ( ) ,
120
138
withdrawalAddress . to_string ( ) ,
@@ -150,38 +168,48 @@ async function buildBondingTxOffline(
150
168
return [ bondTx , unBondTx , claimUnbondTx ] ;
151
169
}
152
170
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
167
171
const stakerAddress = new Account ( ) . address ( ) ;
168
172
const validatorAddress = new Account ( ) . address ( ) ;
169
173
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 ( ) ;
185
213
186
214
// -------------------ONLINE COMPONENT---------------------
187
215
// (Do this part on an internet connected machine)
0 commit comments