Skip to content

Commit

Permalink
Revert "fix: validate addresses during tx broadcast (#5440)"
Browse files Browse the repository at this point in the history
This reverts commit 3c27504.
  • Loading branch information
woodenfurniture committed Oct 12, 2023
1 parent 9e6c7ac commit fdf154e
Show file tree
Hide file tree
Showing 40 changed files with 189 additions and 596 deletions.
6 changes: 2 additions & 4 deletions packages/chain-adapters/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import type { BIP44Params, UtxoAccountType } from '@shapeshiftoss/types'

import type {
Account,
BroadcastTransactionInput,
BuildSendTxInput,
FeeDataEstimate,
GetAddressInput,
GetBIP44ParamsInput,
GetFeeDataInput,
SignAndBroadcastTransactionInput,
SignTx,
SignTxInput,
SubscribeError,
Expand Down Expand Up @@ -67,11 +65,11 @@ export type ChainAdapter<T extends ChainId> = {

signTransaction(signTxInput: SignTxInput<SignTx<T>>): Promise<string>

signAndBroadcastTransaction?(input: SignAndBroadcastTransactionInput<T>): Promise<string>
signAndBroadcastTransaction?(signTxInput: SignTxInput<SignTx<T>>): Promise<string>

getFeeData(input: Partial<GetFeeDataInput<T>>): Promise<FeeDataEstimate<T>>

broadcastTransaction(input: BroadcastTransactionInput): Promise<string>
broadcastTransaction(hex: string): Promise<string>

validateAddress(address: string): Promise<ValidAddressResult>

Expand Down
16 changes: 2 additions & 14 deletions packages/chain-adapters/src/cosmossdk/CosmosSdkBaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ import type { ChainAdapter as IChainAdapter } from '../api'
import { ErrorHandler } from '../error/ErrorHandler'
import type {
Account,
BroadcastTransactionInput,
BuildSendApiTxInput,
BuildSendTxInput,
FeeDataEstimate,
GetAddressInput,
GetBIP44ParamsInput,
GetFeeDataInput,
SignAndBroadcastTransactionInput,
SignTx,
SignTxInput,
SubscribeError,
Expand All @@ -37,7 +35,6 @@ import type {
import { ValidAddressResultType } from '../types'
import { toAddressNList, toRootDerivationPath } from '../utils'
import { bnOrZero } from '../utils/bignumber'
import { validateAddress } from '../utils/validateAddress'
import type { cosmos, thorchain } from './'
import type {
BuildTransactionInput,
Expand Down Expand Up @@ -155,7 +152,7 @@ export abstract class CosmosSdkBaseAdapter<T extends CosmosSdkChainId> implement
abstract getAddress(input: GetAddressInput): Promise<string>
abstract getFeeData(input: Partial<GetFeeDataInput<T>>): Promise<FeeDataEstimate<T>>
abstract signTransaction(signTxInput: SignTxInput<SignTx<T>>): Promise<string>
abstract signAndBroadcastTransaction(input: SignAndBroadcastTransactionInput<T>): Promise<string>
abstract signAndBroadcastTransaction(signTxInput: SignTxInput<SignTx<T>>): Promise<string>

getChainId(): ChainId {
return this.chainId
Expand Down Expand Up @@ -332,16 +329,7 @@ export abstract class CosmosSdkBaseAdapter<T extends CosmosSdkChainId> implement
return { txToSign }
}

async broadcastTransaction({
senderAddress,
receiverAddress,
hex,
}: BroadcastTransactionInput): Promise<string> {
await Promise.all([
validateAddress(senderAddress),
receiverAddress !== undefined && validateAddress(receiverAddress),
])

broadcastTransaction(hex: string): Promise<string> {
try {
return this.providers.http.sendTx({ body: { rawTx: hex } })
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ import type {
FeeDataEstimate,
GetAddressInput,
GetFeeDataInput,
SignAndBroadcastTransactionInput,
SignTxInput,
} from '../../types'
import { ChainAdapterDisplayName } from '../../types'
import { bn, calcFee, toAddressNList } from '../../utils'
import { validateAddress } from '../../utils/validateAddress'
import type { ChainAdapterArgs } from '../CosmosSdkBaseAdapter'
import { assertIsValidatorAddress, CosmosSdkBaseAdapter } from '../CosmosSdkBaseAdapter'
import type { Message, ValidatorAction } from '../types'
Expand Down Expand Up @@ -282,16 +280,7 @@ export class ChainAdapter extends CosmosSdkBaseAdapter<KnownChainIds.CosmosMainn
}
}

async signAndBroadcastTransaction({
senderAddress,
receiverAddress,
signTxInput,
}: SignAndBroadcastTransactionInput<KnownChainIds.CosmosMainnet>): Promise<string> {
await Promise.all([
validateAddress(senderAddress),
receiverAddress !== undefined && validateAddress(receiverAddress),
])

async signAndBroadcastTransaction(signTxInput: SignTxInput<CosmosSignTx>): Promise<string> {
const { wallet } = signTxInput
try {
if (supportsCosmos(wallet)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ import type {
FeeDataEstimate,
GetAddressInput,
GetFeeDataInput,
SignAndBroadcastTransactionInput,
SignTxInput,
} from '../../types'
import { ChainAdapterDisplayName } from '../../types'
import { toAddressNList } from '../../utils'
import { bnOrZero } from '../../utils/bignumber'
import { validateAddress } from '../../utils/validateAddress'
import type { ChainAdapterArgs } from '../CosmosSdkBaseAdapter'
import { CosmosSdkBaseAdapter } from '../CosmosSdkBaseAdapter'
import type { Message } from '../types'
Expand Down Expand Up @@ -206,18 +204,8 @@ export class ChainAdapter extends CosmosSdkBaseAdapter<KnownChainIds.ThorchainMa
}
}

async signAndBroadcastTransaction({
senderAddress,
receiverAddress,
signTxInput,
}: SignAndBroadcastTransactionInput<KnownChainIds.ThorchainMainnet>): Promise<string> {
await Promise.all([
validateAddress(senderAddress),
receiverAddress !== undefined && validateAddress(receiverAddress),
])

async signAndBroadcastTransaction(signTxInput: SignTxInput<ThorchainSignTx>): Promise<string> {
const { wallet } = signTxInput

try {
if (supportsThorchain(wallet)) {
const signedTx = await this.signTransaction(signTxInput)
Expand Down
24 changes: 2 additions & 22 deletions packages/chain-adapters/src/evm/EvmBaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ import type { ChainAdapter as IChainAdapter } from '../api'
import { ErrorHandler } from '../error/ErrorHandler'
import type {
Account,
BroadcastTransactionInput,
BuildSendApiTxInput,
BuildSendTxInput,
FeeDataEstimate,
GetAddressInput,
GetBIP44ParamsInput,
GetFeeDataInput,
SignAndBroadcastTransactionInput,
SignMessageInput,
SignTx,
SignTxInput,
Expand All @@ -49,7 +47,6 @@ import type {
import { ValidAddressResultType } from '../types'
import { getAssetNamespace, toAddressNList, toRootDerivationPath } from '../utils'
import { bnOrZero } from '../utils/bignumber'
import { validateAddress } from '../utils/validateAddress'
import type { arbitrum, avalanche, bnbsmartchain, ethereum, gnosis, optimism, polygon } from '.'
import type {
BuildCustomApiTxInput,
Expand Down Expand Up @@ -445,16 +442,7 @@ export abstract class EvmBaseAdapter<T extends EvmChainId> implements IChainAdap
}
}

async signAndBroadcastTransaction({
senderAddress,
receiverAddress,
signTxInput,
}: SignAndBroadcastTransactionInput<T>): Promise<string> {
await Promise.all([
validateAddress(senderAddress),
receiverAddress !== undefined && validateAddress(receiverAddress),
])

async signAndBroadcastTransaction(signTxInput: SignTxInput<ETHSignTx>): Promise<string> {
try {
const { txToSign, wallet } = signTxInput

Expand All @@ -473,15 +461,7 @@ export abstract class EvmBaseAdapter<T extends EvmChainId> implements IChainAdap
}
}

async broadcastTransaction({
senderAddress,
receiverAddress,
hex,
}: BroadcastTransactionInput): Promise<string> {
await Promise.all([
validateAddress(senderAddress),
receiverAddress !== undefined && validateAddress(receiverAddress),
])
broadcastTransaction(hex: string): Promise<string> {
return this.providers.http.sendTx({ sendTxBody: { hex } })
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ import { bn } from '../../utils/bignumber'
import type { ChainAdapterArgs, EvmChainId } from '../EvmBaseAdapter'
import * as arbitrum from './ArbitrumChainAdapter'

jest.mock('../../utils/validateAddress', () => ({
validateAddress: jest.fn(),
}))

const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
const EOA_ADDRESS = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'

Expand Down Expand Up @@ -322,15 +318,11 @@ describe('ArbitrumChainAdapter', () => {

wallet.ethSendTx = async () => await Promise.resolve(null)

const signTxInput = { wallet, txToSign: {} } as unknown as SignTxInput<ETHSignTx>
const tx = { wallet, txToSign: {} } as unknown as SignTxInput<ETHSignTx>

await expect(
adapter.signAndBroadcastTransaction({
senderAddress: '0x1234',
receiverAddress: '0x1234',
signTxInput,
}),
).rejects.toThrow(/Error signing & broadcasting tx/)
await expect(adapter.signAndBroadcastTransaction(tx)).rejects.toThrow(
/Error signing & broadcasting tx/,
)
})

it('should return the hash returned by wallet.ethSendTx', async () => {
Expand All @@ -342,15 +334,11 @@ describe('ArbitrumChainAdapter', () => {
hash: '0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331',
})

const signTxInput = { wallet, txToSign: {} } as unknown as SignTxInput<ETHSignTx>
const tx = { wallet, txToSign: {} } as unknown as SignTxInput<ETHSignTx>

await expect(
adapter.signAndBroadcastTransaction({
senderAddress: '0x1234',
receiverAddress: '0x1234',
signTxInput,
}),
).resolves.toEqual('0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331')
await expect(adapter.signAndBroadcastTransaction(tx)).resolves.toEqual(
'0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331',
)
})
})

Expand Down Expand Up @@ -404,11 +392,7 @@ describe('ArbitrumChainAdapter', () => {
const adapter = new arbitrum.ChainAdapter(args)

const mockTx = '0x123'
const result = await adapter.broadcastTransaction({
senderAddress: '0x1234',
receiverAddress: '0x1234',
hex: mockTx,
})
const result = await adapter.broadcastTransaction(mockTx)

expect(args.providers.http.sendTx).toHaveBeenCalledWith<any>({ sendTxBody: { hex: mockTx } })
expect(result).toEqual(expectedResult)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ import { bn } from '../../utils/bignumber'
import type { ChainAdapterArgs, EvmChainId } from '../EvmBaseAdapter'
import * as avalanche from './AvalancheChainAdapter'

jest.mock('../../utils/validateAddress', () => ({
validateAddress: jest.fn(),
}))

const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
const EOA_ADDRESS = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'

Expand Down Expand Up @@ -327,15 +323,11 @@ describe('AvalancheChainAdapter', () => {

wallet.ethSendTx = async () => await Promise.resolve(null)

const signTxInput = { wallet, txToSign: {} } as unknown as SignTxInput<ETHSignTx>
const tx = { wallet, txToSign: {} } as unknown as SignTxInput<ETHSignTx>

await expect(
adapter.signAndBroadcastTransaction({
senderAddress: '0x1234',
receiverAddress: '0x1234',
signTxInput,
}),
).rejects.toThrow(/Error signing & broadcasting tx/)
await expect(adapter.signAndBroadcastTransaction(tx)).rejects.toThrow(
/Error signing & broadcasting tx/,
)
})

it('should return the hash returned by wallet.ethSendTx', async () => {
Expand All @@ -347,15 +339,11 @@ describe('AvalancheChainAdapter', () => {
hash: '0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331',
})

const signTxInput = { wallet, txToSign: {} } as unknown as SignTxInput<ETHSignTx>
const tx = { wallet, txToSign: {} } as unknown as SignTxInput<ETHSignTx>

await expect(
adapter.signAndBroadcastTransaction({
senderAddress: '0x1234',
receiverAddress: '0x1234',
signTxInput,
}),
).resolves.toEqual('0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331')
await expect(adapter.signAndBroadcastTransaction(tx)).resolves.toEqual(
'0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331',
)
})
})

Expand Down Expand Up @@ -409,11 +397,7 @@ describe('AvalancheChainAdapter', () => {
const adapter = new avalanche.ChainAdapter(args)

const mockTx = '0x123'
const result = await adapter.broadcastTransaction({
senderAddress: '0x1234',
receiverAddress: '0x1234',
hex: mockTx,
})
const result = await adapter.broadcastTransaction(mockTx)

expect(args.providers.http.sendTx).toHaveBeenCalledWith<any>({ sendTxBody: { hex: mockTx } })
expect(result).toEqual(expectedResult)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ import { bn } from '../../utils/bignumber'
import type { ChainAdapterArgs, EvmChainId } from '../EvmBaseAdapter'
import * as bsc from './BscChainAdapter'

jest.mock('../../utils/validateAddress', () => ({
validateAddress: jest.fn(),
}))

const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
const EOA_ADDRESS = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'

Expand Down Expand Up @@ -304,15 +300,11 @@ describe('BscChainAdapter', () => {

wallet.ethSendTx = async () => await Promise.resolve(null)

const signTxInput = { wallet, txToSign: {} } as unknown as SignTxInput<ETHSignTx>
const tx = { wallet, txToSign: {} } as unknown as SignTxInput<ETHSignTx>

await expect(
adapter.signAndBroadcastTransaction({
senderAddress: '0x1234',
receiverAddress: '0x1234',
signTxInput,
}),
).rejects.toThrow(/Error signing & broadcasting tx/)
await expect(adapter.signAndBroadcastTransaction(tx)).rejects.toThrow(
/Error signing & broadcasting tx/,
)
})

it('should return the hash returned by wallet.ethSendTx', async () => {
Expand All @@ -324,15 +316,11 @@ describe('BscChainAdapter', () => {
hash: '0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331',
})

const signTxInput = { wallet, txToSign: {} } as unknown as SignTxInput<ETHSignTx>
const tx = { wallet, txToSign: {} } as unknown as SignTxInput<ETHSignTx>

await expect(
adapter.signAndBroadcastTransaction({
senderAddress: '0x1234',
receiverAddress: '0x1234',
signTxInput,
}),
).resolves.toEqual('0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331')
await expect(adapter.signAndBroadcastTransaction(tx)).resolves.toEqual(
'0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331',
)
})
})

Expand Down Expand Up @@ -386,11 +374,7 @@ describe('BscChainAdapter', () => {
const adapter = new bsc.ChainAdapter(args)

const mockTx = '0x123'
const result = await adapter.broadcastTransaction({
senderAddress: '0x1234',
receiverAddress: '0x1234',
hex: mockTx,
})
const result = await adapter.broadcastTransaction(mockTx)

expect(args.providers.http.sendTx).toHaveBeenCalledWith<any>({ sendTxBody: { hex: mockTx } })
expect(result).toEqual(expectedResult)
Expand Down
Loading

0 comments on commit fdf154e

Please sign in to comment.