Created
July 31, 2021 17:13
-
-
Save oskin1/7334dd90df59d04b3350a2809225bc3e to your computer and use it in GitHub Desktop.
Scan a UTXO with P2S address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Constant} from "ergo-lib-wasm-browser" | |
import JSONBigInt from "json-bigint" | |
const JSONBI = JSONBigInt({useNativeBigInt: true}) | |
type SigmaRust = typeof import("ergo-lib-wasm-browser") | |
class Module { | |
_ergo?: SigmaRust | |
async load(node: boolean = false): Promise<SigmaRust> { | |
if (this._ergo === undefined) { | |
this._ergo = await (node ? import("ergo-lib-wasm-nodejs") : import("ergo-lib-wasm-browser")) | |
} | |
return this._ergo! | |
} | |
// Need to expose through a getter to get Flow to detect the type correctly | |
get SigmaRust(): SigmaRust { | |
return this._ergo! | |
} | |
} | |
// need this otherwise Wallet's flow type isn't properly exported | |
const RustModule: Module = new Module() | |
const walletAddresses = ["9g1N1xqhrNG1b2TkmFcQGTFZ47EquUYUZAiWWCBEbZaBcsMhXJU"] | |
const p2sBox = JSONBI.parse(`{ | |
"boxId": "3c6569cd9d79016699ddf8cae47623474fa557851abc6fbe8375b4005a440930", | |
"transactionId": "7df66ea36e75fd4bedd00ebb58f0555e6d471613c5b9d68cac53df35412cfd02", | |
"index": 0, | |
"ergoTree": "198c031108cd02c3f56e66191a903758f53a4b90d07cef80f93e7a4f17d106098ad0caf189722a0400040204000e2030974274078845f263b4f21787e33cc99e9ec19a17ad85a5bc6da2cca91c5a2e0404040004c80f040604d00f06010104000e20f1fb942ebd039dc782fd9109acdb60aabea4dc7e75e9c813b6528c62692fc781040405decddefb02058ad7f5faacbf9c59058080a0f6f4acdbe01bd80ed6017300d602db6308b2a4730100d603b2a5730200d604b2db63087203730300d6057304d6068c720402d6077e720606d608b27202730500d6097e8c72080206d60a7e8cb2db6308a77306000206d60b7307d60c7e8cb272027308000206d60d7309d60e9a7207730aeb027201d1eded938cb27202730b0001730c93b1a4730dedededed93c27203d07201938c7204017205927206730e927ec1720306997ec1a7069d9c72077e730f067e73100695938c7208017205909c9c7209720a7e720b069c720e9a9c720c7e720d069c720a7e720b06909c9c720c720a7e720b069c720e9a9c72097e720d069c720a7e720b06", | |
"address": "EPNcS461xwdT5drQxJfoHRPqvQLsvuuJHUBa964oSHwx116u4i9xLjLvDp9VrpfdWGdhJV9bZDrEKzc484Gw94Ws78sR7iMr47fApZpGxTKktxPJRs4ZUXisV75tMT95Tyha1d2D7qpFW2cR1yRGwHKhrnKNBaAs1njAWedU9YrXon3SGWcC6PVRQRtoxrqbB8cvzXb2zyTwqBpXDYP6LqwmEE7BtVw1NzWh6HFPipiyRHxZNCKykw746yqmwd8DYhQNob7qPoB75CWmegCdQvdvHUY56dpwEaQsYb1dvgMrW1g3PzN7rUQffX1WktxN9Ui73MjpJEPPArDaBr992N5o9kFFwKcu6K71yPo5X39pNaFcNP2f93Mryh6DzZJWjVeXc4Vf52HWVBwqkDScfgHYYyERezGdsshzgUGAv8dS1jffBG9CPsFW1r7c1qYUjw54W83j4CRMt5vmGEQUTvxYXnSwYHe4pVG1SD4T6ttNPUa94i8Kg2FbH4YYQcZdZP4BYoQLvDj62ueDq4dqdEeky56Q3QzoeaabfQh", | |
"creationHeight": 506880, | |
"value": "12550000", | |
"assets": [ | |
{ | |
"tokenId": "ef802b475c06189fdbf844153cdc1d449a5ba87cce13d11bb47b5a539f27f12b", | |
"index": 0, | |
"amount": "200000000", | |
"name": "WT_ERG", | |
"decimals": 9, | |
"type": "EIP-004" | |
} | |
], | |
"additionalRegisters": {}, | |
"spentTransactionId": null, | |
"extension": {} | |
}`) | |
export function scanBox() { | |
const walletPKs = walletAddresses.map((a) => | |
RustModule.SigmaRust.Address | |
.from_base58(a) | |
.to_ergo_tree() | |
.to_base16_bytes() | |
.replace(/^0008cd/, "") | |
) | |
const ergoTreeParsed = RustModule.SigmaRust.ErgoTree.from_base16_bytes(p2sBox.ergoTree) | |
const constantsNum = ergoTreeParsed.constants_len() | |
const constants: Constant[] = [] | |
for (let i = 0; i < constantsNum; i++) { | |
constants.push(ergoTreeParsed.get_constant(i)!) | |
} | |
let matchedPk: string | undefined = undefined | |
const SigmaPropConstPat = new RegExp(/^08cd([0-9a-fA-F]+)$/) | |
for (const c of constants) { | |
const hex = c.encode_to_base16() | |
const matched = SigmaPropConstPat.exec(hex) | |
if (matched && walletPKs.includes(matched[1])) | |
matchedPk = matched[1] | |
} | |
const canBeSpentMaybe = !!matchedPk | |
console.log("matchedPk ", matchedPk) | |
console.log("canBeSpentMaybe ", canBeSpentMaybe) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment