diff --git a/package-lock.json b/package-lock.json index e7df764..b45ffdd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "xrpio", - "version": "0.2.3", + "version": "0.3.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "xrpio", - "version": "0.2.3", + "version": "0.3.0", "license": "MIT", "dependencies": { "axios": "^1.7.7", diff --git a/src/xrpIO/xrpl-binding.ts b/src/xrpIO/xrpl-binding.ts index 8435147..a724142 100644 --- a/src/xrpIO/xrpl-binding.ts +++ b/src/xrpIO/xrpl-binding.ts @@ -75,6 +75,7 @@ export class xrpIO { this.dbg("Sending payment", wallet.address, '->', to) const _api = await this.cloneApi() + const payment: Payment = await _api.autofill({ TransactionType: 'Payment', Account: wallet.address, @@ -84,7 +85,7 @@ export class xrpIO { Memos: [{ Memo: { MemoData: hexEncode(data.data || ""), - MemoFormat: hexEncode(data.format || ""), + MemoFormat: hexEncode(data.format || "0"), MemoType: hexEncode(data.type || "") } }] @@ -188,8 +189,6 @@ export class xrpIO { } const tx = await this.getTransaction(hash) - - if(verifyOwner && tx.result.tx_json.Account != verifyOwner){ throw new CannotVerifyOwnerError(hash, tx.result.tx_json.Account, verifyOwner) } @@ -204,7 +203,7 @@ export class xrpIO { return memoParsed } - public async treeWrite(data: string, to: string, secret: string, format: 'L' | 'N' = 'L'): Promise { + public async treeWrite(data: string, to: string, secret: string, format: string = "0"): Promise { const wallet = Wallet.fromSecret(secret) data = await compressB64(data) const chunks = chunkString(data, PAYLOAD_SIZE) @@ -215,19 +214,36 @@ export class xrpIO { return hashes[0] } - return await this.treeWrite(JSON.stringify(hashes), to, secret, 'N') + return await this.treeWrite(JSON.stringify(hashes), to, secret, `${hashes.length}`) } - public async treeRead(hashes: string[], verifyOwner?:string): Promise { + private maxMemos = 0 + + public async treeRead(hashes: string[], verifyOwner?:string, progressCallback:(max:number,finished:number)=>void = ()=>{}): Promise { const bad_hash = hashes.find(hash => !NON_ZERO_TX_HASH.test(hash)) if (bad_hash) throw new BadTxHashError(bad_hash) - const memos = await Promise.all(hashes.map(hash => this.readRaw(hash, verifyOwner))) + let count = 0; + + const memos = await Promise.all(hashes.map(async hash => { + const memo = await this.readRaw(hash, verifyOwner) + if(String(memo.format) === '0'){ + count += 1; + progressCallback(count, this.maxMemos) + if(count === this.maxMemos){ + this.maxMemos = 0; + } + }else{ + this.maxMemos = Math.max(this.maxMemos, Number(memo.format)) + progressCallback(-1, this.maxMemos) + } + return memo + })) const payload: string = await decompressB64(memos.map(memo => memo.data).join('')) - if (memos.some(memo => memo.format === 'N')) { - return await this.treeRead(JSON.parse(payload), verifyOwner) + if (memos.some(memo => memo.format !== '0')) { + return await this.treeRead(JSON.parse(payload), verifyOwner, progressCallback) } return payload