noob-ethereum
Version:
A simple Ethereum library
41 lines (40 loc) • 1.45 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const trie_1 = require("@ethereumjs/trie");
const ethereumjs_util_1 = require("ethereumjs-util");
const transaction_1 = require("../../lib/rlp/transaction");
const conversion_1 = require("../../lib/utils/conversion");
class TransactionTrie {
constructor() {
this.trie = new trie_1.Trie();
}
/**
* Fetch the decoded data corresponding to a transactionIndex key from the trie
* @param {string | number} key - transaction index (in hex string form or integer)
* @returns {Promise<string[]>}
*/
async get(key) {
if (typeof key === 'number') {
key = (0, conversion_1.hexify)(key);
}
const raw = await this.trie.get(ethereumjs_util_1.rlp.encode(key));
const data = ethereumjs_util_1.rlp.decode(raw);
return data.map((elem) => (0, conversion_1.hexify)(elem));
}
/**
* Insert transaction into trie
* @param {RawTransaction} tx - transaction body (must be signed - aka including v,r,s)
* @returns {Promise<boolean>}
*/
async put(tx) {
try {
const serialised = (0, transaction_1.serialiseTransaction)(tx);
await this.trie.put(ethereumjs_util_1.rlp.encode(tx.transactionIndex), serialised);
return true;
}
catch (err) {
return false;
}
}
}
exports.default = TransactionTrie;
;