UNPKG

meta-contract

Version:

Meta Contract SDK

105 lines (104 loc) 4.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NftGenesisFactory = exports.NftGenesis = void 0; const ContractAdapter_1 = require("../../common/ContractAdapter"); const protoheader_1 = require("../../common/protoheader"); const scryptlib_1 = require("../../scryptlib"); const nftProto = require("../contract-proto/nft.proto"); const genesisTokenIDTxid = '0000000000000000000000000000000000000000000000000000000000000000'; class NftGenesis extends ContractAdapter_1.ContractAdapter { constructor() { const desc = require('../contract-desc/nftGenesis_desc.json'); let GenesisContractClass = (0, scryptlib_1.buildContractClass)(desc); let contract = new GenesisContractClass(); super(contract); } clone() { let contract = new NftGenesis(); contract.setFormatedDataPart(this.getFormatedDataPart()); return contract; } setFormatedDataPart(dataPart) { this._formatedDataPart = Object.assign({}, this._formatedDataPart, dataPart); this._formatedDataPart.genesisHash = ''; this._formatedDataPart.protoVersion = nftProto.PROTO_VERSION; this._formatedDataPart.protoType = protoheader_1.PROTO_TYPE.NFT; const dataPartHex = nftProto.newDataPart(this._formatedDataPart).toString('hex'); super.setDataPart(dataPartHex); } getFormatedDataPart() { return this._formatedDataPart; } setFormatedDataPartFromLockingScript(script) { let dataPart = nftProto.parseDataPart(script.toBuffer()); this.setFormatedDataPart(dataPart); } isFirstGenesis() { return this.getFormatedDataPart().sensibleID.txid == genesisTokenIDTxid; } unlock({ txPreimage, // sig pubKey, sig, // GenesisTx Input Proof genesisTxHeader, prevInputIndex, genesisTxInputProof, // Prev GenesisTx Output Proof prevGenesisTxHeader, prevTxOutputHashProof, prevTxOutputSatoshiBytes, // output nftScript, genesisSatoshis, nftSatoshis, changeAddress, changeSatoshis, opReturnScript, }) { return this._contract.unlock(txPreimage, pubKey, sig, genesisTxHeader, prevInputIndex, genesisTxInputProof, prevGenesisTxHeader, prevTxOutputHashProof, prevTxOutputSatoshiBytes, nftScript, genesisSatoshis, nftSatoshis, changeAddress, changeSatoshis, opReturnScript); } } exports.NftGenesis = NftGenesis; class NftGenesisFactory { static getLockingScriptSize() { return this.lockingScriptSize; } static createContract() { return new NftGenesis(); } static getDummyInstance() { let contract = this.createContract(); contract.setFormatedDataPart({}); return contract; } static calLockingScriptSize() { let contract = this.getDummyInstance(); let size = contract.lockingScript.toBuffer().length; return size; } static calUnlockingScriptSize(opreturnData) { return 10000; // let opreturnScriptHex = '' // if (opreturnData) { // let script = mvc.Script.buildSafeDataOut(opreturnData) // opreturnScriptHex = script.toHex() // } // let contract = this.getDummyInstance() // let tokenContract = NftFactory.getDummyInstance() // const preimage = getPreimage(dummyTx, contract.lockingScript.toASM(), 1) // const sig = Buffer.from(PLACE_HOLDER_SIG, 'hex') // const rabinMsg = dummyPayload // const rabinPaddingArray = [] // const rabinSigArray: Int[] = [] // const rabinPubKeyIndexArray = [] // const rabinPubKeyArray: Int[] = [] // let unlockResult = contract.unlock({ // txPreimage: new SigHashPreimage(toHex(preimage)), // sig: new Sig(toHex(sig)), // rabinMsg: new Bytes(toHex(rabinMsg)), // rabinPaddingArray, // rabinSigArray, // rabinPubKeyIndexArray, // rabinPubKeyVerifyArray: rabinPubKeyArray, // rabinPubKeyHashArray: new Bytes(toHex(dummyRabinPubKeyHashArray)), // genesisSatoshis: 1000, // nftScript: new Bytes(tokenContract.lockingScript.toHex()), // nftSatoshis: 1000, // changeAddress: new Ripemd160(toHex(dummyAddress.hashBuffer)), // changeSatoshis: 1000, // opReturnScript: new Bytes(opreturnScriptHex), // }) // return (unlockResult.toScript() as mvc.Script).toBuffer().length } } exports.NftGenesisFactory = NftGenesisFactory;