meta-contract
Version:
Meta Contract SDK
169 lines (168 loc) • 7.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NftUnlockContractCheckFactory = exports.NFT_UNLOCK_CONTRACT_TYPE = exports.NftUnlockContractCheck = void 0;
const ContractAdapter_1 = require("../../common/ContractAdapter");
const dummy_1 = require("../../common/dummy");
const scryptlib_1 = require("../../scryptlib");
const unlockProto = require("../contract-proto/nftUnlockContractCheck.proto");
class NftUnlockContractCheck extends ContractAdapter_1.ContractAdapter {
constructor(constuctParams) {
let desc;
switch (constuctParams.unlockType) {
case NFT_UNLOCK_CONTRACT_TYPE.OUT_3:
desc = require('../contract-desc/nftUnlockContractCheck_desc.json');
break;
case NFT_UNLOCK_CONTRACT_TYPE.OUT_6:
desc = require('../contract-desc/nftUnlockContractCheck_6_desc.json');
break;
case NFT_UNLOCK_CONTRACT_TYPE.OUT_10:
desc = require('../contract-desc/nftUnlockContractCheck_10_desc.json');
break;
case NFT_UNLOCK_CONTRACT_TYPE.OUT_20:
desc = require('../contract-desc/nftUnlockContractCheck_20_desc.json');
break;
case NFT_UNLOCK_CONTRACT_TYPE.OUT_100:
desc = require('../contract-desc/nftUnlockContractCheck_100_desc.json');
break;
default:
throw 'invalid checkType';
}
const NftUnlockContractCheckClass = (0, scryptlib_1.buildContractClass)(desc);
const unlockCheckContract = new NftUnlockContractCheckClass();
super(unlockCheckContract);
this.constuctParams = constuctParams;
}
clone() {
let contract = new NftUnlockContractCheck(this.constuctParams);
contract.setFormatedDataPart(this.getFormatedDataPart());
return contract;
}
setFormatedDataPart(dataPart) {
this._formatedDataPart = Object.assign({}, this._formatedDataPart, dataPart);
super.setDataPart((0, scryptlib_1.toHex)(unlockProto.newDataPart(this._formatedDataPart)));
}
getFormatedDataPart() {
return this._formatedDataPart;
}
unlock({ txPreimage, prevouts, nftInputIndex, nftScript, nftTxHeader, nftTxHashProof, nftSatoshiBytes, nOutputs, txNftOutputIndex, nftOutputAddress, nftOutputSatoshis, otherOutputArray, }) {
return this._contract.unlock(txPreimage, prevouts,
// nft
nftInputIndex, nftScript, nftTxHeader, nftTxHashProof, nftSatoshiBytes,
// output
nOutputs, txNftOutputIndex, nftOutputAddress, nftOutputSatoshis, otherOutputArray);
}
}
exports.NftUnlockContractCheck = NftUnlockContractCheck;
var NFT_UNLOCK_CONTRACT_TYPE;
(function (NFT_UNLOCK_CONTRACT_TYPE) {
NFT_UNLOCK_CONTRACT_TYPE[NFT_UNLOCK_CONTRACT_TYPE["OUT_3"] = 1] = "OUT_3";
NFT_UNLOCK_CONTRACT_TYPE[NFT_UNLOCK_CONTRACT_TYPE["OUT_6"] = 2] = "OUT_6";
NFT_UNLOCK_CONTRACT_TYPE[NFT_UNLOCK_CONTRACT_TYPE["OUT_10"] = 3] = "OUT_10";
NFT_UNLOCK_CONTRACT_TYPE[NFT_UNLOCK_CONTRACT_TYPE["OUT_20"] = 4] = "OUT_20";
NFT_UNLOCK_CONTRACT_TYPE[NFT_UNLOCK_CONTRACT_TYPE["OUT_100"] = 5] = "OUT_100";
NFT_UNLOCK_CONTRACT_TYPE[NFT_UNLOCK_CONTRACT_TYPE["UNSUPPORT"] = 6] = "UNSUPPORT";
})(NFT_UNLOCK_CONTRACT_TYPE = exports.NFT_UNLOCK_CONTRACT_TYPE || (exports.NFT_UNLOCK_CONTRACT_TYPE = {}));
let _unlockContractTypeInfos = [
{
type: NFT_UNLOCK_CONTRACT_TYPE.OUT_3,
out: 3,
lockingScriptSize: 0,
},
{
type: NFT_UNLOCK_CONTRACT_TYPE.OUT_6,
out: 6,
lockingScriptSize: 0,
},
{
type: NFT_UNLOCK_CONTRACT_TYPE.OUT_10,
out: 10,
lockingScriptSize: 0,
},
{
type: NFT_UNLOCK_CONTRACT_TYPE.OUT_20,
out: 20,
lockingScriptSize: 0,
},
{
type: NFT_UNLOCK_CONTRACT_TYPE.OUT_100,
out: 100,
lockingScriptSize: 0,
},
];
class NftUnlockContractCheckFactory {
static getLockingScriptSize(unlockType) {
return this.unlockContractTypeInfos.find((v) => v.type == unlockType).lockingScriptSize;
}
static getOptimumType(outCount) {
if (outCount <= 3) {
return NFT_UNLOCK_CONTRACT_TYPE.OUT_3;
}
else if (outCount <= 6) {
return NFT_UNLOCK_CONTRACT_TYPE.OUT_6;
}
else if (outCount <= 10) {
return NFT_UNLOCK_CONTRACT_TYPE.OUT_10;
}
else if (outCount <= 20) {
return NFT_UNLOCK_CONTRACT_TYPE.OUT_20;
}
else if (outCount <= 100) {
return NFT_UNLOCK_CONTRACT_TYPE.OUT_100;
}
else {
return NFT_UNLOCK_CONTRACT_TYPE.UNSUPPORT;
}
}
static createContract(unlockType) {
return new NftUnlockContractCheck({ unlockType });
}
static getDummyInstance(unlockType) {
let contract = this.createContract(unlockType);
contract.setFormatedDataPart({
nftID: dummy_1.dummyCodehash.toBuffer(),
nftCodeHash: Buffer.from('0000000000000000000000000000000000000000', 'hex'),
});
return contract;
}
static calLockingScriptSize(unlockType) {
let contract = this.getDummyInstance(unlockType);
return contract.lockingScript.toBuffer().length;
}
static calUnlockingScriptSize(unlockType, prevouts, otherOutputArray) {
return 10000; // TODO
// let contract = this.getDummyInstance(unlockType)
// let nftContractInstance = NftFactory.getDummyInstance()
// const preimage = getPreimage(dummyTx, contract.lockingScript.toASM(), 1)
// const rabinMsg = new Bytes(dummyPayload)
// let paddingCountBuf = Buffer.alloc(2, 0)
// paddingCountBuf.writeUInt16LE(dummyPadding.length / 2)
// const padding = Buffer.alloc(dummyPadding.length / 2, 0)
// padding.write(dummyPadding, 'hex')
// const rabinPaddingArray: Bytes[] = []
// const rabinSigArray: Int[] = []
// const rabinPubKeyIndexArray: number[] = []
// const rabinPubKeyArray: Int[] = []
// let tokenAmount = Buffer.alloc(8)
// tokenAmount.writeInt32BE(100000)
// let unlockedContract = contract.unlock({
// txPreimage: new SigHashPreimage(toHex(preimage)),
// nftInputIndex: 0,
// nftScript: new Bytes(nftContractInstance.lockingScript.toHex()),
// prevouts: prevouts,
// rabinMsg: rabinMsg,
// rabinPaddingArray: rabinPaddingArray,
// rabinSigArray: rabinSigArray,
// rabinPubKeyIndexArray,
// rabinPubKeyVerifyArray: rabinPubKeyArray,
// rabinPubKeyHashArray: new Bytes(toHex(dummyRabinPubKeyHashArray)),
// nOutputs: 2,
// nftOutputIndex: 0,
// nftOutputAddress: new Bytes(toHex(dummyAddress.hashBuffer)),
// nftOutputSatoshis: 1000,
// otherOutputArray,
// })
// return (unlockedContract.toScript() as mvc.Script).toBuffer().length
}
}
exports.NftUnlockContractCheckFactory = NftUnlockContractCheckFactory;
NftUnlockContractCheckFactory.unlockContractTypeInfos = _unlockContractTypeInfos;