Skip to content

Commit

Permalink
fix: eip712v2 for authz authorizations
Browse files Browse the repository at this point in the history
  • Loading branch information
bangjelkoski committed Nov 29, 2024
1 parent 486c8d1 commit bd99fb4
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class IndexerGrpcWeb3GwApi extends IndexerGrpcTransactionApi {
memo,
sequence,
accountNumber,
estimateGas = true,
estimateGas = false,
gasLimit = DEFAULT_GAS_LIMIT,
feeDenom = DEFAULT_BRIDGE_FEE_DENOM,
feePrice = DEFAULT_BRIDGE_FEE_PRICE,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import MsgGrantWithAuthorization from './MsgGrantWithAuthorization.js'
import { mockFactory } from '@injectivelabs/test-utils'
import { mockFactory, prepareEip712 } from '@injectivelabs/test-utils'
import GenericAuthorization from './authorizations/GenericAuthorization.js'
import ContractExecutionAuthorization from './authorizations/ContractExecutionAuthorization.js'
import { getEip712TypedData, getEip712TypedDataV2 } from '../../../tx/index.js'
import { IndexerGrpcWeb3GwApi } from './../../../../client/indexer/index.js'

const { injectiveAddress, injectiveAddress2 } = mockFactory

Expand Down Expand Up @@ -39,14 +41,6 @@ describe('MsgGrantWithAuthorization', () => {
expiration: new Date(params.expiration! * 1000),
},
}
const protoParamsGenericAuthorizationWeb3 = {
grantee: params.grantee,
granter: params.granter,
grant: {
authorization: params.authorization.toWeb3(),
expiration: new Date(params.expiration! * 1000),
},
}
const message = MsgGrantWithAuthorization.fromJSON(params)

it('generates proper proto', () => {
Expand Down Expand Up @@ -74,55 +68,6 @@ describe('MsgGrantWithAuthorization', () => {
value: protoParamsGenericAuthorizationAmino,
})
})

it('generates proper Eip712 types', () => {
const eip712Types = message.toEip712Types()

expect(Object.fromEntries(eip712Types)).toStrictEqual({
TypeGrant: [
{ name: 'authorization', type: 'TypeGrantAuthorization' },
{ name: 'expiration', type: 'string' },
],
TypeGrantAuthorization: [
{ name: 'type', type: 'string' },
{ name: 'value', type: 'TypeGrantAuthorizationValue' },
],
TypeGrantAuthorizationValue: [{ name: 'msg', type: 'string' }],
MsgValue: [
{ name: 'granter', type: 'string' },
{ name: 'grantee', type: 'string' },
{ name: 'grant', type: 'TypeGrant' },
],
})
})

it('generates proper Eip712 values', () => {
const eip712 = message.toEip712()

expect(eip712).toStrictEqual({
type: 'cosmos-sdk/MsgGrant',
value: {
granter: 'inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku',
grantee: 'inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku',
grant: {
authorization: {
type: 'cosmos-sdk/GenericAuthorization',
value: { msg: '/cosmos.bank.v1beta1.MsgSend' },
},
expiration: '2027-01-15T08:00:00Z',
},
},
})
})

it('generates proper web3', () => {
const web3 = message.toWeb3()

expect(web3).toStrictEqual({
'@type': protoType,
...protoParamsGenericAuthorizationWeb3,
})
})
})

describe('ContractExecutionAuthorization', () => {
Expand Down Expand Up @@ -164,14 +109,6 @@ describe('MsgGrantWithAuthorization', () => {
expiration: new Date(params.expiration! * 1000),
},
}
const protoParamsContractExecutionAuthorizationWeb3 = {
grantee: params.grantee,
granter: params.granter,
grant: {
authorization: params.authorization.toWeb3(),
expiration: new Date(params.expiration! * 1000),
},
}
const message = MsgGrantWithAuthorization.fromJSON(params)

it('generates proper proto', () => {
Expand Down Expand Up @@ -200,73 +137,37 @@ describe('MsgGrantWithAuthorization', () => {
})
})

it('generates proper Eip712 types', () => {
const eip712Types = message.toEip712Types()

expect(Object.fromEntries(eip712Types)).toStrictEqual({
TypeGrant: [
{ name: 'authorization', type: 'TypeGrantAuthorization' },
{ name: 'expiration', type: 'string' },
],
TypeGrantAuthorization: [
{ name: 'type', type: 'string' },
{ name: 'grants', type: 'TypeGrantAuthorizationGrants[]' },
],
TypeGrantAuthorizationGrants: [
{ name: 'contract', type: 'string' },
{ name: 'limit', type: 'TypeGrantAuthorizationGrantsLimit' },
{ name: 'filter', type: 'TypeGrantAuthorizationGrantsFilter' },
],
TypeGrantAuthorizationGrantsLimit: [
{ name: 'type', type: 'string' },
{ name: 'remaining', type: 'uint64' },
],
TypeGrantAuthorizationGrantsFilter: [
{ name: 'type', type: 'string' },
{ name: 'keys', type: 'string[]' },
],
MsgValue: [
{ name: 'granter', type: 'string' },
{ name: 'grantee', type: 'string' },
{ name: 'grant', type: 'TypeGrant' },
],
describe('generates proper EIP712 compared to the Web3Gw (chain)', () => {
const { endpoints, eip712Args, prepareEip712Request } = prepareEip712({
sequence: 0,
accountNumber: 3,
messages: message,
})
})

it('generates proper Eip712 values', () => {
const eip712 = message.toEip712()
test('EIP712 v1', async () => {
const eip712TypedData = getEip712TypedData(eip712Args)

expect(eip712).toStrictEqual({
type: 'cosmos-sdk/MsgGrant',
value: {
granter: 'inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku',
grantee: 'inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku',
grant: {
authorization: {
type: 'wasm/ContractExecutionAuthorization',
grants: [
{
contract: 'inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku',
limit: { type: 'wasm/MaxCallsLimit', remaining: 100 },
filter: {
type: 'wasm/AcceptedMessageKeysFilter',
keys: ['set_routes'],
},
},
],
},
expiration: '2027-01-15T08:00:00Z',
},
},
const txResponse = await new IndexerGrpcWeb3GwApi(
endpoints.indexer,
).prepareEip712Request({
...prepareEip712Request,
eip712Version: 'v1',
})

expect(eip712TypedData).toStrictEqual(JSON.parse(txResponse.data))
})
})

it('generates proper web3', () => {
const web3 = message.toWeb3()
test('EIP712 v2', async () => {
const eip712TypedData = getEip712TypedDataV2(eip712Args)

expect(web3).toStrictEqual({
'@type': protoType,
...protoParamsContractExecutionAuthorizationWeb3,
const txResponse = await new IndexerGrpcWeb3GwApi(
endpoints.indexer,
).prepareEip712Request({
...prepareEip712Request,
eip712Version: 'v2',
})

expect(eip712TypedData).toStrictEqual(JSON.parse(txResponse.data))
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ export default class MsgGrantWithAuthorization extends MsgBase<
grantee: amino.value.grantee,
grant: {
authorization: params.authorization.toWeb3(),
expiration: new Date(Number(timestamp.seconds) * 1000),
expiration:
new Date(Number(timestamp.seconds) * 1000).toJSON().slice(0, -5) +
'Z',
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default class ContractExecutionAuthorization extends BaseAuthorization<
if (params.limit) {
grant.limit = {
type: 'wasm/MaxCallsLimit',
remaining: params.limit.maxCalls,
remaining: params.limit.maxCalls.toString(),
}
}

Expand All @@ -128,7 +128,7 @@ export default class ContractExecutionAuthorization extends BaseAuthorization<
if (params.limit) {
grant.limit = {
'@type': '/cosmwasm.wasm.v1.MaxCallsLimit',
remaining: params.limit.maxCalls,
remaining: params.limit.maxCalls.toString(),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,35 +83,33 @@ describe('MsgCancelSpotOrder', () => {
})

describe('generates proper EIP712 compared to the Web3Gw (chain)', () => {
const { endpoints, eip712Args, prepareEip712Request } =
prepareEip712(message)
const { endpoints, eip712Args, prepareEip712Request } = prepareEip712({
sequence: 0,
accountNumber: 3,
messages: message,
})

it('EIP712 v1', async () => {
const eip712TypedData = getEip712TypedData(eip712Args)

try {
const txResponse = await new IndexerGrpcWeb3GwApi(
endpoints.indexer,
).prepareEip712Request({ ...prepareEip712Request, eip712Version: 'v1' })
const txResponse = await new IndexerGrpcWeb3GwApi(
endpoints.indexer,
).prepareEip712Request({
...prepareEip712Request,
eip712Version: 'v1',
})

expect(JSON.parse(txResponse.data)).toStrictEqual(eip712TypedData)
} catch (e: any) {
// throw silently now
}
expect(eip712TypedData).toStrictEqual(JSON.parse(txResponse.data))
})

it('EIP712 v2', async () => {
const eip712TypedData = getEip712TypedDataV2(eip712Args)

try {
const txResponse = await new IndexerGrpcWeb3GwApi(
endpoints.indexer,
).prepareEip712Request({ ...prepareEip712Request, eip712Version: 'v2' })
const txResponse = await new IndexerGrpcWeb3GwApi(
endpoints.indexer,
).prepareEip712Request({ ...prepareEip712Request, eip712Version: 'v2' })

expect(JSON.parse(txResponse.data)).toStrictEqual(eip712TypedData)
} catch (e: any) {
// throw silently now
}
expect(eip712TypedData).toStrictEqual(JSON.parse(txResponse.data))
})
})
})
1 change: 0 additions & 1 deletion packages/sdk-ts/src/core/tx/eip712/eip712.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export const getEip712TypedData = ({
fee,
types: typesWithMessageTypes,
})

return {
...typesWithFeePayer,
primaryType: 'Tx',
Expand Down
14 changes: 12 additions & 2 deletions packages/sdk-ts/src/core/tx/eip712/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ export const mapValuesToProperValueType = <T extends Record<string, unknown>>(
): T => {
const numberToStringKeys = [
'proposal_id',
'remaining',
'round',
'oracle_scale_factor',
'timeout_timestamp',
Expand All @@ -256,6 +257,9 @@ export const mapValuesToProperValueType = <T extends Record<string, unknown>>(
const nullableStringsTypeMaps = {
'wasmx/MsgExecuteContractCompat': ['funds'],
}
// const dateTypesMap = {
// 'cosmos-sdk/MsgGrant': ['expiration'],
// }

const nullableStrings = ['uri', 'uri_hash']

Expand Down Expand Up @@ -301,20 +305,25 @@ export const mapValuesToProperValueType = <T extends Record<string, unknown>>(
[key]: value.toJSON().split('.')[0] + 'Z',
}
}

if (Array.isArray(value)) {
return {
...result,
[key]: value.every((i) => typeof i === 'string')
? value
: value.map((item) =>
mapValuesToProperValueType(item as Record<string, unknown>),
mapValuesToProperValueType(
item as Record<string, unknown>,
),
),
}
}

return {
...result,
[key]: mapValuesToProperValueType(value as Record<string, unknown>),
[key]: mapValuesToProperValueType(
value as Record<string, unknown>,
),
}
}

Expand Down Expand Up @@ -609,6 +618,7 @@ export const protoTypeToAminoType = (type: string): string => {
return 'injective/tokenfactory/burn'
case 'injective.tokenfactory.v1beta1.MsgSetDenomMetadata':
return 'injective/tokenfactory/set-denom-metadata'

// Auth
case 'cosmos.auth.v1beta1.MsgUpdateParams':
return 'cosmos-sdk/x/auth/MsgUpdateParams'
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk-ts/src/core/tx/eip712/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const getEip712Fee = (

return {
fee: {
feePayer: feePayer,
...(feePayer && { feePayer }),
gas,
amount,
},
Expand Down Expand Up @@ -152,7 +152,7 @@ export const getEip712FeeV2 = (
fee: {
amount,
gas,
payer: payer,
...(payer && { payer }),
},
}
}
Expand Down
Loading

0 comments on commit bd99fb4

Please sign in to comment.