meta-contract
Version:
Meta Contract SDK
174 lines (173 loc) • 7.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NftFactory = exports.Nft = 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 nft_proto_1 = require("../contract-proto/nft.proto");
const contractUtil_1 = require("../contractUtil");
class Nft extends ContractAdapter_1.ContractAdapter {
constructor(constuctParams, version = 2) {
let desc = version === 2
? require('../contract-desc/nft-v2_desc.json')
: require('../contract-desc/nft_desc.json');
let ClassObj = (0, scryptlib_1.buildContractClass)(desc);
let contract = new ClassObj(constuctParams.unlockContractCodeHashArray);
super(contract);
this.constuctParams = constuctParams;
}
clone() {
let contract = new Nft(this.constuctParams);
contract.setFormatedDataPart(this.getFormatedDataPart());
return contract;
}
setFormatedDataPart(dataPart) {
this._formatedDataPart = Object.assign({}, this._formatedDataPart, dataPart);
this._formatedDataPart.protoVersion = nftProto.PROTO_VERSION;
this._formatedDataPart.protoType = protoheader_1.PROTO_TYPE.NFT;
super.setDataPart((0, scryptlib_1.toHex)(nftProto.newDataPart(this._formatedDataPart)));
}
getFormatedDataPart() {
return this._formatedDataPart;
}
unlock({ txPreimage, prevouts,
// nft
prevNftInputIndex, prevNftAddress, nftTxHeader, nftTxInputProof, prevNftTxProof, genesisScript,
// contract
contractInputIndex, contractTxProof,
// unlockCheck
amountCheckHashIndex, amountCheckInputIndex, amountCheckTxProof, amountCheckScrypt,
// sig
senderPubKey, senderSig,
// output
receiverAddress, nftOutputSatoshis, opReturnScript, changeAddress, changeSatoshis,
// checkInputIndex,
// checkScriptTx,
// checkScriptTxOutIndex,
// lockContractInputIndex,
// lockContractTx,
// lockContractTxOutIndex,
operation, }) {
if (!genesisScript) {
genesisScript = new scryptlib_1.Bytes('');
}
if (operation != nft_proto_1.NFT_OP_TYPE.TRANSFER) {
senderPubKey = new scryptlib_1.PubKey('00');
senderSig = new scryptlib_1.Sig('00');
receiverAddress = new scryptlib_1.Bytes('');
nftOutputSatoshis = new scryptlib_1.Int(0);
opReturnScript = new scryptlib_1.Bytes('');
changeAddress = new scryptlib_1.Ripemd160('00');
changeSatoshis = new scryptlib_1.Int(0);
}
return this._contract.unlock(txPreimage, prevouts,
// nft
prevNftInputIndex, prevNftAddress, nftTxHeader, nftTxInputProof, prevNftTxProof, genesisScript,
// contract
contractInputIndex, contractTxProof,
// unlockCheck
amountCheckHashIndex, amountCheckInputIndex, amountCheckTxProof, amountCheckScrypt,
// sig
senderPubKey, senderSig,
// output
receiverAddress, nftOutputSatoshis, opReturnScript, changeAddress, changeSatoshis,
// checkInputIndex,
// checkScriptTx,
// checkScriptTxOutIndex,
// lockContractInputIndex,
// lockContractTx,
// lockContractTxOutIndex,
operation);
}
}
exports.Nft = Nft;
class NftFactory {
static getLockingScriptSize() {
return this.lockingScriptSize;
}
static createContract(unlockContractCodeHashArray, version = 2) {
return new Nft({
unlockContractCodeHashArray,
}, version);
}
static getDummyInstance() {
let contract = this.createContract(contractUtil_1.ContractUtil.unlockContractCodeHashArray);
contract.setFormatedDataPart({});
return contract;
}
static calLockingScriptSize() {
let contract = this.getDummyInstance();
return contract.lockingScript.toBuffer().length;
}
static calUnlockingScriptSize(bsvInputLen, genesisScript, opreturnData, operation) {
return 10000;
// let opreturnScriptHex = ''
// if (opreturnData) {
// let script = mvc.Script.buildSafeDataOut(opreturnData)
// opreturnScriptHex = script.toHex()
// }
// let contract = this.getDummyInstance()
// const preimage = getPreimage(dummyTx, contract.lockingScript.toASM(), 1)
// const sig = Buffer.from(PLACE_HOLDER_SIG, 'hex')
// const rabinMsg = new Bytes(dummyPayload)
// const rabinPaddingArray: Bytes[] = []
// const rabinSigArray: Int[] = []
// const rabinPubKeyIndexArray: number[] = []
// const rabinPubKeyArray: Int[] = []
// const nftInputIndex = 0
// let prevouts = Buffer.alloc(0)
// const indexBuf = TokenUtil.getUInt32Buf(0)
// const txidBuf = TokenUtil.getTxIdBuf(dummyTxId)
// for (let i = 0; i < 1 + bsvInputLen; i++) {
// prevouts = Buffer.concat([prevouts, txidBuf, indexBuf])
// }
// let unlockCheckContact = NftUnlockContractCheckFactory.getDummyInstance(
// NFT_UNLOCK_CONTRACT_TYPE.OUT_6
// )
// let checkScriptTx = new mvc.Transaction(dummyTx.serialize(true))
// checkScriptTx.addOutput(
// new mvc.Transaction.Output({
// script: unlockCheckContact.lockingScript,
// satoshis: 10000,
// })
// )
// let sellContract = NftSellFactory.getDummyInstance()
// let sellTx = new mvc.Transaction(dummyTx.serialize(true))
// sellTx.addOutput(
// new mvc.Transaction.Output({
// script: sellContract.lockingScript,
// satoshis: 10000,
// })
// )
// let changeSatoshis = 0
// let unlockedContract = contract.unlock({
// txPreimage: new SigHashPreimage(toHex(preimage)),
// prevouts: new Bytes(toHex(prevouts)),
// rabinMsg,
// rabinPaddingArray,
// rabinSigArray,
// rabinPubKeyIndexArray,
// rabinPubKeyVerifyArray: rabinPubKeyArray,
// rabinPubKeyHashArray: new Bytes(toHex(dummyRabinPubKeyHashArray)),
// prevNftAddress: new Bytes(toHex(dummyAddress.hashBuffer)),
// genesisScript,
// senderPubKey: new PubKey(toHex(dummyPk)),
// senderSig: new Sig(toHex(sig)),
// receiverAddress: new Bytes(toHex(dummyAddress.hashBuffer)),
// nftOutputSatoshis: new Int(0),
// opReturnScript: new Bytes(opreturnScriptHex),
// changeAddress: new Ripemd160(toHex(dummyAddress.hashBuffer)),
// changeSatoshis: new Int(changeSatoshis),
// checkInputIndex: 0,
// checkScriptTx: new Bytes(checkScriptTx.serialize(true)),
// checkScriptTxOutIndex: 0,
// lockContractInputIndex: 0,
// lockContractTx: new Bytes(sellTx.serialize(true)),
// lockContractTxOutIndex: 0,
// operation,
// })
// return (unlockedContract.toScript() as mvc.Script).toBuffer().length
}
}
exports.NftFactory = NftFactory;