Skip to content

Commit

Permalink
feat: eip712 verification against chain
Browse files Browse the repository at this point in the history
  • Loading branch information
bangjelkoski committed Sep 30, 2024
1 parent 189cea3 commit 4fd66f7
Show file tree
Hide file tree
Showing 46 changed files with 291 additions and 145 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
"bootstrap": "yarn clean-up && lerna bootstrap",
"test": "jest",
"test:sdk-ts:core:stargate": "jest ./packages/sdk-ts/src/core/stargate",
"test:sdk-ts:client": "jest ./packages/sdk-ts/src/client",
"test:sdk-ts:core:modules": "jest ./packages/sdk-ts/src/core/modules",
"test:sdk-ts:core": "jest ./packages/sdk-ts/src/core",
"test:sdk-ts:client": "jest ./packages/sdk-ts/src/client",
"test:bridge-ts": "jest ./packages/bridge-ts/src",
"test:sdk-ui-ts": "jest ./packages/sdk-ui-ts/src",
"test:token-metadata": "jest ./packages/token-metadata/test",
Expand Down
1 change: 1 addition & 0 deletions packages/exceptions/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export enum ErrorType {
GrpcUnaryRequest = 'grpc-unary-request',
HttpRequest = 'http-request',
Web3 = 'web3',
Web3Gateway = 'web3-gateway',
}

export interface ErrorContext {
Expand Down
1 change: 1 addition & 0 deletions packages/exceptions/src/types/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export enum IndexerErrorModule {
ChronosSpot = 'indexer-chronos-spot',
ChronosMarkets = 'indexer-chronos-markets',
Campaign = 'indexer-campaign',
Web3Gw = 'web3-gateway'
}

export enum WalletErrorActionModule {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@injectivelabs/grpc-web": "^0.0.1",
"@injectivelabs/grpc-web-node-http-transport": "^0.0.2",
"@injectivelabs/grpc-web-react-native-transport": "^0.0.2",
"@injectivelabs/indexer-proto-ts": "1.11.55",
"@injectivelabs/indexer-proto-ts": "1.11.56",
"@injectivelabs/mito-proto-ts": "1.0.65",
"@injectivelabs/networks": "^1.14.15-beta.0",
"@injectivelabs/test-utils": "^1.14.14",
Expand Down
141 changes: 39 additions & 102 deletions packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcTransactionApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,33 @@ import {
import { recoverTypedSignaturePubKey } from '../../../utils/transaction'
import { IndexerModule } from '../types'
import {
ErrorType,
GrpcUnaryRequestException,
TransactionException,
UnspecifiedErrorCode,
} from '@injectivelabs/exceptions'
import { getGrpcIndexerWebImpl } from '../../base/BaseIndexerGrpcWebConsumer'
import { InjectiveExchangeRpc } from '@injectivelabs/indexer-proto-ts'
import {
CosmosBaseV1Beta1Coin,
CosmosTxV1Beta1Tx,
CosmosBaseV1Beta1Coin,
} from '@injectivelabs/core-proto-ts'

interface PrepareTxArgs {
address: AccountAddress
chainId: EthereumChainId
message: any
estimateGas?: boolean
gasLimit?: number
memo?: string | number
timeoutHeight?: number
feeDenom?: string
feePrice?: string
}

/**
* @category Indexer Grpc API
* @deprecated use IndexerGrpcWeb3GwApi
*/
export class IndexerGrpcTransactionApi {
protected module: string = IndexerModule.Transaction
Expand All @@ -33,27 +47,18 @@ export class IndexerGrpcTransactionApi {
)
}

async prepareTxRequest({
address,
chainId,
message,
memo,
estimateGas = true,
gasLimit = DEFAULT_GAS_LIMIT,
feeDenom = DEFAULT_BRIDGE_FEE_DENOM,
feePrice = DEFAULT_BRIDGE_FEE_PRICE,
timeoutHeight,
}: {
address: AccountAddress
chainId: EthereumChainId
message: any
estimateGas?: boolean
gasLimit?: number
memo?: string | number
timeoutHeight?: number
feeDenom?: string
feePrice?: string
}) {
async prepareTxRequest(args: PrepareTxArgs) {
const {
address,
chainId,
message,
memo,
estimateGas = true,
gasLimit = DEFAULT_GAS_LIMIT,
feeDenom = DEFAULT_BRIDGE_FEE_DENOM,
feePrice = DEFAULT_BRIDGE_FEE_PRICE,
timeoutHeight,
} = args
const txFeeAmount = CosmosBaseV1Beta1Coin.Coin.create()
txFeeAmount.denom = feeDenom
txFeeAmount.amount = feePrice
Expand Down Expand Up @@ -103,10 +108,18 @@ export class IndexerGrpcTransactionApi {
code: UnspecifiedErrorCode,
context: 'PrepareTx',
contextModule: 'Web3Gateway',
type: ErrorType.Web3Gateway,
})
}
}

async prepareExchangeTxRequest(args: PrepareTxArgs) {
return this.prepareTxRequest({
...args,
gasLimit: args.gasLimit || DEFAULT_EXCHANGE_LIMIT,
})
}

async prepareCosmosTxRequest({
memo,
address,
Expand Down Expand Up @@ -175,86 +188,7 @@ export class IndexerGrpcTransactionApi {
code: UnspecifiedErrorCode,
context: 'CosmosPrepareTx',
contextModule: 'Web3Gateway',
})
}
}

async prepareExchangeTxRequest({
address,
chainId,
message,
memo,
estimateGas = true,
gasLimit = DEFAULT_EXCHANGE_LIMIT,
feeDenom = DEFAULT_BRIDGE_FEE_DENOM,
feePrice = DEFAULT_BRIDGE_FEE_PRICE,
timeoutHeight,
delegatedFee,
}: {
address: AccountAddress
chainId: EthereumChainId
message: any
estimateGas?: boolean
gasLimit?: number
memo?: string | number
feeDenom?: string
feePrice?: string
timeoutHeight?: number
delegatedFee?: boolean
}) {
const txFeeAmount = CosmosBaseV1Beta1Coin.Coin.create()
txFeeAmount.denom = feeDenom
txFeeAmount.amount = feePrice

const cosmosTxFee = InjectiveExchangeRpc.CosmosTxFee.create()
cosmosTxFee.price = [txFeeAmount]

if (delegatedFee !== undefined) {
cosmosTxFee.delegateFee = delegatedFee
}

if (!estimateGas) {
cosmosTxFee.gas = gasLimit.toString()
}

const prepareTxRequest = InjectiveExchangeRpc.PrepareTxRequest.create()
prepareTxRequest.chainId = chainId.toString()
prepareTxRequest.signerAddress = address
prepareTxRequest.fee = cosmosTxFee

const arrayOfMessages = Array.isArray(message) ? message : [message]
const messagesList = arrayOfMessages.map((message) =>
Buffer.from(JSON.stringify(message), 'utf8'),
)

prepareTxRequest.msgs = messagesList

if (timeoutHeight !== undefined) {
prepareTxRequest.timeoutHeight = timeoutHeight.toString()
}

if (memo) {
prepareTxRequest.memo = typeof memo === 'number' ? memo.toString() : memo
}

try {
const response = await this.client.PrepareTx(prepareTxRequest)

return response
} catch (e: unknown) {
if (e instanceof InjectiveExchangeRpc.GrpcWebError) {
throw new TransactionException(new Error(e.toString()), {
code: e.code,
type: e.type,
context: 'PrepareTx',
contextModule: 'Web3Gateway',
})
}

throw new TransactionException(e as Error, {
code: UnspecifiedErrorCode,
context: 'PrepareTx',
contextModule: 'Web3Gateway',
type: ErrorType.Web3Gateway,
})
}
}
Expand Down Expand Up @@ -325,6 +259,7 @@ export class IndexerGrpcTransactionApi {
code: UnspecifiedErrorCode,
context: 'BroadcastTx',
contextModule: 'Web3Gateway',
type: ErrorType.Web3Gateway,
})
}
}
Expand Down Expand Up @@ -382,6 +317,7 @@ export class IndexerGrpcTransactionApi {
code: UnspecifiedErrorCode,
context: 'BroadcastTx',
contextModule: 'Web3Gateway',
type: ErrorType.Web3Gateway,
})
}
}
Expand All @@ -407,6 +343,7 @@ export class IndexerGrpcTransactionApi {
code: UnspecifiedErrorCode,
context: 'FeePayer',
contextModule: 'Web3Gateway',
type: ErrorType.Web3Gateway,
})
}
}
Expand Down
119 changes: 119 additions & 0 deletions packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcWeb3GwApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { AccountAddress, EthereumChainId } from '@injectivelabs/ts-types'
import {
DEFAULT_GAS_LIMIT,
DEFAULT_BRIDGE_FEE_DENOM,
DEFAULT_BRIDGE_FEE_PRICE,
} from '@injectivelabs/utils'
import { IndexerModule } from '../types'
import {
ErrorType,
TransactionException,
UnspecifiedErrorCode,
} from '@injectivelabs/exceptions'
import { InjectiveExchangeRpc } from '@injectivelabs/indexer-proto-ts'
import { CosmosBaseV1Beta1Coin } from '@injectivelabs/core-proto-ts'
import { IndexerGrpcTransactionApi } from './IndexerGrpcTransactionApi'

/**
* @category Indexer Grpc API
*/
export class IndexerGrpcWeb3GwApi extends IndexerGrpcTransactionApi {
protected module: string = IndexerModule.Web3Gw

constructor(endpoint: string) {
super(endpoint)
}

async prepareEip712Request({
address,
chainId,
message,
memo,
sequence,
accountNumber,
estimateGas = true,
gasLimit = DEFAULT_GAS_LIMIT,
feeDenom = DEFAULT_BRIDGE_FEE_DENOM,
feePrice = DEFAULT_BRIDGE_FEE_PRICE,
timeoutHeight,
eip712Version = 'v1',
}: {
address: AccountAddress
chainId: EthereumChainId
message: any
estimateGas?: boolean
gasLimit?: number
memo?: string | number
timeoutHeight?: number
feeDenom?: string
feePrice?: string
sequence?: number
accountNumber?: number
eip712Version?: string
}) {
const txFeeAmount = CosmosBaseV1Beta1Coin.Coin.create()
txFeeAmount.denom = feeDenom
txFeeAmount.amount = feePrice

const cosmosTxFee = InjectiveExchangeRpc.CosmosTxFee.create()
cosmosTxFee.price = [txFeeAmount]

if (!estimateGas) {
cosmosTxFee.gas = gasLimit.toString()
}

const prepareTxRequest = InjectiveExchangeRpc.PrepareEip712Request.create()
prepareTxRequest.chainId = chainId.toString()
prepareTxRequest.signerAddress = address
prepareTxRequest.fee = cosmosTxFee

const arrayOfMessages = Array.isArray(message) ? message : [message]
const messagesList = arrayOfMessages.map((message) =>
Buffer.from(JSON.stringify(message), 'utf8'),
)

prepareTxRequest.msgs = messagesList

if (timeoutHeight !== undefined) {
prepareTxRequest.timeoutHeight = timeoutHeight.toString()
}

if (memo) {
prepareTxRequest.memo = typeof memo === 'number' ? memo.toString() : memo
}

if (eip712Version) {
prepareTxRequest.eip712Wrapper = eip712Version
}

if (accountNumber) {
prepareTxRequest.accountNumber = accountNumber.toString()
}

if (sequence) {
prepareTxRequest.sequence = sequence.toString()
}

try {
const response = await this.client.PrepareEip712(prepareTxRequest)

return response
} catch (e: unknown) {
if (e instanceof InjectiveExchangeRpc.GrpcWebError) {
throw new TransactionException(new Error(e.toString()), {
code: e.code,
context: 'PrepareEip712',
contextModule: 'Web3Gateway',
type: e.type,
})
}

throw new TransactionException(e as Error, {
code: UnspecifiedErrorCode,
context: 'PrepareEip712',
contextModule: 'Web3Gateway',
type: ErrorType.Web3Gateway,
})
}
}
}
1 change: 1 addition & 0 deletions packages/sdk-ts/src/client/indexer/grpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export { IndexerGrpcCampaignApi } from './IndexerGrpcCampaignApi'
export { IndexerGrpcArchiverApi } from './IndexerGrpcArchiverApi'
export { IndexerGrpcDerivativesApi } from './IndexerGrpcDerivativesApi'
export { IndexerGrpcTransactionApi } from './IndexerGrpcTransactionApi'
export { IndexerGrpcWeb3GwApi } from './IndexerGrpcWeb3GwApi'
export { IndexerGrpcAccountPortfolioApi } from './IndexerGrpcPortfolioApi'
export { IndexerGrpcInsuranceFundApi } from './IndexerGrpcInsuranceFundApi'
4 changes: 2 additions & 2 deletions packages/sdk-ts/src/core/accounts/PrivateKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
CosmosTxV1Beta1Tx,
InjectiveTypesV1Beta1TxExt,
} from '@injectivelabs/core-proto-ts'
import { getTransactionPartsFromTxRaw } from '../modules/tx/utils/tx'
import { getEip712TypedData, MsgDecoder } from '../modules/tx/eip712'
import { getTransactionPartsFromTxRaw } from '../tx/utils/tx'
import { getEip712TypedData, MsgDecoder } from '../tx/eip712'
import { GeneralException } from '@injectivelabs/exceptions'
import { ChainId, EthereumChainId } from '@injectivelabs/ts-types'

Expand Down
1 change: 1 addition & 0 deletions packages/sdk-ts/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './modules'
export * from './tx'
export * from './accounts'
export * from './utils'
4 changes: 2 additions & 2 deletions packages/sdk-ts/src/core/modules/MsgBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { SnakeCaseKeys } from 'snakecase-keys'
import {
mapValuesToProperValueType,
objectKeysToEip712Types,
} from './tx/eip712/maps'
import { TypedDataField } from './tx/eip712/types'
} from '../tx/eip712/maps'
import { TypedDataField } from '../tx/eip712/types'
import { prepareSignBytes } from './utils'

/**
Expand Down
Loading

0 comments on commit 4fd66f7

Please sign in to comment.