import { sha256 } from "js-sha256"; import { SignedTransaction, Transaction } from "./transactions/schema"; const BN = require("bn.js"); import { Signature } from "./transactions/signature"; import { PublicKey } from "@near-js/crypto"; import { Action, Transfer } from "./transactions/actions"; import { BigNumber } from "bignumber.js"; import { log } from "console"; const Sha256 = (msg: Buffer) => Buffer.from(sha256(msg), "hex"); export type BuildTransactionArgs = { from: string; fromPubKey: string; nonce: string; to: string; amount: string; blockHash: Buffer; signature?: Buffer; }; export function buildTransactionHelper(args: BuildTransactionArgs): Buffer { let amount = new BigNumber(args.amount).times( new BigNumber("1000000000000000000000000") ); let amountBN = new BN(amount.toFixed()); let txTemp = new Transaction({ signerId: args.from, publicKey: new PublicKey({ keyType: "u8", data: new Uint8Array(Buffer.from(args.fromPubKey, "hex")), }), nonce: new BN(args.nonce, 10), receiverId: args.to, actions: [new Action({ transfer: new Transfer({ deposit: amountBN }) })], blockHash: new Uint8Array(args.blockHash), }); let tx; if (args.signature != null) { tx = new SignedTransaction({ transaction: txTemp, signature: new Signature({ keyType: "u8", data: new Uint8Array(args.signature), }), }); } else { tx = txTemp; } return tx.encode(); } export function getHolaParameters_LINKHelper(args: BuildTransactionArgs): { blk: any[]; } { let tx = buildTransactionHelper(args); let dataToSign = Sha256(tx); let blk = []; blk.push(dataToSign); return { blk: blk }; }