Browse Source

progress callback for treeWrite

master
nitowa 2 months ago
parent
commit
5277fbd703
2 changed files with 27 additions and 11 deletions
  1. 2
    2
      package-lock.json
  2. 25
    9
      src/xrpIO/xrpl-binding.ts

+ 2
- 2
package-lock.json View File

@@ -1,12 +1,12 @@
1 1
 {
2 2
   "name": "xrpio",
3
-  "version": "0.2.3",
3
+  "version": "0.3.0",
4 4
   "lockfileVersion": 2,
5 5
   "requires": true,
6 6
   "packages": {
7 7
     "": {
8 8
       "name": "xrpio",
9
-      "version": "0.2.3",
9
+      "version": "0.3.0",
10 10
       "license": "MIT",
11 11
       "dependencies": {
12 12
         "axios": "^1.7.7",

+ 25
- 9
src/xrpIO/xrpl-binding.ts View File

@@ -75,6 +75,7 @@ export class xrpIO {
75 75
     this.dbg("Sending payment", wallet.address, '->', to)
76 76
 
77 77
     const _api = await this.cloneApi()
78
+    
78 79
     const payment: Payment = await _api.autofill({
79 80
       TransactionType: 'Payment',
80 81
       Account: wallet.address,
@@ -84,7 +85,7 @@ export class xrpIO {
84 85
       Memos: [{
85 86
         Memo: {
86 87
           MemoData: hexEncode(data.data || ""),
87
-          MemoFormat: hexEncode(data.format || ""),
88
+          MemoFormat: hexEncode(data.format || "0"),
88 89
           MemoType: hexEncode(data.type || "")
89 90
         }
90 91
       }]
@@ -188,8 +189,6 @@ export class xrpIO {
188 189
     }
189 190
     const tx = await this.getTransaction(hash)
190 191
 
191
-    
192
-
193 192
     if(verifyOwner && tx.result.tx_json.Account != verifyOwner){
194 193
       throw new CannotVerifyOwnerError(hash, tx.result.tx_json.Account, verifyOwner)
195 194
     }
@@ -204,7 +203,7 @@ export class xrpIO {
204 203
     return memoParsed
205 204
   }
206 205
 
207
-  public async treeWrite(data: string, to: string, secret: string, format: 'L' | 'N' = 'L'): Promise<string> {
206
+  public async treeWrite(data: string, to: string, secret: string, format: string = "0"): Promise<string> {
208 207
     const wallet = Wallet.fromSecret(secret)
209 208
     data = await compressB64(data)
210 209
     const chunks = chunkString(data, PAYLOAD_SIZE)
@@ -215,19 +214,36 @@ export class xrpIO {
215 214
       return hashes[0]
216 215
     }
217 216
 
218
-    return await this.treeWrite(JSON.stringify(hashes), to, secret, 'N')
217
+    return await this.treeWrite(JSON.stringify(hashes), to, secret, `${hashes.length}`)
219 218
   }
220 219
 
221
-  public async treeRead(hashes: string[], verifyOwner?:string): Promise<string> {
220
+  private maxMemos = 0
221
+
222
+  public async treeRead(hashes: string[], verifyOwner?:string, progressCallback:(max:number,finished:number)=>void = ()=>{}): Promise<string> {
222 223
     const bad_hash = hashes.find(hash => !NON_ZERO_TX_HASH.test(hash))
223 224
     if (bad_hash)
224 225
       throw new BadTxHashError(bad_hash)
225 226
 
226
-    const memos = await Promise.all(hashes.map(hash => this.readRaw(hash, verifyOwner)))
227
+    let count = 0;
228
+
229
+    const memos = await Promise.all(hashes.map(async hash => {
230
+      const memo = await this.readRaw(hash, verifyOwner)
231
+      if(String(memo.format) === '0'){
232
+        count += 1;
233
+        progressCallback(count, this.maxMemos)
234
+        if(count === this.maxMemos){
235
+          this.maxMemos = 0;
236
+        }
237
+      }else{
238
+        this.maxMemos = Math.max(this.maxMemos, Number(memo.format))
239
+        progressCallback(-1, this.maxMemos)
240
+      }
241
+      return memo
242
+    }))
227 243
     const payload: string = await decompressB64(memos.map(memo => memo.data).join(''))
228 244
 
229
-    if (memos.some(memo => memo.format === 'N')) {
230
-      return await this.treeRead(JSON.parse(payload), verifyOwner)
245
+    if (memos.some(memo => memo.format !== '0')) {
246
+      return await this.treeRead(JSON.parse(payload), verifyOwner, progressCallback)
231 247
     }
232 248
 
233 249
     return payload

Loading…
Cancel
Save