import { Network } from "./types"; import { BigNumber } from "bignumber.js"; import { generateAddressHelper } from "./AddressHelper"; import { extractSignatureHelper } from "./SignatureHelper"; import { Coin } from "./TransactionHelpers/Coin"; import { MsgSend } from "./TransactionHelpers/MsgSend"; import { Msg } from "./TransactionHelpers/Msg"; import { TxBody } from "./TransactionHelpers/TxBody"; import { ModeInfo, ModeInfoSingle } from "./TransactionHelpers/ModeInfo"; import { SignerInfo } from "./TransactionHelpers/SignerInfo"; import { Fee } from "./TransactionHelpers/Fee"; import { AuthInfo } from "./TransactionHelpers/AuthInfo"; import { SignDoc } from "./TransactionHelpers/SignDoc"; import { SignedTx } from "./TransactionHelpers/SignedTx"; import { MsgDelegate } from "./TransactionHelpers/MsgDelegate"; import { MsgUndelegate } from "./TransactionHelpers/MsgUndelegate"; import { MsgBeginRedelegate } from "./TransactionHelpers/MsgBeginRedelegate"; import { MsgWithdrawDelegatorReward } from "./TransactionHelpers/MsgWithdrawDelegatorReward"; import { buildBeginRedelegateTransactionBody, buildClaimRewardTransactionBody, buildCompoundRewardTransactionBody, buildDelegateTransactionBody, buildTransactionBody, buildUndelegateTransactionBody } from "./TransactionBodyHelper"; var shajs = require("sha.js"); var protobuf = require("protobufjs/minimal"); export let protoW = protobuf.Writer; export type BuildTransactionArgs = { fromPubK: Buffer; toAddress: string; amount: string; fees: string; sequence: string; accountNumber: string; memo: string; signature?: Buffer; network: Network; }; export type BuildRedelegateTransactionArgs = { fromPubK: Buffer; sourceValidator: string; destValidator: string amount: string; fees: string; sequence: string; accountNumber: string; memo: string; signature?: Buffer; network: Network; } enum TxType{ MsgSend, MsgDelegate, MsgUndelegate, MsgBeginRedelegate, MsgWithdrawDelegatorReward, MsgCompound } const buildAuthInfo = (fromPubK: Buffer, sequence: string, fees: string) => { ///Get signerInfo var buf1 = Buffer.from([10]); var buf2 = Buffer.from([fromPubK.length]); fromPubK = Buffer.concat([buf1, buf2, fromPubK]); const pubKeyMsg = new Msg("/cosmos.crypto.secp256k1.PubKey", fromPubK); let mode_info = new ModeInfo(new ModeInfoSingle()); const sequenceN = parseInt(sequence); const signerInfo = new SignerInfo(pubKeyMsg, mode_info, sequenceN); //Get feeValue let feesBN = new BigNumber(fees); feesBN = feesBN.times(new BigNumber(1000000)); const feesV = new Coin("uatom", feesBN.toFixed()); const feeValue = new Fee([feesV], 400000); //Get AuthInfo return new AuthInfo([signerInfo], feeValue); }; const buildDataToSign = (args, type: TxType) => { //Get transaction body const from = generateAddressHelper(args.fromPubK); let txBody; if(type == TxType.MsgSend){ txBody = buildTransactionBody(from, args.toAddress, args.amount, args.memo); }else if(type == TxType.MsgDelegate){ txBody = buildDelegateTransactionBody(from, args.toAddress, args.amount, args.memo); }else if(type == TxType.MsgUndelegate){ txBody = buildUndelegateTransactionBody(from, args.toAddress, args.amount, args.memo); }else if(type == TxType.MsgBeginRedelegate){ txBody = buildBeginRedelegateTransactionBody(from, args.sourceValidator, args.destValidator, args.amount, args.memo); }else if(type == TxType.MsgWithdrawDelegatorReward){ txBody = buildClaimRewardTransactionBody(from, args.toAddress, args.memo); }else if(type == TxType.MsgCompound){ txBody = buildCompoundRewardTransactionBody(from, args.toAddress, args.amount, args.memo); } //Get AuthInfo const authInfo = buildAuthInfo(args.fromPubK, args.sequence, args.fees); //Get encoded bytes of tx body and auth info const bodyBytes = txBody.encode(null).finish(); const authInfoBytes = authInfo.encode(null).finish(); //Build SignDoc const chainId = args.network == Network.Testnet ? "cosmoshub-testnet" : "cosmoshub-4"; //Identificador de la red const signDoc = new SignDoc( bodyBytes, authInfoBytes, chainId, parseInt(args.accountNumber) ); let signMessage = signDoc.encode(null).finish(); return signMessage; }; const buildSignedTx = (args, type: TxType) => { //Get transaction body const from = generateAddressHelper(args.fromPubK); let txBody; if(type == TxType.MsgSend){ txBody = buildTransactionBody(from, args.toAddress, args.amount, args.memo); }else if(type == TxType.MsgDelegate){ txBody = buildDelegateTransactionBody(from, args.toAddress, args.amount, args.memo); }else if(type == TxType.MsgUndelegate){ txBody = buildUndelegateTransactionBody(from, args.toAddress, args.amount, args.memo); }else if(type == TxType.MsgBeginRedelegate){ txBody = buildBeginRedelegateTransactionBody(from, args.sourceValidator, args.destValidator, args.amount, args.memo); }else if(type == TxType.MsgWithdrawDelegatorReward){ txBody = buildClaimRewardTransactionBody(from, args.toAddress, args.memo); }else if(type == TxType.MsgCompound){ txBody = buildCompoundRewardTransactionBody(from, args.toAddress, args.amount, args.memo); } //Get AuthInfo const authInfo = buildAuthInfo(args.fromPubK, args.sequence, args.fees); //Get encoded bytes of tx body and auth info const bodyBytes = txBody.encode(null).finish(); const authInfoBytes = authInfo.encode(null).finish(); let signatures = []; if (args.signature !== undefined) { const signature = extractSignatureHelper(args.signature); signatures.push(signature); } const signedTx = new SignedTx(bodyBytes, authInfoBytes, signatures); return signedTx.encode(null).finish(); }; //#region MsgSendType export const buildTransactionHelper = (args: BuildTransactionArgs) => { return buildSignedTx(args, TxType.MsgSend); }; export const getHolaParameters_LINKHelper = (args: BuildTransactionArgs) => { //Get blk let tx = buildDataToSign(args, TxType.MsgSend); let hash = shajs("sha256").update(tx).digest(); let blkData = []; blkData.push(hash); return { blk: blkData }; }; //#endregion //#region MsgDelegateType export const buildDelegateTransactionHelper = (args: BuildTransactionArgs) => { return buildSignedTx(args, TxType.MsgDelegate); } export const getDelegateHolaParameters_LINKHelper = (args: BuildTransactionArgs) => { //Get blk let tx = buildDataToSign(args, TxType.MsgDelegate); let hash = shajs("sha256").update(tx).digest(); let blkData = []; blkData.push(hash); return { blk: blkData }; }; //#endregion //#region MsgUndelegateType export const buildUndelegateTransactionHelper = (args: BuildTransactionArgs) => { return buildSignedTx(args, TxType.MsgUndelegate); } export const getUndelegateHolaParameters_LINKHelper = (args: BuildTransactionArgs) => { //Get blk let tx = buildDataToSign(args, TxType.MsgUndelegate); let hash = shajs("sha256").update(tx).digest(); let blkData = []; blkData.push(hash); return { blk: blkData }; }; //#endregion //#region MsgBeginRedelegateType export const buildBeginRedelegateTransactionHelper = (args: BuildRedelegateTransactionArgs) => { return buildSignedTx(args, TxType.MsgBeginRedelegate); } export const getBeginRedelegateHolaParameters_LINKHelper = (args: BuildRedelegateTransactionArgs) => { //Get blk let tx = buildDataToSign(args, TxType.MsgBeginRedelegate); let hash = shajs("sha256").update(tx).digest(); let blkData = []; blkData.push(hash); return { blk: blkData }; }; //#endregion //#region MsgWithdrawDelegatorReward export const buildClaimRewardTransactionHelper = (args: BuildTransactionArgs) => { return buildSignedTx(args, TxType.MsgWithdrawDelegatorReward); } export const getClaimRewardHolaParameters_LINKHelper = (args: BuildTransactionArgs) => { //Get blk let tx = buildDataToSign(args, TxType.MsgWithdrawDelegatorReward); let hash = shajs("sha256").update(tx).digest(); let blkData = []; blkData.push(hash); return { blk: blkData }; }; //#endregion //#region MsgWithdrawDelegatorRewardType + MsgDelegateType export const buildCompoundRewardTransactionHelper = (args: BuildTransactionArgs) => { return buildSignedTx(args, TxType.MsgCompound); } export const getCompoundRewardHolaParameters_LINKHelper = (args: BuildTransactionArgs) => { //Get blk let tx = buildDataToSign(args, TxType.MsgCompound); let hash = shajs("sha256").update(tx).digest(); let blkData = []; blkData.push(hash); return { blk: blkData }; }; //#endregion