Add functionality to verify sender on reads

This commit is contained in:
nitowa
2023-05-03 22:07:18 +02:00
parent 50fa056fe3
commit 04dab07fb5
4 changed files with 45 additions and 20 deletions
+2 -1
View File
@@ -1 +1,2 @@
export const ERR_BAD_TX_HASH = (hash:string) => new Error(`Bad tx hash format: "${hash}"`)
export const ERR_BAD_TX_HASH = (hash:string) => new Error(`Bad tx hash format: "${hash}"`)
export const ERR_NO_VERIFY_OWNER = (hash: string, actualAccount: string, desiredAccount: string) => new Error(`Expected tx "${hash}" to be initiated by ${desiredAccount} but was ${actualAccount}`)
+10 -7
View File
@@ -4,7 +4,7 @@ import { Client, Payment, TxResponse, Wallet } from 'xrpl'
import * as zlib from 'zlib'
import * as util from 'util'
import { NON_ZERO_TX_HASH } from '../util/protocol.constants'
import { ERR_BAD_TX_HASH } from '../util/errors'
import { ERR_BAD_TX_HASH, ERR_NO_VERIFY_OWNER } from '../util/errors'
const compressB64 = async (data: string) => (await util.promisify(zlib.deflate)(Buffer.from(data, 'utf-8'))).toString('base64')
const decompressB64 = async (data: string) => (await util.promisify(zlib.inflate)(Buffer.from(data, 'base64'))).toString('utf-8')
@@ -125,13 +125,16 @@ export class xrpIO {
}
}
public async readRaw(hash: string): Promise<Memo> {
public async readRaw(hash: string, verifyOwner?: string): Promise<Memo> {
if (!NON_ZERO_TX_HASH.test(hash)) {
throw ERR_BAD_TX_HASH(hash)
}
const tx = await this.getTransaction(hash)
if(verifyOwner && tx.result.Account != verifyOwner){
throw ERR_NO_VERIFY_OWNER(hash, tx.result.Account, verifyOwner)
}
const memo = tx.result.Memos[0].Memo
const memoParsed = {
data: hexDecode(memo.MemoData),
@@ -156,16 +159,16 @@ export class xrpIO {
return await this.treeWrite(JSON.stringify(hashes), to, secret, 'N')
}
public async treeRead(hashes: string[]): Promise<string> {
public async treeRead(hashes: string[], verifyOwner?:string): Promise<string> {
const bad_hash = hashes.find(hash => !NON_ZERO_TX_HASH.test(hash))
if (bad_hash)
throw ERR_BAD_TX_HASH(bad_hash)
const memos = await Promise.all(hashes.map(hash => this.readRaw(hash)))
const memos = await Promise.all(hashes.map(hash => this.readRaw(hash, verifyOwner)))
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))
return await this.treeRead(JSON.parse(payload), verifyOwner)
}
return payload