Testnet Mode — switch back to Production in the menuheader when you're done.
VarnirCorrection extends Standard
/**
* Voting round: the social-recovery guard (thrown, as this vote is not a
* Promise executor), then voteMod.
*
* @returns {Promise<boolean>}
*/ public vote(): Promise<boolean> {
// Social recovery (spec §4.5): vote() here is not a Promise executor, so
// the refusal is thrown rather than rejected - the correction stream is
// the only "$o" here, so every stream this touches may be debited.
const recoveryRefusal = this.refuseRecoveredStreams([]);
if (recoveryRefusal) throw recoveryRefusal;
return this.voteMod();
}/**
* Commit round: commitMod.
*
* @returns {Promise<any>}
*/ public commit(): Promise<any> {
return this.commitMod();
} private refuseRecoveredIdentity(state: any, outgoing: boolean): string | null {
if (state && state.recoveredTo) {
// A dead identity may still RECEIVE: the credit lands and is swept to recoveredTo.
return outgoing
? `Identity recovered - new identity is ${state.recoveredTo}; your keys are still valid`
: null;
}
if (outgoing && state && state.recovery && state.recovery.frozen) {
return "Identity frozen - a social recovery is in its 48h waiting period";
}
return null;
} private refuseRecoveredStreams(incomingOnly: string[] | "all-outputs"): string | null {
// Never getActivityStreams(ref): an unloaded ref CREATES a stream. The
// no-argument form returns only the streams this transaction loaded.
const ctx: any = this as any;
const loaded: any = ctx.getActivityStreams() || {};
const outputs: any = (ctx.transactions && ctx.transactions.$o) || {};
const toId = (label: string, ref: any): string | null => {
const raw = ref && typeof ref === "object" ? ref.$stream : typeof ref === "string" ? ref : /^[0-9a-f]{64}$/i.test(label) ? label : null;
return typeof raw === "string" ? raw.split(":")[0].slice(-64) : null;
};
const outputIds: string[] = [];
const incomingIds: string[] = [];
for (const label of Object.keys(outputs)) {
const id = toId(label, outputs[label]);
if (!id) continue;
outputIds.push(id);
if (incomingOnly === "all-outputs" || incomingOnly.indexOf(label) !== -1) incomingIds.push(id);
}
for (const id of Object.keys(loaded)) {
const activity = loaded[id];
if (!activity || typeof activity.getState !== "function") continue;
const signed = typeof activity.hasSignature === "function" && activity.hasSignature();
const incoming = !signed && outputIds.indexOf(id) !== -1 && incomingIds.indexOf(id) !== -1;
const refusal = this.refuseRecoveredIdentity(activity.getState(), !incoming);
if (refusal) return refusal;
}
return null;
}/**
* Refuses a missing `$o.correction` ("Invalid Correction #1"), fewer than
* 4 `$i` inputs ("Unauthorised Correction #1") and any `$i` identity
* whose volatile lacks `minigate` ("Unauthorised Correction #2").
*/ private async voteMod(): Promise<boolean> {
// Make sure there is a correction
if (!this.transactions.$o.correction) {
throw "Invalid Correction #1";
}
// if (
// (!this.transactions.$o.correction.network &&
// !this.transactions.$o.correction.token &&
// !this.transactions.$o.correction.amount) || !this.transactions.$o.revert
// ) {
// throw "Invalid Correction #2";
// }
//Uses BN now
// const hexTest = /0x[0-9a-f]+$/;
// if (!hexTest.test(this.transactions.$o.correction.amount)) {
// throw "Invalid Correction #3 - Is it HEX?";
// }
// We can now use mingates, So need to loop the $i and fetch volatile and make sure they all have minigate=1
const minigates = Object.keys(this.transactions.$i);
// Do we have enough minigates (How to solve harcoding? Could use dynamic data)
const MINIMUM = 4;
if (minigates.length < MINIMUM) {
throw "Unauthorised Correction #1";
}
for (let i = minigates.length; i--; ) {
const minigate = this.getActivityStreams(minigates[i]);
const volatile = await minigate.getVolatile();
if (!volatile.minigate) {
// Not an approved minigate so fail
throw "Unauthorised Correction #2";
}
}
// Got this far enough minigates!
return true;
}/** * Applies the correction. With `$o.revert`: when the type is not * `revertOnly` and the record's `confirmed.success` is not true, the * correction is replaced by a `sub` of the record's own network, token and * amount; either way the record's `confirmed` is set to false. Then * `clearPending` empties `walletDepositPending`, and when a network and * token are given the balance there is added to, subtracted from, set, or * (revertOnly) left alone. */
private async commitMod(): Promise<boolean> {
// Revert confirmation
if(this.transactions.$o.revert) {
const revert = this.getActivityStreams(this.transactions.$o.revert);
const revertData = revert.getState();
if(this.transactions.$o.correction.type !== 'revertOnly' && !revertData.confirmed?.success) {
// The record is not a success: the correction becomes a `sub` of
// the record's own network/token/amount.
this.transactions.$o.correction.network = revertData.network;
this.transactions.$o.correction.token = revertData.token;
this.transactions.$o.correction.amount = revertData.amount;
this.transactions.$o.correction.type = "sub";
}
// Do we need to deduct, No because next confirmation will do that
revertData.confirmed = false;
revert.setState(revertData);
}
const correctionIden = this.getActivityStreams(
this.transactions.$o.correction
);
const correctionData = correctionIden.getState();
if(this.transactions.$o.correction.clearPending) {
correctionData.walletDepositPending = {};
}
if(this.transactions.$o.correction.network && this.transactions.$o.correction.token) {
// No complicated layer manipulation needed here like cryptotransfer.
// Note: the "partitions[id]" wrapper this used to key by was always
// this same document's own stream id - simplified to a plain
// "balance" field, applied consistently across this contract family.
const current = this.getBN(correctionData.layer2[this.transactions.$o.correction.network][
this.transactions.$o.correction.token
].balance);
this.ActiveLogger.info(`Correction Current :${current} will ${this.transactions.$o.correction.type} or SET ${this.transactions.$o.correction.amount}`);
switch (this.transactions.$o.correction.type) {
case "revertOnly":
break;
case "add":
correctionData.layer2[this.transactions.$o.correction.network][
this.transactions.$o.correction.token
].balance = current.add(this.getBN(this.transactions.$o.correction.amount));
break;
case "sub":
correctionData.layer2[this.transactions.$o.correction.network][
this.transactions.$o.correction.token
].balance = current.sub(this.getBN(this.transactions.$o.correction.amount));
break;
default:
correctionData.layer2[this.transactions.$o.correction.network][
this.transactions.$o.correction.token
].balance = this.getBN(this.transactions.$o.correction.amount);
break;
}
this.ActiveLogger.info(`Correction Updated :${current}`);
// Update State
correctionIden.setState(correctionData);
}
return true;
}/**
* Convert to a big number (native BigInt-backed, see the BN class above)
*
* @private
* @param {*} [data]
* @returns {BN}
* @memberof VarnirCorrection
*/ private getBN(data?: any): BN {
if (data) {
if (data.hex) {
return BN.from(data.hex);
} else {
return BN.from(data);
}
} else {
return BN.from("0x00");
}
}