@bsv/wallet-toolbox-client
Version:
Client only Wallet Storage
264 lines • 8.04 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityOutput = void 0;
const utilityHelpers_1 = require("../../../utility/utilityHelpers");
const EntityBase_1 = require("./EntityBase");
class EntityOutput extends EntityBase_1.EntityBase {
constructor(api) {
const now = new Date();
super(api || {
outputId: 0,
created_at: now,
updated_at: now,
userId: 0,
transactionId: 0,
spendable: false,
change: false,
satoshis: 0,
outputDescription: '',
vout: 0,
type: '',
providedBy: 'you',
purpose: '',
txid: undefined,
basketId: undefined,
spentBy: undefined,
derivationPrefix: undefined,
derivationSuffix: undefined,
senderIdentityKey: undefined,
customInstructions: undefined,
spendingDescription: undefined,
scriptLength: undefined,
scriptOffset: undefined,
lockingScript: undefined
});
}
updateApi() {
/* nothing needed yet... */
}
get outputId() {
return this.api.outputId;
}
set outputId(v) {
this.api.outputId = v;
}
get created_at() {
return this.api.created_at;
}
set created_at(v) {
this.api.created_at = v;
}
get updated_at() {
return this.api.updated_at;
}
set updated_at(v) {
this.api.updated_at = v;
}
get userId() {
return this.api.userId;
}
set userId(v) {
this.api.userId = v;
}
get transactionId() {
return this.api.transactionId;
}
set transactionId(v) {
this.api.transactionId = v;
}
get basketId() {
return this.api.basketId;
}
set basketId(v) {
this.api.basketId = v;
}
get spentBy() {
return this.api.spentBy;
}
set spentBy(v) {
this.api.spentBy = v;
}
get vout() {
return this.api.vout;
}
set vout(v) {
this.api.vout = v;
}
get satoshis() {
return this.api.satoshis;
}
set satoshis(v) {
this.api.satoshis = v;
}
get outputDescription() {
return this.api.outputDescription;
}
set outputDescription(v) {
this.api.outputDescription = v;
}
get spendable() {
return this.api.spendable;
}
set spendable(v) {
this.api.spendable = v;
}
get change() {
return this.api.change;
}
set change(v) {
this.api.change = v;
}
get txid() {
return this.api.txid;
}
set txid(v) {
this.api.txid = v;
}
get type() {
return this.api.type;
}
set type(v) {
this.api.type = v;
}
get providedBy() {
return this.api.providedBy;
}
set providedBy(v) {
this.api.providedBy = v;
}
get purpose() {
return this.api.purpose;
}
set purpose(v) {
this.api.purpose = v;
}
get spendingDescription() {
return this.api.spendingDescription;
}
set spendingDescription(v) {
this.api.spendingDescription = v;
}
get derivationPrefix() {
return this.api.derivationPrefix;
}
set derivationPrefix(v) {
this.api.derivationPrefix = v;
}
get derivationSuffix() {
return this.api.derivationSuffix;
}
set derivationSuffix(v) {
this.api.derivationSuffix = v;
}
get senderIdentityKey() {
return this.api.senderIdentityKey;
}
set senderIdentityKey(v) {
this.api.senderIdentityKey = v;
}
get customInstructions() {
return this.api.customInstructions;
}
set customInstructions(v) {
this.api.customInstructions = v;
}
get lockingScript() {
return this.api.lockingScript;
}
set lockingScript(v) {
this.api.lockingScript = v;
}
get scriptLength() {
return this.api.scriptLength;
}
set scriptLength(v) {
this.api.scriptLength = v;
}
get scriptOffset() {
return this.api.scriptOffset;
}
set scriptOffset(v) {
this.api.scriptOffset = v;
}
get id() {
return this.api.outputId;
}
set id(v) {
this.api.outputId = v;
}
get entityName() {
return 'output';
}
get entityTable() {
return 'outputs';
}
equals(ei, syncMap) {
if (this.transactionId !== (syncMap ? syncMap.transaction.idMap[ei.transactionId] : ei.transactionId) ||
this.basketId !== (syncMap && ei.basketId ? syncMap.outputBasket.idMap[ei.basketId] : ei.basketId) ||
this.spentBy !== (syncMap && ei.spentBy ? syncMap.transaction.idMap[ei.spentBy] : ei.spentBy) ||
this.vout !== ei.vout ||
this.satoshis !== ei.satoshis ||
this.spendable !== ei.spendable ||
this.change !== ei.change ||
this.txid !== ei.txid ||
this.type !== ei.type ||
this.providedBy !== ei.providedBy ||
this.purpose !== ei.purpose ||
this.outputDescription !== ei.outputDescription ||
this.spendingDescription !== ei.spendingDescription ||
this.derivationPrefix !== ei.derivationPrefix ||
this.derivationSuffix !== ei.derivationSuffix ||
this.senderIdentityKey !== ei.senderIdentityKey ||
this.customInstructions !== ei.customInstructions ||
!(0, utilityHelpers_1.optionalArraysEqual)(this.lockingScript, ei.lockingScript) ||
this.scriptLength !== ei.scriptLength ||
this.scriptOffset !== ei.scriptOffset)
return false;
return true;
}
static async mergeFind(storage, userId, ei, syncMap, trx) {
const transactionId = syncMap.transaction.idMap[ei.transactionId];
const basketId = ei.basketId ? syncMap.outputBasket.idMap[ei.basketId] : null;
const ef = (0, utilityHelpers_1.verifyOneOrNone)(await storage.findOutputs({
partial: { userId, transactionId, vout: ei.vout },
trx
}));
return {
found: !!ef,
eo: new EntityOutput(ef || { ...ei }),
eiId: (0, utilityHelpers_1.verifyId)(ei.outputId)
};
}
async mergeNew(storage, userId, syncMap, trx) {
this.userId = userId;
this.basketId = this.basketId ? syncMap.outputBasket.idMap[this.basketId] : undefined;
this.transactionId = syncMap.transaction.idMap[this.transactionId];
this.spentBy = this.spentBy ? syncMap.transaction.idMap[this.spentBy] : undefined;
this.outputId = 0;
this.outputId = await storage.insertOutput(this.toApi(), trx);
}
async mergeExisting(storage, since, ei, syncMap, trx) {
let wasMerged = false;
if (ei.updated_at > this.updated_at) {
this.spentBy = ei.spentBy ? syncMap.transaction.idMap[ei.spentBy] : undefined;
this.spendable = ei.spendable;
this.change = ei.change;
this.type = ei.type;
this.providedBy = ei.providedBy;
this.purpose = ei.purpose;
this.outputDescription = ei.outputDescription;
this.spendingDescription = ei.spendingDescription;
this.senderIdentityKey = ei.senderIdentityKey;
this.customInstructions = ei.customInstructions;
this.scriptLength = ei.scriptLength;
this.scriptOffset = ei.scriptOffset;
this.lockingScript = ei.lockingScript;
this.updated_at = new Date(Math.max(ei.updated_at.getTime(), this.updated_at.getTime()));
await storage.updateOutput(this.id, this.toApi(), trx);
wasMerged = true;
}
return wasMerged;
}
}
exports.EntityOutput = EntityOutput;
//# sourceMappingURL=EntityOutput.js.map