Skip to content

Commit

Permalink
refactor!: simplify buildTxHash helper
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Jun 24, 2021
1 parent df048b3 commit 9e1fde7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 38 deletions.
9 changes: 3 additions & 6 deletions src/account/base.js
Expand Up @@ -24,8 +24,8 @@

import stampit from '@stamp/it'
import { required } from '@stamp/required'
import { messageToHash, decodeBase64Check, assertedType, verifyMessage as verifyMessageCrypto } from '../utils/crypto'
import { buildTx, buildTxHash } from '../tx/builder'
import { messageToHash, decodeBase64Check, assertedType, verifyMessage as verifyMessageCrypto, hash } from '../utils/crypto'
import { buildTx } from '../tx/builder'
import { decode } from '../tx/builder/helpers'
import { TX_TYPE } from '../tx/builder/schema'
import { getNetworkId } from '../node'
Expand All @@ -50,10 +50,7 @@ export const isAccountBase = (acc) => !['sign', 'address'].find(f => typeof acc[
async function signTransaction (tx, opt) {
const networkId = this.getNetworkId(opt)
const rlpBinaryTx = decodeBase64Check(assertedType(tx, 'tx'))
const txWithNetworkId = Buffer.concat([
Buffer.from(networkId),
buildTxHash(rlpBinaryTx, { raw: true })
])
const txWithNetworkId = Buffer.concat([Buffer.from(networkId), hash(rlpBinaryTx)])

const signatures = [await this.sign(txWithNetworkId, opt)]
return buildTx({ encodedTx: rlpBinaryTx, signatures }, TX_TYPE.signed).tx
Expand Down
14 changes: 0 additions & 14 deletions src/tx/builder/helpers.js
Expand Up @@ -47,20 +47,6 @@ export function buildContractId (ownerId, nonce) {
return encode(b2bHash, 'ct')
}

/**
* Build hash
* @function
* @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
* @param {String} prefix Transaction hash prefix
* @param {Buffer} data Rlp encoded transaction buffer
* @param {Object} options
* @param {Boolean} options.raw
* @return {String} Transaction hash
*/
export function buildHash (prefix, data, options = {}) {
return options.raw ? hash(data) : encode(hash(data), prefix)
}

/**
* Build a oracle query id
* @function
Expand Down
13 changes: 5 additions & 8 deletions src/tx/builder/index.js
@@ -1,7 +1,7 @@
import BigNumber from 'bignumber.js'
import { decode as rlpDecode, encode as rlpEncode } from 'rlp'
import { AE_AMOUNT_FORMATS, formatAmount } from '../../utils/amount-formatter'
import { assertedType } from '../../utils/crypto'
import { assertedType, hash } from '../../utils/crypto'

import {
DEFAULT_FEE,
Expand All @@ -24,8 +24,7 @@ import {
writeInt,
buildPointers,
encode,
decode,
buildHash
decode
} from './helpers'
import { toBytes } from '../../utils/bytes'
import MPTree from '../../utils/mptree'
Expand Down Expand Up @@ -411,13 +410,11 @@ export function unpackTx (encodedTx, fromRlpBinary = false, prefix = 'tx') {
* @function
* @alias module:@aeternity/aepp-sdk/es/tx/builder
* @param {String | Buffer} rawTx base64 or rlp encoded transaction
* @param {Object} options
* @param {Boolean} options.raw
* @return {String} Transaction hash
*/
export function buildTxHash (rawTx, options) {
if (typeof rawTx === 'string' && rawTx.indexOf('tx_') !== -1) return buildHash('th', unpackTx(rawTx).rlpEncoded, options)
return buildHash('th', rawTx, options)
export function buildTxHash (rawTx) {
const data = typeof rawTx === 'string' && rawTx.startsWith('tx_') ? decode(rawTx, 'tx') : rawTx
return encode(hash(data), 'th')
}

export default { calculateMinFee, calculateFee, unpackTx, unpackRawTx, buildTx, buildRawTx, validateParams, buildTxHash }
7 changes: 4 additions & 3 deletions src/tx/validator.js
@@ -1,7 +1,8 @@
import {
verify,
decodeBase58Check,
assertedType
assertedType,
hash
} from '../utils/crypto'
import { encode } from '../tx/builder/helpers'

Expand All @@ -17,7 +18,7 @@ import {
SIGNATURE_VERIFICATION_SCHEMA,
TX_TYPE
} from './builder/schema'
import { buildTxHash, calculateFee, unpackTx } from './builder'
import { calculateFee, unpackTx } from './builder'
import NodePool from '../node-pool'

/**
Expand All @@ -31,7 +32,7 @@ const VALIDATORS = {
// VALIDATE SIGNATURE
signature ({ rlpEncoded, signature, ownerPublicKey, networkId = 'ae_mainnet' }) {
const txWithNetworkId = Buffer.concat([Buffer.from(networkId), rlpEncoded])
const txHashWithNetworkId = Buffer.concat([Buffer.from(networkId), buildTxHash(rlpEncoded, { raw: true })])
const txHashWithNetworkId = Buffer.concat([Buffer.from(networkId), hash(rlpEncoded)])
const decodedPub = decodeBase58Check(assertedType(ownerPublicKey, 'ak'))
return verify(txWithNetworkId, signature, decodedPub) || verify(txHashWithNetworkId, signature, decodedPub)
},
Expand Down
11 changes: 4 additions & 7 deletions test/integration/rpc.js
Expand Up @@ -17,12 +17,12 @@

import { before, describe, it } from 'mocha'
import { MemoryAccount, Node, RpcAepp, RpcWallet } from '../../src'
import { buildTxHash, unpackTx } from '../../src/tx/builder'
import { unpackTx } from '../../src/tx/builder'
import { decode } from '../../src/tx/builder/helpers'
import BrowserWindowMessageConnection from '../../src/utils/aepp-wallet-communication/connection/browser-window-message'
import { getBrowserAPI, getHandler } from '../../src/utils/aepp-wallet-communication/helpers'
import { METHODS, RPC_STATUS } from '../../src/utils/aepp-wallet-communication/schema'
import { generateKeyPair, verify } from '../../src/utils/crypto'
import { generateKeyPair, verify, hash } from '../../src/utils/crypto'
import { compilerUrl, genesisAccount, internalUrl, networkId, publicKey, url, ignoreVersion } from './'

describe('Aepp<->Wallet', function () {
Expand Down Expand Up @@ -273,11 +273,8 @@ describe('Aepp<->Wallet', function () {
})

const signedTx = await aepp.signTransaction(tx)
const { tx: { signatures: [signature] } } = unpackTx(signedTx)
const txWithNetwork = Buffer.concat([
Buffer.from(networkId),
buildTxHash(tx, { raw: true })
])
const { tx: { signatures: [signature], encodedTx: { rlpEncoded } } } = unpackTx(signedTx)
const txWithNetwork = Buffer.concat([Buffer.from(networkId), hash(rlpEncoded)])
const valid = verify(txWithNetwork, signature, decode(address))
valid.should.be.equal(true)
})
Expand Down

0 comments on commit 9e1fde7

Please sign in to comment.