From 51d638c1b5891e4698d2b8f2b0e8cc5e94d84080 Mon Sep 17 00:00:00 2001 From: feruz Date: Wed, 11 Aug 2021 10:50:45 +0300 Subject: [PATCH] local broadcast tx generation --- package.json | 2 +- src/providers/hive/dhive.js | 54 +++++++++++++++++++++++++++++-------- yarn.lock | 5 ++-- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 93af07c66..a7ef5a6f9 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "@esteemapp/react-native-multi-slider": "^1.1.0", "@esteemapp/react-native-render-html": "^4.1.5", "@esteemapp/react-native-slider": "^0.12.0", - "@hiveio/dhive": "ecency/dhive-test", + "@hiveio/dhive": "^1.0.1", "@react-native-community/async-storage": "^1.11.0", "@react-native-community/cameraroll": "^1.3.0", "@react-native-community/cli-platform-ios": "^4.10.1", diff --git a/src/providers/hive/dhive.js b/src/providers/hive/dhive.js index 3e9414c3d..a198be53f 100644 --- a/src/providers/hive/dhive.js +++ b/src/providers/hive/dhive.js @@ -2,7 +2,15 @@ // import '../../../shim'; // import * as bitcoin from 'bitcoinjs-lib'; -import { Client, cryptoUtils, utils, Types, Transaction } from '@hiveio/dhive'; +import { + Client, + cryptoUtils, + utils, + Types, + Transaction, + Operation, + TransactionConfirmation, +} from '@hiveio/dhive'; import { PrivateKey } from '@esteemapp/dhive'; import bytebuffer from 'bytebuffer'; import { createHash } from 'react-native-crypto'; @@ -73,6 +81,38 @@ export const generateTrxId = (transaction) => { return sha256(transactionData).toString('hex').slice(0, 40); //CryptoJS.enc.Hex }; +export const sendHiveOperations = async ( + operations: Operation[], + key: PrivateKey | PrivateKey[], +): Promise => { + const { head_block_number, head_block_id, time } = await getDynamicGlobalProperties(); + const ref_block_num = head_block_number & 0xffff; + const ref_block_prefix = Buffer.from(head_block_id, 'hex').readUInt32LE(4); + const expireTime = 60 * 1000; + const chainId = Buffer.from( + 'beeab0de00000000000000000000000000000000000000000000000000000000', + 'hex', + ); + const expiration = new Date(new Date(time + 'Z').getTime() + expireTime) + .toISOString() + .slice(0, -5); + const extensions = []; + + const tx: Transaction = { + expiration: expiration, + extensions: extensions, + operations: operations, + ref_block_num: ref_block_num, + ref_block_prefix: ref_block_prefix, + }; + + const transaction = await cryptoUtils.signTransaction(tx, key, chainId); + const trxId = generateTrxId(transaction); + const resultHive = await client.broadcast.call('broadcast_transaction', [transaction]); + const result = Object.assign({ id: trxId }, resultHive); + return result; +}; + export const getDigitPinCode = (pin) => decryptKey(pin, Config.PIN_KEY); export const getDynamicGlobalProperties = () => client.database.getDynamicGlobalProperties(); @@ -727,17 +767,9 @@ const _vote = (currentAccount, pin, author, permlink, weight) => { ]; return new Promise((resolve, reject) => { - client.broadcast - .sendOperations(args, privateKey) + sendHiveOperations(args, privateKey) .then((result) => { - console.log(result); - console.log(generateTrxId(result.tx)); - Alert.alert( - 'dhive transaction id: ' + - JSON.stringify(result) + - 'localGen:' + - generateTrxId(result.tx), - ); + console.log('vote result', result); resolve(result); }) .catch((err) => { diff --git a/yarn.lock b/yarn.lock index e4cb6a6e9..3b75cb49b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1171,9 +1171,10 @@ dependencies: "@hapi/hoek" "^8.3.0" -"@hiveio/dhive@ecency/dhive-test": +"@hiveio/dhive@^1.0.1": version "1.0.1" - resolved "https://codeload.github.com/ecency/dhive-test/tar.gz/7bcf3d51ed889d47e2a0ed747debc37099c03c6b" + resolved "https://registry.yarnpkg.com/@hiveio/dhive/-/dhive-1.0.1.tgz#76bb9e9dbd4cc9efadafd8a40b7025c997a56aba" + integrity sha512-j6PEAhLwhSWzMjQiPS0Qsy3jZ+6pAirlnPkgkkb+3q7CGG0eNA7w5VYaFv4OFGecOQfESj7MViYhpzC+5qiq0Q== dependencies: bs58 "^4.0.1" bytebuffer "^5.0.1"