ecash-agora
Version:
Library for interacting with the eCash Agora protocol
116 lines • 3.74 kB
JavaScript
;
// Copyright (c) 2024 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseAgoraTx = void 0;
const ecash_lib_1 = require("ecash-lib");
const consts_js_1 = require("./consts.js");
const oneshot_js_1 = require("./oneshot.js");
function parseAgoraTx(tx) {
if (tx.inputs.length === 0) {
return undefined;
}
if (tx.outputs.length < 2) {
return undefined;
}
if (tx.tokenEntries.length === 0) {
return undefined;
}
const adInput = tx.inputs[0];
const offerOutput = tx.outputs[1];
if (offerOutput.token === undefined) {
return undefined;
}
const tokenEntry = tx.tokenEntries[0];
let opreturnScript;
switch (tokenEntry.tokenType.protocol) {
case 'SLP':
opreturnScript = (0, ecash_lib_1.slpSend)(tokenEntry.tokenId, tokenEntry.tokenType.number, [0n, offerOutput.token.atoms]);
break;
// ALP not implemented yet
case 'ALP':
case 'UNKNOWN':
return undefined;
}
const scriptSig = new ecash_lib_1.Script((0, ecash_lib_1.fromHex)(adInput.inputScript));
const parsedAd = parseAdScriptSig(scriptSig);
if (parsedAd === undefined) {
return undefined;
}
let variant;
let expectedAgoraScript;
let expectedAgoraP2sh;
switch (parsedAd.covenantVariant) {
case oneshot_js_1.AgoraOneshot.COVENANT_VARIANT: {
let agoraOneshot;
try {
agoraOneshot = oneshot_js_1.AgoraOneshot.fromRedeemScript(parsedAd.redeemScript, opreturnScript);
}
catch {
return undefined;
}
variant = {
type: 'ONESHOT',
params: agoraOneshot,
};
expectedAgoraScript = agoraOneshot.script();
expectedAgoraP2sh = ecash_lib_1.Script.p2sh((0, ecash_lib_1.shaRmd160)(expectedAgoraScript.bytecode));
break;
}
default:
return undefined;
}
if (offerOutput.outputScript !== (0, ecash_lib_1.toHex)(expectedAgoraP2sh.bytecode)) {
return undefined;
}
const outpoint = {
txid: tx.txid,
outIdx: 1,
};
return {
...variant,
outpoint,
txBuilderInput: {
prevOut: outpoint,
signData: {
sats: offerOutput.sats,
redeemScript: expectedAgoraScript,
},
},
spentBy: offerOutput.spentBy,
};
}
exports.parseAgoraTx = parseAgoraTx;
/**
* How many pushops we expect at least for an advertisement.
* There has to be at least a LOKAD ID, a covenant variant and a redeemScript.
**/
const MIN_NUM_SCRIPTSIG_PUSHOPS = 3;
function parseAdScriptSig(scriptSig) {
const ops = scriptSig.ops();
const pushdata = [];
let op;
while ((op = ops.next())) {
if (!(0, ecash_lib_1.isPushOp)(op)) {
return undefined;
}
pushdata.push(op.data);
}
if (pushdata.length < MIN_NUM_SCRIPTSIG_PUSHOPS) {
return undefined;
}
const lokadId = pushdata[0];
if ((0, ecash_lib_1.bytesToStr)(lokadId) != consts_js_1.AGORA_LOKAD_ID_STR) {
return undefined;
}
const covenantVariant = (0, ecash_lib_1.bytesToStr)(pushdata[1]);
const parsedPushdata = pushdata.slice(2, -1);
const redeemScript = new ecash_lib_1.Script(pushdata[pushdata.length - 1]);
return {
covenantVariant,
pushdata: parsedPushdata,
redeemScript,
};
}
//# sourceMappingURL=ad.js.map