"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.scriptToProto = exports.dataEntryToProto = exports.signedTxToProto = exports.txToProto = exports.protoBytesToOrder = exports.orderToProtoBytes = exports.protoTxDataToTx = exports.protoBytesToTx = exports.protoBytesToSignedTx = exports.signedTxToProtoBytes = exports.txToProtoBytes = void 0; const wavesProto = __importStar(require("@waves/protobuf-serialization")); const ts_lib_crypto_1 = require("@waves/ts-lib-crypto"); const marshall_1 = require("@waves/marshall"); const ts_types_1 = require("@waves/ts-types"); const generic_1 = require("./generic"); const long_1 = __importDefault(require("long")); const invokeScriptCallSchema = Object.assign({}, marshall_1.schemas.txFields.functionCall[1]); const recipientFromProto = (recipient, chainId) => { if (recipient.alias) { return `alias:${String.fromCharCode(chainId)}:${recipient.alias}`; } const rawAddress = ts_lib_crypto_1.concat([1], [chainId], recipient.publicKeyHash); const checkSum = ts_lib_crypto_1.keccak(ts_lib_crypto_1.blake2b(rawAddress)).slice(0, 4); return ts_lib_crypto_1.base58Encode(ts_lib_crypto_1.concat(rawAddress, checkSum)); }; function convertNumber(n) { const maxJsNumber = Math.pow(2, 53) - 1; return n.toNumber() > maxJsNumber ? n.toString() : n.toNumber(); } function txToProtoBytes(obj) { return new Uint8Array(wavesProto.waves.Transaction.encode(exports.txToProto(obj)).finish()); } exports.txToProtoBytes = txToProtoBytes; function signedTxToProtoBytes(obj) { return new Uint8Array(wavesProto.waves.SignedTransaction.encode(exports.signedTxToProto(obj)).finish()); } exports.signedTxToProtoBytes = signedTxToProtoBytes; function protoBytesToSignedTx(bytes) { const txData = wavesProto.waves.SignedTransaction.decode(bytes); const tx = protoTxDataToTx(txData.transaction); const signedTx = Object.assign(Object.assign({}, tx), { proofs: (txData.proofs || []).map(uint8Array2proof) }); return signedTx; } exports.protoBytesToSignedTx = protoBytesToSignedTx; function protoBytesToTx(bytes) { const t = wavesProto.waves.Transaction.decode(bytes); const res = protoTxDataToTx(t); return res; } exports.protoBytesToTx = protoBytesToTx; function protoTxDataToTx(t) { // | 'invokeExpression' let res = { version: t.version, type: typeByName[t.data], senderPublicKey: ts_lib_crypto_1.base58Encode(t.senderPublicKey), timestamp: t.timestamp.toNumber(), fee: convertNumber(t.fee.amount), }; if (t.fee.hasOwnProperty('assetId')) { res.feeAssetId = ts_lib_crypto_1.base58Encode(t.fee.assetId); } else { res.feeAssetId = null; } if (t.hasOwnProperty('chainId')) { res.chainId = t.chainId; } switch (t.data) { case 'issue': res.name = t.issue.name; res.description = t.issue.description; res.quantity = convertNumber(t.issue.amount); res.decimals = t.issue.decimals; res.reissuable = t.issue.reissuable; res.script = (t.issue.hasOwnProperty('script')) ? generic_1.base64Prefix(ts_lib_crypto_1.base64Encode(t.issue.script)) : null; break; case 'transfer': res.amount = convertNumber(t.transfer.amount.amount); res.recipient = recipientFromProto(t.transfer.recipient, t.chainId); res.attachment = (t.transfer.hasOwnProperty('attachment')) ? ts_lib_crypto_1.base58Encode(t.transfer.attachment) : ''; res.assetId = (t.transfer.amount.hasOwnProperty('assetId')) ? ts_lib_crypto_1.base58Encode(t.transfer.amount.assetId) : null; break; case 'reissue': res.quantity = convertNumber(t.reissue.assetAmount.amount); res.assetId = t.reissue.assetAmount.assetId == null ? null : ts_lib_crypto_1.base58Encode(t.reissue.assetAmount.assetId); res.reissuable = t.reissue.reissuable; break; case 'burn': res.amount = convertNumber(t.burn.assetAmount.amount); res.assetId = ts_lib_crypto_1.base58Encode(t.burn.assetAmount.assetId); break; case 'exchange': res.amount = convertNumber(t.exchange.amount); res.price = convertNumber(t.exchange.price); res.buyMatcherFee = convertNumber(t.exchange.buyMatcherFee); res.sellMatcherFee = convertNumber(t.exchange.sellMatcherFee); res.order1 = orderFromProto(t.exchange.orders[0]); res.order2 = orderFromProto(t.exchange.orders[1]); break; case 'lease': res.recipient = recipientFromProto(t.lease.recipient, t.chainId); res.amount = convertNumber(t.lease.amount); break; case 'leaseCancel': res.leaseId = ts_lib_crypto_1.base58Encode(t.leaseCancel.leaseId); break; case 'createAlias': res.alias = t.createAlias.alias; break; case 'massTransfer': res.assetId = (t.massTransfer.hasOwnProperty('assetId')) ? ts_lib_crypto_1.base58Encode(t.massTransfer.assetId) : null; res.attachment = (t.massTransfer.hasOwnProperty('attachment')) ? ts_lib_crypto_1.base58Encode(t.massTransfer.attachment) : ''; res.transfers = t.massTransfer.transfers.map(({ amount, recipient }) => ({ amount: convertNumber(amount), recipient: recipientFromProto(recipient, t.chainId), })); break; case 'dataTransaction': res.data = t.dataTransaction.data.map(de => { if (de.hasOwnProperty('binaryValue')) return { key: de.key, type: 'binary', value: generic_1.base64Prefix(ts_lib_crypto_1.base64Encode(de.binaryValue)), }; if (de.hasOwnProperty('boolValue')) return { key: de.key, type: 'boolean', value: de.boolValue }; if (de.hasOwnProperty('intValue')) return { key: de.key, type: 'integer', value: convertNumber(de.intValue) }; if (de.hasOwnProperty('stringValue')) return { key: de.key, type: 'string', value: de.stringValue }; return { key: de.key, value: null }; }); break; case 'setScript': res.script = (t.setScript.hasOwnProperty('script')) ? generic_1.base64Prefix(ts_lib_crypto_1.base64Encode(t.setScript.script)) : null; break; case 'sponsorFee': res.minSponsoredAssetFee = convertNumber(t.sponsorFee.minFee.amount); res.assetId = ts_lib_crypto_1.base58Encode(t.sponsorFee.minFee.assetId); break; case 'setAssetScript': res.assetId = ts_lib_crypto_1.base58Encode(t.setAssetScript.assetId); res.script = generic_1.base64Prefix(ts_lib_crypto_1.base64Encode(t.setAssetScript.script)); break; case 'invokeScript': res.dApp = recipientFromProto(t.invokeScript.dApp, t.chainId); if (t.invokeScript.functionCall != null) { res.call = marshall_1.binary.parserFromSchema(invokeScriptCallSchema)(t.invokeScript.functionCall).value; //todo: export function call from marshall and use it directly } res.payment = t.invokeScript.payments.map(p => ({ amount: convertNumber(p.amount), assetId: p.hasOwnProperty('assetId') ? ts_lib_crypto_1.base58Encode(p.assetId) : null })); break; case 'updateAssetInfo': res.assetId = ts_lib_crypto_1.base58Encode(t.updateAssetInfo.assetId); res.name = t.updateAssetInfo.name; res.description = t.updateAssetInfo.description; break; // case 'invokeExpression': // res.expression = t.invokeExpression?.expression == null ? null : base64Prefix(base64Encode(t.invokeExpression?.expression)) // break default: throw new Error(`Unsupported tx type ${t.data}`); } if (res.hasOwnProperty('chainId')) { res.sender = ts_lib_crypto_1.address({ publicKey: t.senderPublicKey }, t.chainId); } else { let recipient = res.recipient || res.dApp || (res.transfers && res.transfers[0] && res.transfers[0].recipient); if (recipient) { res.sender = ts_lib_crypto_1.address({ publicKey: t.senderPublicKey }, generic_1.chainIdFromRecipient(recipient)); } } return res; } exports.protoTxDataToTx = protoTxDataToTx; function orderToProtoBytes(obj) { return wavesProto.waves.Order.encode(orderToProto(obj)).finish(); } exports.orderToProtoBytes = orderToProtoBytes; function protoBytesToOrder(bytes) { const o = wavesProto.waves.Order.decode(bytes); return orderFromProto(o); } exports.protoBytesToOrder = protoBytesToOrder; const getCommonFields = (_a) => { var { senderPublicKey, fee, timestamp, type, version } = _a, rest = __rest(_a, ["senderPublicKey", "fee", "timestamp", "type", "version"]); const typename = nameByType[type]; let chainId = rest.chainId; if (chainId == null) { const r = rest; let recipient = r.recipient || r.dApp || (r.transfers && r.transfers[0] && r.transfers[0].recipient); if (recipient) { chainId = generic_1.chainIdFromRecipient(recipient); } } return { version, type, chainId, senderPublicKey: ts_lib_crypto_1.base58Decode(senderPublicKey), timestamp: long_1.default.fromValue(timestamp), fee: amountToProto(fee, rest.feeAssetId), data: typename, }; }; const getCommonSignedFields = (tx) => { const fields = getCommonFields(tx); if (tx.hasOwnProperty('proofs')) { fields.proofs = tx.proofs; } return fields; }; const getIssueData = (t) => ({ name: t.name, description: t.description === '' ? null : t.description, amount: long_1.default.fromValue(t.quantity), decimals: t.decimals === 0 ? null : t.decimals, reissuable: t.reissuable ? true : undefined, script: t.script == null ? null : exports.scriptToProto(t.script), }); const getTransferData = (t) => ({ recipient: recipientToProto(t.recipient), amount: amountToProto(t.amount, t.assetId), attachment: t.attachment == null || t.attachment == '' ? undefined : ts_lib_crypto_1.base58Decode(t.attachment), }); const getReissueData = (t) => ({ assetAmount: amountToProto(t.quantity, t.assetId), reissuable: t.reissuable ? true : undefined, }); const getBurnData = (t) => ({ assetAmount: amountToProto(t.amount || t.amount, t.assetId), }); const getExchangeData = (t) => ({ amount: long_1.default.fromValue(t.amount), price: long_1.default.fromValue(t.price), buyMatcherFee: long_1.default.fromValue(t.buyMatcherFee), sellMatcherFee: long_1.default.fromValue(t.sellMatcherFee), orders: [orderToProto(Object.assign({ chainId: t.chainId }, t.order1)), orderToProto(Object.assign({ chainId: t.chainId }, t.order2))], }); const getLeaseData = (t) => ({ recipient: recipientToProto(t.recipient), amount: long_1.default.fromValue(t.amount), }); const getCancelLeaseData = (t) => ({ leaseId: ts_lib_crypto_1.base58Decode(t.leaseId), }); const getAliasData = (t) => ({ alias: t.alias }); const getMassTransferData = (t) => ({ assetId: t.assetId == null ? null : ts_lib_crypto_1.base58Decode(t.assetId), attachment: t.attachment == null || t.attachment == '' ? undefined : ts_lib_crypto_1.base58Decode(t.attachment), transfers: t.transfers.map(massTransferItemToProto), }); const getDataTxData = (t) => ({ data: t.data.map(exports.dataEntryToProto), }); const getSetScriptData = (t) => ({ script: t.script == null ? null : exports.scriptToProto(t.script), }); const getSponsorData = (t) => ({ minFee: t.minSponsoredAssetFee === null ? amountToProto(0, t.assetId) : amountToProto(t.minSponsoredAssetFee, t.assetId), }); const getSetAssetScriptData = (t) => ({ assetId: ts_lib_crypto_1.base58Decode(t.assetId), script: t.script == null ? null : exports.scriptToProto(t.script), }); const getInvokeData = (t) => ({ dApp: recipientToProto(t.dApp), functionCall: marshall_1.binary.serializerFromSchema(marshall_1.schemas.invokeScriptSchemaV1.schema[5][1])(t.call), payments: t.payment == null ? null : t.payment.map(({ amount, assetId }) => amountToProto(amount, assetId)), }); const getUpdateAssetInfoData = (t) => { return { assetId: ts_lib_crypto_1.base58Decode(t.assetId), name: t.name, description: t.description === '' ? null : t.description, }; }; // const getInvokeExpressionData = (t: InvokeExpressionTransaction): wavesProto.waves.IInvokeExpressionTransactionData => { // return { // expression: t.expression == null ? null : scriptToProto(t.expression), // } // } const getTxData = (t) => { let txData; switch (t.type) { case ts_types_1.TRANSACTION_TYPE.ISSUE: txData = getIssueData(t); break; case ts_types_1.TRANSACTION_TYPE.TRANSFER: txData = getTransferData(t); break; case ts_types_1.TRANSACTION_TYPE.REISSUE: txData = getReissueData(t); break; case ts_types_1.TRANSACTION_TYPE.BURN: txData = getBurnData(t); break; case ts_types_1.TRANSACTION_TYPE.LEASE: txData = getLeaseData(t); break; case ts_types_1.TRANSACTION_TYPE.CANCEL_LEASE: txData = getCancelLeaseData(t); break; case ts_types_1.TRANSACTION_TYPE.ALIAS: txData = getAliasData(t); break; case ts_types_1.TRANSACTION_TYPE.MASS_TRANSFER: txData = getMassTransferData(t); break; case ts_types_1.TRANSACTION_TYPE.DATA: txData = getDataTxData(t); break; case ts_types_1.TRANSACTION_TYPE.SET_SCRIPT: txData = getSetScriptData(t); break; case ts_types_1.TRANSACTION_TYPE.SET_ASSET_SCRIPT: txData = getSetAssetScriptData(t); break; case ts_types_1.TRANSACTION_TYPE.SPONSORSHIP: txData = getSponsorData(t); break; case ts_types_1.TRANSACTION_TYPE.EXCHANGE: txData = getExchangeData(t); break; case ts_types_1.TRANSACTION_TYPE.INVOKE_SCRIPT: txData = getInvokeData(t); break; case ts_types_1.TRANSACTION_TYPE.UPDATE_ASSET_INFO: txData = getUpdateAssetInfoData(t); break; // case TRANSACTION_TYPE.INVOKE_EXPRESSION: // txData = getInvokeExpressionData(t) // break } return txData; }; exports.txToProto = (t) => { const common = getCommonFields(t); const txData = getTxData(t); return Object.assign(Object.assign({}, common), { [common.data]: txData }); }; exports.signedTxToProto = (t) => { const common = getCommonSignedFields(t); const txData = getTxData(t); return { wavesTransaction: Object.assign(Object.assign({}, common), { [common.data]: txData }), proofs: (t.proofs || []).map(proof2Uint8Array), }; }; const orderToProto = (o) => { var _a; let priceMode; if (o.version === 4 && 'priceMode' in o) { if (o.priceMode === 0 || o.priceMode === 'default') { priceMode = undefined; } o.priceMode === 'assetDecimals' ? priceMode = wavesProto.waves.Order.PriceMode.ASSET_DECIMALS : priceMode = wavesProto.waves.Order.PriceMode.FIXED_DECIMALS; } else priceMode = undefined; const isNullOrWaves = (asset) => asset == null || asset.toLowerCase() == 'waves'; return ({ chainId: o.chainId, senderPublicKey: o.senderPublicKey ? ts_lib_crypto_1.base58Decode(o.senderPublicKey) : null, matcherPublicKey: ts_lib_crypto_1.base58Decode(o.matcherPublicKey), assetPair: { amountAssetId: isNullOrWaves(o.assetPair.amountAsset) ? null : ts_lib_crypto_1.base58Decode(o.assetPair.amountAsset), priceAssetId: isNullOrWaves(o.assetPair.priceAsset) ? null : ts_lib_crypto_1.base58Decode(o.assetPair.priceAsset), }, orderSide: o.orderType === 'buy' ? undefined : wavesProto.waves.Order.Side.SELL, amount: long_1.default.fromValue(o.amount), price: long_1.default.fromValue(o.price), timestamp: long_1.default.fromValue(o.timestamp), expiration: long_1.default.fromValue(o.expiration), matcherFee: amountToProto(o.matcherFee, o.matcherFeeAssetId ? o.matcherFeeAssetId : null), version: o.version, proofs: (_a = o.proofs) === null || _a === void 0 ? void 0 : _a.map(ts_lib_crypto_1.base58Decode), eip712Signature: o.eip712Signature ? ts_lib_crypto_1.base16Decode(o.eip712Signature.slice(2)) : undefined, priceMode: priceMode, }); }; const orderFromProto = (po) => { var _a; let priceMode; if (po.version === 4 && po.priceMode) { po.priceMode === 1 ? priceMode = 'fixedDecimals' : priceMode = 'assetDecimals'; } return { version: po.version, senderPublicKey: ts_lib_crypto_1.base58Encode(po.senderPublicKey), matcherPublicKey: ts_lib_crypto_1.base58Encode(po.matcherPublicKey), assetPair: { amountAsset: po.assetPair.amountAssetId == null ? null : ts_lib_crypto_1.base58Encode(po.assetPair.amountAssetId), priceAsset: po.assetPair.priceAssetId == null ? null : ts_lib_crypto_1.base58Encode(po.assetPair.priceAssetId), }, // @ts-ignore chainId: po.chainId, orderType: po.orderSide === wavesProto.waves.Order.Side.BUY ? 'buy' : 'sell', amount: convertNumber(po.amount), price: convertNumber(po.price), timestamp: po.timestamp.toNumber(), expiration: po.expiration.toNumber(), matcherFee: convertNumber(po.matcherFee.amount), matcherFeeAssetId: po.matcherFee.assetId == null ? null : ts_lib_crypto_1.base58Encode(po.matcherFee.assetId), // @ts-ignore priceMode: priceMode, eip712Signature: ((_a = po.eip712Signature) === null || _a === void 0 ? void 0 : _a.length) ? `0x${ts_lib_crypto_1.base16Encode(po.eip712Signature)}` : undefined }; }; const recipientToProto = (r) => ({ alias: r.startsWith('alias') ? r.slice(8) : undefined, publicKeyHash: !r.startsWith('alias') ? ts_lib_crypto_1.base58Decode(r).slice(2, -4) : undefined, }); const amountToProto = (a, assetId) => ({ amount: a == 0 ? null : long_1.default.fromValue(a), assetId: assetId == null ? null : ts_lib_crypto_1.base58Decode(assetId), }); const massTransferItemToProto = (mti) => ({ recipient: recipientToProto(mti.recipient), amount: mti.amount == 0 ? null : long_1.default.fromValue(mti.amount), }); exports.dataEntryToProto = (de) => ({ key: de.key, intValue: de.type === 'integer' ? long_1.default.fromValue(de.value) : undefined, boolValue: de.type === 'boolean' ? de.value : undefined, binaryValue: de.type === 'binary' ? ts_lib_crypto_1.base64Decode((de.value.startsWith('base64:') ? de.value.slice(7) : de.value)) : undefined, stringValue: de.type === 'string' ? de.value : undefined, }); exports.scriptToProto = (s) => { return s ? ts_lib_crypto_1.base64Decode(s.toString().startsWith('base64:') ? s.slice(7) : s) : null; }; const nameByType = { 1: 'genesis', 2: 'payment', 3: 'issue', 4: 'transfer', 5: 'reissue', 6: 'burn', 7: 'exchange', 8: 'lease', 9: 'leaseCancel', 10: 'createAlias', 11: 'massTransfer', 12: 'dataTransaction', 13: 'setScript', 14: 'sponsorFee', 15: 'setAssetScript', 16: 'invokeScript', 17: 'updateAssetInfo', }; const typeByName = { 'genesis': 1, 'payment': 2, 'issue': 3, 'transfer': 4, 'reissue': 5, 'burn': 6, 'exchange': 7, 'lease': 8, 'leaseCancel': 9, 'createAlias': 10, 'massTransfer': 11, 'dataTransaction': 12, 'setScript': 13, 'sponsorFee': 14, 'setAssetScript': 15, 'invokeScript': 16, 'updateAssetInfo': 17, }; const proof2Uint8Array = (proof) => { return ts_lib_crypto_1.base58Decode(proof); }; const uint8Array2proof = (proofBytes) => { return ts_lib_crypto_1.base58Encode(proofBytes); }; //# sourceMappingURL=proto-serialize.js.map