Skip to content

Commit

Permalink
WIP: Fix transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianBorst committed Jul 31, 2023
1 parent 1b8031a commit 268986e
Show file tree
Hide file tree
Showing 64 changed files with 1,324 additions and 681 deletions.
360 changes: 22 additions & 338 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/proto/src/encoding/decoder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PubKey } from '../proto/ethermint/crypto/keys.js'
import { EthAccount } from '../proto/ethermint/types/account.js'
import { PubKey } from '../proto/ethermint/crypto/v1/ethsecp256k1/keys_pb'
import { EthAccount } from '../proto/ethermint/types/v1/account_pb'

export const decodeEthSecp256k1PubKey = (key: any) => {
const { typeUrl, value } = key
Expand Down
2 changes: 1 addition & 1 deletion packages/proto/src/proto/lockup/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * as Genesis from './genesis_pb.js'
export * as Genesis from './v1/genesis_pb'
3 changes: 2 additions & 1 deletion packages/proto/src/transaction/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import {
AuthInfo,
SignDoc,
} from '../proto/cosmos/transactions/tx.js'
import { PubKey } from '../proto/ethermint/crypto/keys.js'
import { PubKey } from '../proto/ethermint/crypto/v1/ethsecp256k1/keys_pb.js'
import { PubKey as SECP256k1 } from '../proto/cosmos/crypto/secp256k1/keys.js'
import { SignMode } from '../proto/cosmos/transactions/signing.js'
import { AminoTypes } from '../amino/registry.js'
import { convertProtoMessageToObject } from '../amino/objectConverter.js'
import { MessageGenerated, createAnyMessage } from '../messages/common.js'


export const SIGN_DIRECT = SignMode.DIRECT
Expand Down
1 change: 0 additions & 1 deletion packages/transactions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"start": "node dist/index.js"
},
"dependencies": {
"@althea-net/althea-proto": "^0.1.0",
"@althea-net/eip712": "^0.4.2",
"link-module-alias": "^1.2.0",
"shx": "^0.3.4"
Expand Down
5 changes: 0 additions & 5 deletions packages/transactions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
export * from './messages/base.js'
export * from './messages/common.js'
export * from './messages/authz/index.js'
export * from './messages/bank/index.js'
export * from './messages/distribution/index.js'
export * from './messages/erc20/index.js'
export * from './messages/gov/index.js'
export * from './messages/ibc/index.js'
export * from './messages/staking/index.js'
export * from './messages/vesting/index.js'
export * from './messages/common.js'
export * from './messages/txRaw.js'
export * from './messages/web3Extension.js'
28 changes: 10 additions & 18 deletions packages/transactions/src/messages/authz/genericGrant.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import {
MsgGrant
} from '@althea-net/althea-proto/src/codegen/cosmos/authz/v1beta1/tx.js'
import {
Grant, GenericAuthorization
} from '@althea-net/althea-proto/src/codegen/cosmos/authz/v1beta1/authz.js'

createGenericAuthorization as protoCreateGenericAuthorization,
createMsgGrant,
} from '@evmos/proto'

import {
generateTypes,
createMsgGenericAuthorization,
MSG_GENERIC_AUTHORIZATION_TYPES,
} from '@althea-net/eip712'
} from '@evmos/eip712'

import {
createTxMsgGenericGrant,
Expand Down Expand Up @@ -44,18 +41,13 @@ describe('test tx payload', () => {
message,
}

const authz = GenericAuthorization.fromJSON({
msg: params.typeUrl,
})
const grant = Grant.fromJSON({
authorization: authz,
expiration: params.expires,
})
const messageCosmos = MsgGrant.fromJSON({
granter: context.sender.accountAddress,
grantee: params.granteeAddress,
const grant = protoCreateGenericAuthorization(params.typeUrl)
const messageCosmos = createMsgGrant(
context.sender.accountAddress,
params.granteeAddress,
grant,
})
params.expires,
)

const payload = createTxMsgGenericGrant(context, params)
const expectedPayload = createTransactionPayload(
Expand Down
39 changes: 19 additions & 20 deletions packages/transactions/src/messages/authz/genericGrant.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import {
MsgGrant
} from '@althea-net/althea-proto/src/codegen/cosmos/authz/v1beta1/tx.js'
import {
Grant, GenericAuthorization
} from '@althea-net/althea-proto/src/codegen/cosmos/authz/v1beta1/authz.js'

createGenericAuthorization as protoCreateGenericAuthorization,
createMsgGrant,
} from '@evmos/proto'

import {
generateTypes,
createMsgGenericAuthorization,
MSG_GENERIC_AUTHORIZATION_TYPES,
} from '@althea-net/eip712'
} from '@evmos/eip712'
import { createTransactionPayload, TxContext } from '../base.js'

export interface MsgGenericAuthorizationParams {
Expand All @@ -24,7 +22,12 @@ const createEIP712MsgGenericGrant = (
) => {
const types = generateTypes(MSG_GENERIC_AUTHORIZATION_TYPES)

const message = createCosmosMsgGenericGrant(context, params)
const message = createMsgGenericAuthorization(
context.sender.accountAddress,
params.granteeAddress,
params.typeUrl,
params.expires,
)

return {
types,
Expand All @@ -36,18 +39,14 @@ const createCosmosMsgGenericGrant = (
context: TxContext,
params: MsgGenericAuthorizationParams,
) => {
const authz = GenericAuthorization.fromJSON({
msg: params.typeUrl,
})
const grant = Grant.fromJSON({
authorization: authz,
expiration: params.expires,
})
return MsgGrant.fromJSON({
granter: context.sender.accountAddress,
grantee: params.granteeAddress,
grant,
})
const msgGenericGrant = protoCreateGenericAuthorization(params.typeUrl)

return createMsgGrant(
context.sender.accountAddress,
params.granteeAddress,
msgGenericGrant,
params.expires,
)
}

/**
Expand Down
16 changes: 7 additions & 9 deletions packages/transactions/src/messages/authz/genericRevoke.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {
MsgRevoke
} from '@althea-net/althea-proto/src/codegen/cosmos/authz/v1beta1/tx.js'
import { createMsgRevoke } from '@evmos/proto'

import {
generateTypes,
createMsgRevokeGenericAuthorization,
MSG_REVOKE_GENERIC_AUTHORIZATION_TYPES,
} from '@althea-net/eip712'
} from '@evmos/eip712'

import {
MsgGenericRevokeParams,
Expand Down Expand Up @@ -37,11 +35,11 @@ describe('test tx payload', () => {
message,
}

const messageCosmos = MsgRevoke.fromJSON({
granter: context.sender.accountAddress,
grantee: params.granteeAddress,
msg_type_url: params.typeUrl,
})
const messageCosmos = createMsgRevoke(
context.sender.accountAddress,
params.granteeAddress,
params.typeUrl,
)

const payload = createTxMsgGenericRevoke(context, params)

Expand Down
16 changes: 7 additions & 9 deletions packages/transactions/src/messages/authz/genericRevoke.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {
MsgRevoke
} from '@althea-net/althea-proto/src/codegen/cosmos/authz/v1beta1/tx.js'
import { createMsgRevoke } from '@evmos/proto'

import {
generateTypes,
createMsgRevokeGenericAuthorization,
MSG_REVOKE_GENERIC_AUTHORIZATION_TYPES,
} from '@althea-net/eip712'
} from '@evmos/eip712'
import { createTransactionPayload, TxContext } from '../base.js'

export interface MsgGenericRevokeParams {
Expand Down Expand Up @@ -36,11 +34,11 @@ const createCosmosMsgGenericRevoke = (
context: TxContext,
params: MsgGenericRevokeParams,
) => {
return MsgRevoke.fromJSON({
granter: context.sender.accountAddress,
grantee: params.granteeAddress,
msg_type_url: params.typeUrl,
})
return createMsgRevoke(
context.sender.accountAddress,
params.granteeAddress,
params.typeUrl,
)
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/transactions/src/messages/authz/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './genericGrant.js'
export * from './genericRevoke.js'
export * from './stakingGrant.js'
export * from './stakingRevoke.js'
66 changes: 66 additions & 0 deletions packages/transactions/src/messages/authz/stakingGrant.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { createMsgGrant, createStakeAuthorization, Proto } from '@evmos/proto'
import { createTransactionPayload } from '../base'

import {
MsgStakeAuthorizationParams,
createTxMsgStakeAuthorization,
} from './stakingGrant'
import TestUtils from '../../tests/utils'

const { context, denom } = TestUtils
const granteeAddress = TestUtils.addr1
const maxTokens = TestUtils.amount1
const expiration = 10000

const createParams = (validatorAddress: string | string[]) => ({
granteeAddress,
validatorAddress,
denom,
maxTokens,
expiration,
})

const validatePayload = (params: MsgStakeAuthorizationParams) => {
const typedData = {
types: {},
message: {},
}

const stakeAuthType = Proto.Cosmos.Staking.Authz.AuthorizationType.DELEGATE
const auth = createStakeAuthorization(
params.validatorAddress,
params.denom,
params.maxTokens,
stakeAuthType,
)

const messageCosmos = createMsgGrant(
context.sender.accountAddress,
params.granteeAddress,
auth,
params.expiration,
)

const payload = createTxMsgStakeAuthorization(context, params)
const expectedPayload = createTransactionPayload(
context,
typedData,
messageCosmos,
)

expect(payload).toStrictEqual(expectedPayload)
}

describe('test tx payload', () => {
it('correctly produces tx payloads with single validator', () => {
const validator = TestUtils.addrVal1
const params = createParams(validator)
validatePayload(params)
})

it('correctly produces tx payloads with multiple validators', () => {
const validators = [TestUtils.addrVal1, TestUtils.addrVal2]
const params = createParams(validators)
validatePayload(params)
})
})
62 changes: 62 additions & 0 deletions packages/transactions/src/messages/authz/stakingGrant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { createMsgGrant, createStakeAuthorization, Proto } from '@evmos/proto'
import { createTransactionPayload, TxContext } from '../base.js'

export interface MsgStakeAuthorizationParams {
granteeAddress: string
validatorAddress: string | string[]
denom: string
maxTokens: string | undefined
expiration: number
}

const createEIP712MsgStakeAuthorization = () => {
// TODO: EIP-712 Stake Authorization is blocked until StakeAuthorization
// is added to @evmos/eip712
return {
types: {},
message: {},
}
}

const createCosmosMsgStakeAuthorization = (
context: TxContext,
params: MsgStakeAuthorizationParams,
) => {
const stakeAuthType = Proto.Cosmos.Staking.Authz.AuthorizationType.DELEGATE

const auth = createStakeAuthorization(
params.validatorAddress,
params.denom,
params.maxTokens,
stakeAuthType,
)

return createMsgGrant(
context.sender.accountAddress,
params.granteeAddress,
auth,
params.expiration,
)
}

/**
* Creates a transaction for a staking MsgGrant.
*
* @remarks
* This method creates a transaction wrapping the Cosmos SDK's
* {@link https://docs.cosmos.network/main/modules/authz#msggrant | MsgGrant}
*
* @param context - Transaction Context
* @param params - MsgGrant Staking Auth Params
* @returns Transaction with the MsgGrant payload
*
*/
export function createTxMsgStakeAuthorization(
context: TxContext,
params: MsgStakeAuthorizationParams,
) {
const typedData = createEIP712MsgStakeAuthorization()
const cosmosMsg = createCosmosMsgStakeAuthorization(context, params)

return createTransactionPayload(context, typedData, cosmosMsg)
}
40 changes: 40 additions & 0 deletions packages/transactions/src/messages/authz/stakingRevoke.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { createMsgRevoke, RevokeMessages } from '@evmos/proto'

import { createTransactionPayload } from '../base'

import {
MsgStakeRevokeAuthorizationParams,
createTxMsgStakeRevokeAuthorization,
} from './stakingRevoke'
import TestUtils from '../../tests/utils'

const { context } = TestUtils
const granteeAddress = TestUtils.addr1

const params: MsgStakeRevokeAuthorizationParams = {
granteeAddress,
}

describe('test tx payload', () => {
it('produces tx payloads as expected', () => {
const typedData = {
types: {},
message: {},
}

const messageCosmos = createMsgRevoke(
context.sender.accountAddress,
params.granteeAddress,
RevokeMessages.REVOKE_MSG_DELEGATE,
)

const payload = createTxMsgStakeRevokeAuthorization(context, params)
const expectedPayload = createTransactionPayload(
context,
typedData,
messageCosmos,
)

expect(payload).toStrictEqual(expectedPayload)
})
})
Loading

0 comments on commit 268986e

Please sign in to comment.