progress callback for treeWrite

This commit is contained in:
nitowa
2024-10-13 17:47:48 +02:00
parent 2f186eccec
commit 5277fbd703
2 changed files with 27 additions and 11 deletions
+2 -2
View File
@@ -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",
+25 -9
View File
@@ -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<string> {
public async treeWrite(data: string, to: string, secret: string, format: string = "0"): Promise<string> {
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<string> {
private maxMemos = 0
public async treeRead(hashes: string[], verifyOwner?:string, progressCallback:(max:number,finished:number)=>void = ()=>{}): Promise<string> {
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