meta-contract
Version:
Meta Contract SDK
108 lines (107 loc) • 4.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TokenGenesisFactory = exports.TokenGenesis = void 0;
const mvc = require("../../mvc");
const ContractAdapter_1 = require("../../common/ContractAdapter");
const protoheader_1 = require("../../common/protoheader");
const scryptlib_1 = require("../../scryptlib");
const ftProto = require("../contract-proto/token.proto");
const token_1 = require("./token");
const genesisTokenIDTxid = '0000000000000000000000000000000000000000000000000000000000000000';
class TokenGenesis extends ContractAdapter_1.ContractAdapter {
// constructor(constuctParams: { pubKey: mvc.PublicKey }) {
constructor(constuctParams) {
let desc = require('../contract-desc/tokenGenesis_desc.json');
let ClassObj = (0, scryptlib_1.buildContractClass)(desc);
let contract = new ClassObj();
super(contract);
this.constuctParams = constuctParams;
this._formatedDataPart = {};
}
clone() {
let contract = new TokenGenesis(this.constuctParams);
contract.setFormatedDataPart(this.getFormatedDataPart());
return contract;
}
setFormatedDataPart(dataPart) {
this._formatedDataPart = Object.assign({}, this._formatedDataPart, dataPart);
this._formatedDataPart.genesisHash = '';
this._formatedDataPart.protoVersion = ftProto.PROTO_VERSION;
this._formatedDataPart.protoType = protoheader_1.PROTO_TYPE.FT;
super.setDataPart((0, scryptlib_1.toHex)(ftProto.newDataPart(this._formatedDataPart)));
}
getFormatedDataPart() {
return this._formatedDataPart;
}
setFormatedDataPartFromLockingScript(script) {
let dataPart = ftProto.parseDataPart(script.toBuffer());
this.setFormatedDataPart(dataPart);
}
isFirstGenesis() {
return this.getFormatedDataPart().sensibleID.txid == genesisTokenIDTxid;
}
unlock({ txPreimage, pubKey, sig, tokenScript,
// GenesisTx Input Proof
genesisTxHeader, prevInputIndex, genesisTxInputProof,
// Prev GenesisTx Output Proof
prevGenesisTxHeader, prevTxOutputHashProof, prevTxOutputSatoshiBytes, genesisSatoshis, tokenSatoshis, changeAddress, changeSatoshis, opReturnScript, }) {
return this._contract.unlock(txPreimage, pubKey, sig, tokenScript, genesisTxHeader, prevInputIndex, genesisTxInputProof, prevGenesisTxHeader, prevTxOutputHashProof, prevTxOutputSatoshiBytes, genesisSatoshis, tokenSatoshis, changeAddress, changeSatoshis, opReturnScript);
}
}
exports.TokenGenesis = TokenGenesis;
class TokenGenesisFactory {
static getLockingScriptSize() {
return this.lockingScriptSize;
}
/**
* create genesis contract
* @param {Object} issuerPubKey issuer public key used to unlocking genesis contract
* @param {string} tokenName the token name
* @param {string} tokenSymbol the token symbol
* @param {number} decimalNum the token amount decimal number
* @returns
*/
static createContract() {
return new TokenGenesis({});
}
static getDummyInstance() {
let contract = this.createContract();
// contract.setFormatedDataPart({}) // TODO:
contract.setDataPart('');
return contract;
}
static calLockingScriptSize() {
let contract = this.getDummyInstance();
let size = contract.lockingScript.toBuffer().length;
return size;
}
static calUnlockingScriptSize(opreturnData) {
let opreturnScriptHex = '';
if (opreturnData) {
let script = mvc.Script.buildSafeDataOut(opreturnData);
opreturnScriptHex = script.toHex();
}
let contract = this.getDummyInstance();
let tokenContract = token_1.TokenFactory.getDummyInstance();
// const preimage = getPreimage(dummyTx, contract.lockingScript.toASM(), 1) // TODO: fix dummy
return 10000;
// let unlockResult = contract.unlock({
// txPreimage: new SigHashPreimage(toHex(preimage)),
// sig: new Sig(toHex(sig)),
// rabinMsg: new Bytes(toHex(rabinMsg)),
// rabinPaddingArray,
// rabinSigArray,
// rabinPubKeyIndexArray,
// rabinPubKeyVerifyArray,
// rabinPubKeyHashArray: new Bytes(toHex(dummyRabinPubKeyHashArray)),
// genesisSatoshis: 1000,
// tokenScript: new Bytes(tokenContract.lockingScript.toHex()),
// tokenSatoshis: 1000,
// changeAddress: new Ripemd160(toHex(dummyAddress.hashBuffer)),
// changeSatoshis: 1000,
// opReturnScript: new Bytes(opreturnScriptHex),
// })
// return (unlockResult.toScript() as mvc.Script).toBuffer().length
}
}
exports.TokenGenesisFactory = TokenGenesisFactory;