Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move precompiles to repo #1625

Merged
merged 18 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

- (evm) [160](https://github.com/evmos/precompiles/pull/160) Fixed hard coded BaseDenom and wrong comparison for MaxUint256

### State Machine Breaking

- (evm) [#1625](https://github.com/evmos/evmos/pull/1625) Migrate updated EVM extensions

## [v13.0.0-rc2] - 2023-06-13

### State Machine Breaking
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/ethereum/go-ethereum v1.11.5
github.com/evmos/evmos-ledger-go v0.4.0
github.com/evmos/precompiles v0.0.0-20230615165716-e0a8cabcbf5d
github.com/gogo/protobuf v1.3.3
github.com/golang/protobuf v1.5.3
github.com/gorilla/mux v1.8.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,6 @@ github.com/evmos/evmos-ledger-go v0.4.0 h1:WPa6TN9x7s/+1YCgvYGtzJrhlW/mIHtYM1DvN
github.com/evmos/evmos-ledger-go v0.4.0/go.mod h1:33X3wWcWZ68KD5Bj4aiED+/W57uO1VR+hbXBxkKDXsQ=
github.com/evmos/go-ethereum v1.10.26-evmos-rc2 h1:tYghk1ZZ8X4/OQ4YI9hvtm8aSN8OSqO0g9vo/sCMdBo=
github.com/evmos/go-ethereum v1.10.26-evmos-rc2/go.mod h1:/6CsT5Ceen2WPLI/oCA3xMcZ5sWMF/D46SjM/ayY0Oo=
github.com/evmos/precompiles v0.0.0-20230615165716-e0a8cabcbf5d h1:Amope5DymYdDs/DU2I6zMX/fZ+xb+U87PQZ4lLtL55k=
github.com/evmos/precompiles v0.0.0-20230615165716-e0a8cabcbf5d/go.mod h1:EN85gINi4B+pjzbdPgqNNw9O1Oras++7gJEZAXD1K4E=
github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c h1:8ISkoahWXwZR41ois5lSJBSVw4D0OV19Ht/JSTzvSv0=
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 h1:JWuenKqqX8nojtoVVWjGfOF9635RETekkoH6Cc9SX0A=
github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 h1:7HZCaLC5+BZpmbhCOZJ293Lz68O7PYrF2EzeiFMwCLk=
Expand Down
101 changes: 101 additions & 0 deletions precompiles/authorization/Authorization.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity >=0.8.17 .0;

/// @author Evmos Team
/// @title Authorization Interface
/// @dev The interface through which solidity contracts will interact with smart contract approvals.
interface AuthorizationI {
/// @dev Approves a list of Cosmos or IBC transactions with a specific amount of tokens.
/// @param spender The address which will spend the funds.
/// @param amount The amount of tokens to be spent.
/// @param methods The message type URLs of the methods to approve.
/// @return approved Boolean value to indicate if the approval was successful.
function approve(
address spender,
uint256 amount,
string[] calldata methods
) external returns (bool approved);

/// @dev Revokes a list of Cosmos transactions.
/// @param spender The address which will spend the funds.
/// @param methods The message type URLs of the methods to revoke.
/// @return revoked Boolean value to indicate if the revocation was successful.
function revoke(
address spender,
string[] calldata methods
) external returns (bool revoked);

/// @dev Increase the allowance of a given spender by a specific amount of tokens for IBC
/// transfer methods or staking.
/// @param spender The address which will spend the funds.
/// @param amount The amount of tokens to be spent.
/// @param methods The message type URLs of the methods to approve.
/// @return approved Boolean value to indicate if the approval was successful.
function increaseAllowance(
address spender,
uint256 amount,
string[] calldata methods
) external returns (bool approved);

/// @dev Decreases the allowance of a given spender by a specific amount of tokens for IBC
/// transfer methods or staking.
/// @param spender The address which will spend the funds.
/// @param amount The amount of tokens to be spent.
/// @param methods The message type URLs of the methods to approve.
/// @return approved Boolean value to indicate if the approval was successful.
function decreaseAllowance(
address spender,
uint256 amount,
string[] calldata methods
) external returns (bool approved);

/// @dev Returns the remaining number of tokens that spender will be allowed to spend
/// on behalf of the owner through IBC transfer methods or staking. This is zero by default.
/// @param owner The address of the account owning tokens.
/// @param spender The address of the account able to transfer the tokens.
/// @param method The message type URL of the methods for which the approval should be queried.
/// @return remaining The remaining number of tokens available to be spent.
function allowance(
address owner,
address spender,
string calldata method
) external view returns (uint256 remaining);

/// @dev This event is emitted when the allowance of a spender is set by a call to the approve method.
/// The value field specifies the new allowance and the methods field holds the information for which methods
/// the approval was set.
/// @param owner The owner of the tokens.
/// @param spender The address which will spend the funds.
/// @param methods The message type URLs of the methods for which the approval is set.
/// @param value The amount of tokens approved to be spent.
event Approval(
address indexed owner,
address indexed spender,
string[] methods,
uint256 value
);

/// @dev This event is emitted when an owner revokes a spender's allowance.
/// @param owner The owner of the tokens.
/// @param spender The address which will spend the funds.
/// @param methods The message type URLs of the methods for which the approval is set.
event Revocation(
address indexed owner,
address indexed spender,
string[] methods
);

/// @dev This event is emitted when the allowance of a spender is changed by a call to the decrease or increase
/// allowance method. The values field specifies the new allowances and the methods field holds the
/// information for which methods the approval was set.
/// @param owner The owner of the tokens.
/// @param spender The address which will spend the funds.
/// @param methods The message type URLs of the methods for which the approval is set.
/// @param values The amounts of tokens approved to be spent.
event AllowanceChange(
address indexed owner,
address indexed spender,
string[] methods,
uint256[] values
);
}
22 changes: 22 additions & 0 deletions precompiles/authorization/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package authorization

const (
// ErrAuthzDoesNotExistOrExpired is raised when the authorization does not exist.
ErrAuthzDoesNotExistOrExpired = "authorization to %s for address %s does not exist or is expired"
// ErrEmptyMethods is raised when the given methods array is empty.
ErrEmptyMethods = "no methods defined; expected at least one message type url"
// ErrEmptyStringInMethods is raised when the given methods array contains an empty string.
ErrEmptyStringInMethods = "empty string found in methods array; expected no empty strings to be passed; got: %v"
// ErrExceededAllowance is raised when the amount exceeds the set allowance.
ErrExceededAllowance = "amount %s greater than allowed limit %s"
// ErrInvalidGranter is raised when the granter address is not valid.
ErrInvalidGranter = "invalid granter address: %v"
// ErrInvalidGrantee is raised when the grantee address is not valid.
ErrInvalidGrantee = "invalid grantee address: %v"
// ErrInvalidMethods is raised when the given methods cannot be unpacked.
ErrInvalidMethods = "invalid methods defined; expected an array of strings; got: %v"
// ErrInvalidMethod is raised when the given method cannot be unpacked.
ErrInvalidMethod = "invalid method defined; expected a string; got: %v"
// ErrAuthzNotAccepted is raised when the authorization is not accepted.
ErrAuthzNotAccepted = "authorization to %s for address %s is not accepted"
)
124 changes: 124 additions & 0 deletions precompiles/authorization/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright Tharsis Labs Ltd.(Evmos)
// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE)

package authorization

import (
"fmt"
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
cmn "github.com/evmos/evmos/v13/precompiles/common"
)

// ApprovalEvent is the event emitted on a successful Approve transaction.
type ApprovalEvent struct {
Grantee common.Address
Granter common.Address
Coin *sdk.Coin
TypeUrls []string
}

// AllowanceChangeEvent is the event emitted on successful IncreaseAllowance or DecreaseAllowance transactions.
type AllowanceChangeEvent struct {
Grantee common.Address
Granter common.Address
Values []*big.Int
TypeUrls []string
}

// RevocationEvent is the event emitted on a successful Revoke transaction.
type RevocationEvent struct {
Grantee common.Address
Granter common.Address
TypeUrls []string
}

// EmitAllowanceChangeEvent creates a new allowance change event emitted on IncreaseAllowance
// and DecreaseAllowance transactions.
func EmitAllowanceChangeEvent(args cmn.EmitEventArgs) error {
// check if the provided Event is correct type
allowanceChangeEvent, ok := args.EventData.(AllowanceChangeEvent)
if !ok {
return fmt.Errorf("invalid Event type, expecting AllowanceChangeEvent but received %T", args.EventData)
}

// Prepare the event topics
event := args.ContractEvents[EventTypeAllowanceChange]
topics := make([]common.Hash, 3)

// The first topic is always the signature of the event.
topics[0] = event.ID

var err error
topics[1], err = cmn.MakeTopic(allowanceChangeEvent.Grantee)
if err != nil {
return err
}

topics[2], err = cmn.MakeTopic(allowanceChangeEvent.Granter)
if err != nil {
return err
}

// Pack the arguments to be used as the Data field
arguments := abi.Arguments{event.Inputs[2], event.Inputs[3]}
packed, err := arguments.Pack(allowanceChangeEvent.TypeUrls, allowanceChangeEvent.Values)
if err != nil {
return err
}

args.StateDB.AddLog(&ethtypes.Log{
Address: args.ContractAddr,
Topics: topics,
Data: packed,
BlockNumber: uint64(args.Ctx.BlockHeight()),

Check failure

Code scanning / gosec

Potential integer overflow by integer type conversion Error

Potential integer overflow by integer type conversion
})

return nil
}

// EmitRevocationEvent creates a new approval event emitted on a Revoke transaction.
func EmitRevocationEvent(args cmn.EmitEventArgs) error {
// Prepare the event topics
revocationEvent, ok := args.EventData.(RevocationEvent)
if !ok {
return fmt.Errorf("invalid Event type, expecting RevocationEvent but received %T", args.EventData)
}
// Prepare the event topics
event := args.ContractEvents[EventTypeRevocation]
topics := make([]common.Hash, 3)

// The first topic is always the signature of the event.
topics[0] = event.ID

var err error
topics[1], err = cmn.MakeTopic(revocationEvent.Grantee)
if err != nil {
return err
}

topics[2], err = cmn.MakeTopic(revocationEvent.Granter)
if err != nil {
return err
}

// Pack the arguments to be used as the Data field
arguments := abi.Arguments{event.Inputs[2]}
packed, err := arguments.Pack(revocationEvent.TypeUrls)
if err != nil {
return err
}

args.StateDB.AddLog(&ethtypes.Log{
Address: args.ContractAddr,
Topics: topics,
Data: packed,
BlockNumber: uint64(args.Ctx.BlockHeight()),

Check failure

Code scanning / gosec

Potential integer overflow by integer type conversion Error

Potential integer overflow by integer type conversion
})

return nil
}
Loading
Loading