import { getAccount } from "./AccountHelper"; import { getMemoBytes } from "./MemoHelper"; import { getOperationBytes } from "./OperationHelper"; import { Account, Network, OperationType } from "./types"; import * as constants from "./Constants"; import BigNumber from "bignumber.js"; import { sha256 } from "js-sha256"; const bigint = require('big-integer') export type BuildTransactionArgs = { from: string, to: string, amount:string, fee: string, sequenceNumber: string, memo: string, type: OperationType, network: Network, signature?: Buffer } export function buildTransactionHelper(args: BuildTransactionArgs): Buffer{ return Buffer.concat([ getTransactionData(args), //Get transactionBytes getSignatureData(getAccount(args.from), args.signature) //Add signature info ]); } export function toBase64Helper(data: Buffer): string{ return data.toString('base64'); } export function getDataToSignHelper(args: BuildTransactionArgs): Buffer{ return Buffer.concat([ getPassAndEnvelopeData(args.network), getTransactionData(args) ]) } export function getHolaParametersHelper(args: BuildTransactionArgs): {blk: any[], u32: number[]}{ //Fist half = Pass + Envelope type + Common data let firstHalf = Buffer.concat([ getPassAndEnvelopeData(args.network), getCommonTxData(args) ]); let amountBN = new BigNumber(args.amount).times(new BigNumber(10000000)); let amountBI = bigint(amountBN.toFixed()); //Second hallf = Operation data + Signature type let secondHalf = Buffer.concat([ getOperationBytes({type: args.type, destinationPubK: getAccount(args.to).publicKey, amountTx: amountBI}), Buffer.from([0,0,0,0]) ]) let blkData = []; blkData.push(firstHalf, secondHalf); //Get u32 let u32Data = []; let valueD = new BigNumber(args.amount).toFixed();; let amountPrecision = 1; if(valueD.includes(".")){ amountPrecision = valueD.split(".")[1].length; } u32Data.push(amountPrecision); let feesD = new BigNumber(args.fee).toFixed(); let feesPrecision = 1; if(feesD.includes(".")){ feesPrecision = feesD.split(".")[1].length; } u32Data.push(feesPrecision); return {blk: blkData, u32: u32Data} } const Sha256 = (msg: Buffer) => sha256(msg) export function getHolaParameters_LINKHelper(args: BuildTransactionArgs): {blk: any[]}{ //Fist half = Pass + Envelope type + Common data let firstHalf = Buffer.concat([ getPassAndEnvelopeData(args.network), getCommonTxData(args) ]); let amountBN = new BigNumber(args.amount).times(new BigNumber(10000000)); let amountBI = bigint(amountBN.toFixed()); //Second hallf = Operation data + Signature type let secondHalf = Buffer.concat([ getOperationBytes({type: args.type, destinationPubK: getAccount(args.to).publicKey, amountTx: amountBI}), Buffer.from([0,0,0,0]) ]) let tx = Buffer.concat([firstHalf, secondHalf]) tx = Buffer.from(Sha256(tx), 'hex'); let blkData = []; blkData.push(tx); return {blk: blkData} } function getTransactionData(args: BuildTransactionArgs): Buffer{ let amountBN = new BigNumber(args.amount).times(new BigNumber(10000000)); let amountBI = bigint(amountBN.toFixed()); return Buffer.concat([ getCommonTxData(args), //Add common data getOperationBytes({type: args.type, destinationPubK: getAccount(args.to).publicKey, amountTx: amountBI}), //Add operation constants.SIGNATURE_TYPE //Add signatureTYpe ]); } function getCommonTxData(args: BuildTransactionArgs): Buffer{ let version = constants.VERSION; let sourceAccount = getAccount(args.from); let feeBN = new BigNumber(args.fee).times(new BigNumber(10000000)); let feeBI = bigint(feeBN.toFixed()); //Get fee bytes let feeBytes = getBytesInt(feeBI); //Get sequence number bytes let sequenceNumberBytes = getBytesLong(bigint(args.sequenceNumber)) return Buffer.concat([ version, //Add version sourceAccount.publicKey, //Add source account public key feeBytes, //Add fee sequenceNumberBytes, //Add sequenceNumber constants.TIMEBOUNDS, //Add timebounds getMemoBytes(args.memo), //Add memo constants.OPERATION_COUNT, //Add operationCount = 1 constants.SOURCE_ACCOUNT //Add sourceAccount (null) ]); } function getPassAndEnvelopeData(net: Network): Buffer{ //Get SHA256 of network pass let pass = net == Network.Mainnet ? hexToBytes(constants.MAINNET_NET_PASS) : hexToBytes(constants.TESTNET_NET_PASS); return Buffer.concat([ pass, constants.ENVELOPE_ID ]) } function getSignatureData(sourceAccount: Account, signature: Buffer):Buffer{ if(signature == null){ //Add signature count = 0 return constants.NO_SIGNATURE; }else{ let signatureLength = getBytesInt(signature.length); return Buffer.concat([ constants.SIGNATURE_COUNT, //Add signature count = 1 sourceAccount.signatureHint, //Add signer hint signatureLength, //Add signature length signature //Add signature ]) } }; export function getBytes(data: bigint | number): Buffer{ let hexS = data.toString(16); if(hexS.length % 2 != 0) {hexS = '0' + hexS }; let bytes = Buffer.alloc(hexS.length /2); let cont = 0; for(var i = 0; i< hexS.length; i+=2){ var byte = parseInt(hexS.substring(i, i+2), 16); bytes[cont] = byte; cont++; } return bytes; } function getBytesInt(data: bigint | number) : Buffer{ let buff = getBytes(data); while(buff.length < 4){buff = Buffer.concat([Buffer.alloc(1), buff])} return buff; } export function getBytesLong(data: bigint | number) : Buffer{ let buff = getBytes(data); while(buff.length < 8){buff = Buffer.concat([Buffer.alloc(1), buff])} return buff; } export function hexToBytes(hex: string) : Buffer{ for (var bytes = [], c = 0; c < hex.length; c += 2) bytes.push(parseInt(hex.substr(c, 2), 16)); return Buffer.from(bytes); }