refactor: remove client.combine (#1632)

* remove combine

* remove tests

* fix tests
This commit is contained in:
Mayukha Vadari
2021-09-21 15:16:34 -04:00
parent d734d886c4
commit 6dba1b142c
4 changed files with 3 additions and 118 deletions

View File

@@ -88,7 +88,6 @@ import getFee from '../sugar/fee'
import getOrderbook from '../sugar/orderbook'
import { submitTransaction, submitSignedTransaction } from '../sugar/submit'
import { ensureClassicAddress } from '../sugar/utils'
import combine from '../transaction/combine'
import generateFaucetWallet from '../wallet/generateFaucetWallet'
import {
@@ -515,8 +514,6 @@ class Client extends EventEmitter {
public getBalances = getBalances
public getOrderbook = getOrderbook
public combine = combine
public generateFaucetWallet = generateFaucetWallet
public errors = errors

View File

@@ -1,82 +0,0 @@
import BigNumber from 'bignumber.js'
import * as _ from 'lodash'
import { decodeAccountID } from 'ripple-address-codec'
import binary from 'ripple-binary-codec'
import { JsonObject } from 'ripple-binary-codec/dist/types/serialized-type'
import { ValidationError } from '../errors'
import { computeSignedTransactionHash } from '../utils/hashes'
/**
* The transactions should all be equal except for the 'Signers' field.
*
* @param transactions
*/
function validateTransactionEquivalence(transactions: JsonObject[]) {
const exampleTransaction = JSON.stringify({
...transactions[0],
Signers: null,
})
if (
transactions
.slice(1)
.some(
(tx) => JSON.stringify({ ...tx, Signers: null }) !== exampleTransaction,
)
) {
throw new ValidationError(
'txJSON is not the same for all signedTransactions',
)
}
}
function addressToBigNumber(address) {
const hex = Buffer.from(decodeAccountID(address)).toString('hex')
return new BigNumber(hex, 16)
}
/**
* If presented in binary form, the Signers array must be sorted based on
* the numeric value of the signer addresses, with the lowest value first.
* (If submitted as JSON, the submit_multisigned method handles this automatically.)
* https://xrpl.org/multi-signing.html.
*
* @param a
* @param b
*/
function compareSigners(a, b) {
return addressToBigNumber(a.Signer.Account).comparedTo(
addressToBigNumber(b.Signer.Account),
)
}
function getTransactionWithAllSigners(transactions: JsonObject[]): JsonObject {
// Signers must be sorted - see compareSigners for more details
const sortedSigners = _.flatMap(transactions, (tx) => tx.Signers)
.filter((signer) => signer)
.sort(compareSigners)
return { ...transactions[0], Signers: sortedSigners }
}
/**
*
* @param signedTransactions - A collection of the same transaction signed by different signers. The only difference
* between the elements of signedTransactions should be the Signers field.
* @returns An object with the combined transaction (now having a sorted list of all signers) which is encoded, along
* with a transaction id based on the combined transaction.
*/
function combine(signedTransactions: string[]): object {
const transactions: JsonObject[] = signedTransactions.map(binary.decode)
validateTransactionEquivalence(transactions)
const signedTransaction = binary.encode(
getTransactionWithAllSigners(transactions),
)
return {
signedTransaction,
id: computeSignedTransactionHash(signedTransaction),
}
}
export default combine

View File

@@ -1,30 +0,0 @@
import { assert } from 'chai'
import binary from 'ripple-binary-codec'
import requests from '../fixtures/requests'
import responses from '../fixtures/responses'
import { setupClient, teardownClient } from '../setupClient'
import { assertResultMatch } from '../testUtils'
const { combine: REQUEST_FIXTURES } = requests
const { combine: RESPONSE_FIXTURES } = responses
describe('client.combine', function () {
beforeEach(setupClient)
afterEach(teardownClient)
it('combine', async function () {
const combined = this.client.combine(REQUEST_FIXTURES.setDomain)
assertResultMatch(combined, RESPONSE_FIXTURES.single, 'sign')
})
it('combine - different transactions', async function () {
const request = [REQUEST_FIXTURES.setDomain[0]]
const tx = binary.decode(REQUEST_FIXTURES.setDomain[0])
tx.Flags = 0
request.push(binary.encode(tx))
assert.throws(() => {
this.client.combine(request)
}, /txJSON is not the same for all signedTransactions/u)
})
})

View File

@@ -66,10 +66,10 @@ describe('integration tests', function () {
const accountSetTx = await client.autofill(accountSet, 2)
const signed1 = sign(signerWallet1, accountSetTx, true)
const signed2 = sign(signerWallet2, accountSetTx, true)
const combined = multisign([signed1, signed2])
const submitResponse = await client.submitSignedTransaction(combined)
const multisignedTx = multisign([signed1, signed2])
const submitResponse = await client.submitSignedTransaction(multisignedTx)
await ledgerAccept(client)
assert.strictEqual(submitResponse.result.engine_result, 'tesSUCCESS')
await verifySubmittedTransaction(this.client, combined)
await verifySubmittedTransaction(this.client, multisignedTx)
})
})