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", "name": "xrpio",
"version": "0.2.3", "version": "0.3.0",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "xrpio", "name": "xrpio",
"version": "0.2.3", "version": "0.3.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"axios": "^1.7.7", "axios": "^1.7.7",
+25 -9
View File
@@ -75,6 +75,7 @@ export class xrpIO {
this.dbg("Sending payment", wallet.address, '->', to) this.dbg("Sending payment", wallet.address, '->', to)
const _api = await this.cloneApi() const _api = await this.cloneApi()
const payment: Payment = await _api.autofill({ const payment: Payment = await _api.autofill({
TransactionType: 'Payment', TransactionType: 'Payment',
Account: wallet.address, Account: wallet.address,
@@ -84,7 +85,7 @@ export class xrpIO {
Memos: [{ Memos: [{
Memo: { Memo: {
MemoData: hexEncode(data.data || ""), MemoData: hexEncode(data.data || ""),
MemoFormat: hexEncode(data.format || ""), MemoFormat: hexEncode(data.format || "0"),
MemoType: hexEncode(data.type || "") MemoType: hexEncode(data.type || "")
} }
}] }]
@@ -188,8 +189,6 @@ export class xrpIO {
} }
const tx = await this.getTransaction(hash) const tx = await this.getTransaction(hash)
if(verifyOwner && tx.result.tx_json.Account != verifyOwner){ if(verifyOwner && tx.result.tx_json.Account != verifyOwner){
throw new CannotVerifyOwnerError(hash, 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 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) const wallet = Wallet.fromSecret(secret)
data = await compressB64(data) data = await compressB64(data)
const chunks = chunkString(data, PAYLOAD_SIZE) const chunks = chunkString(data, PAYLOAD_SIZE)
@@ -215,19 +214,36 @@ export class xrpIO {
return hashes[0] 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)) const bad_hash = hashes.find(hash => !NON_ZERO_TX_HASH.test(hash))
if (bad_hash) if (bad_hash)
throw new BadTxHashError(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('')) const payload: string = await decompressB64(memos.map(memo => memo.data).join(''))
if (memos.some(memo => memo.format === 'N')) { if (memos.some(memo => memo.format !== '0')) {
return await this.treeRead(JSON.parse(payload), verifyOwner) return await this.treeRead(JSON.parse(payload), verifyOwner, progressCallback)
} }
return payload return payload