Skip to content

Commit

Permalink
Merge pull request #22 from m-Peter/unsupported-api-methods
Browse files Browse the repository at this point in the history
Add proper method interfaces for unsupported API methods
  • Loading branch information
m-Peter authored Jan 22, 2024
2 parents 6fdc8a1 + 3968c20 commit 1b1548a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 27 deletions.
28 changes: 11 additions & 17 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,28 +559,24 @@ func (s *BlockChainAPI) NewPendingTransactionFilter(fullTx *bool) rpc.ID {
// eth_accounts
// Accounts returns the collection of accounts this node manages.
func (s *BlockChainAPI) Accounts() []common.Address {
return []common.Address{
common.HexToAddress("0x407d73d8a49eeb85d32cf465507dd71d507100c1"),
}
return []common.Address{}
}

// eth_sign
// Sign calculates an Ethereum ECDSA signature for:
// keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))
// Sign calculates an ECDSA signature for:
// keccak256("\x19Ethereum Signed Message:\n" + len(message) + message).
//
// Note, the produced signature conforms to the secp256k1 curve R, S and V values,
// where the V value will be 27 or 28 for legacy reasons.
//
// The key used to calculate the signature is decrypted with the given password.
// The account associated with addr must be unlocked.
//
// https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_sign
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign
func (s *BlockChainAPI) Sign(
ctx context.Context,
addr common.Address,
data hexutil.Bytes,
address common.Address,
password string,
) (hexutil.Bytes, error) {
return hexutil.Bytes{}, fmt.Errorf("not implemented")
return hexutil.Bytes{}, fmt.Errorf("method is not implemented")
}

// eth_signTransaction
Expand All @@ -591,19 +587,17 @@ func (s *BlockChainAPI) SignTransaction(
ctx context.Context,
args TransactionArgs,
) (*SignTransactionResult, error) {
return &SignTransactionResult{}, fmt.Errorf("not implemented")
return &SignTransactionResult{}, fmt.Errorf("method is not implemented")
}

// eth_sendTransaction
// SendTransaction will create a transaction from the given arguments and
// tries to sign it with the key associated with args.From. If the given
// passwd isn't able to decrypt the key it fails.
// SendTransaction creates a transaction for the given argument, sign it
// and submit it to the transaction pool.
func (s *BlockChainAPI) SendTransaction(
ctx context.Context,
args TransactionArgs,
password string,
) (common.Hash, error) {
return common.Hash{}, fmt.Errorf("not implemented")
return common.Hash{}, fmt.Errorf("method is not implemented")
}

// eth_call (readonly calls, we might need this (if the wallet use it to get balanceOf an ERC-20))
Expand Down
71 changes: 64 additions & 7 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,7 @@ func TestBlockChainAPI(t *testing.T) {

assert.Equal(
t,
[]common.Address{
common.HexToAddress("0x407D73d8a49eeb85D32Cf465507dd71d507100c1"),
},
[]common.Address{},
accounts,
)
})
Expand All @@ -529,10 +527,8 @@ func TestBlockChainAPI(t *testing.T) {
key, _ := crypto.GenerateKey()
addr := crypto.PubkeyToAddress(key.PublicKey)
_, err := blockchainAPI.Sign(
context.Background(),
hexutil.Bytes{1, 2, 3, 4, 5},
addr,
"secret_password",
hexutil.Bytes{1, 2, 3, 4, 5},
)
require.Error(t, err)
assert.ErrorContains(t, err, "not implemented")
Expand Down Expand Up @@ -571,7 +567,6 @@ func TestBlockChainAPI(t *testing.T) {
To: &to.addr,
Value: (*hexutil.Big)(big.NewInt(1000)),
},
"secret_password",
)
require.Error(t, err)
assert.ErrorContains(t, err, "not implemented")
Expand Down Expand Up @@ -643,4 +638,66 @@ func TestBlockChainAPI(t *testing.T) {

assert.Equal(t, map[string]interface{}{}, uncle)
})

t.Run("Sign", func(t *testing.T) {
_, err := blockchainAPI.Sign(
common.HexToAddress("0x16c5785ac562ff41e2dcfdf829c5a142f1fccd7d"),
hexutil.Bytes{1, 2, 3, 4, 5},
)
require.Error(t, err)

assert.ErrorContains(
t,
err,
"method is not implemented",
)
})

t.Run("SignTransaction", func(t *testing.T) {
key1, _ := crypto.GenerateKey()
addr1 := crypto.PubkeyToAddress(key1.PublicKey)
from := Account{key: key1, addr: addr1}
key2, _ := crypto.GenerateKey()
addr2 := crypto.PubkeyToAddress(key1.PublicKey)
to := Account{key: key2, addr: addr2}
_, err := blockchainAPI.SignTransaction(
context.Background(),
api.TransactionArgs{
From: &from.addr,
To: &to.addr,
Value: (*hexutil.Big)(big.NewInt(1000)),
},
)
require.Error(t, err)

assert.ErrorContains(
t,
err,
"method is not implemented",
)
})

t.Run("SendTransaction", func(t *testing.T) {
key1, _ := crypto.GenerateKey()
addr1 := crypto.PubkeyToAddress(key1.PublicKey)
from := Account{key: key1, addr: addr1}
key2, _ := crypto.GenerateKey()
addr2 := crypto.PubkeyToAddress(key1.PublicKey)
to := Account{key: key2, addr: addr2}
_, err := blockchainAPI.SendTransaction(
context.Background(),
api.TransactionArgs{
From: &from.addr,
To: &to.addr,
Value: (*hexutil.Big)(big.NewInt(1000)),
},
)
require.Error(t, err)

assert.ErrorContains(
t,
err,
"method is not implemented",
)
})
}
5 changes: 4 additions & 1 deletion api/fixtures/eth_json_rpc_requests.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@
{"jsonrpc":"2.0","id":29,"method":"eth_call","params":[{"from":"0xb60e8dd61c5d32be8058bb8eb970870f07233155","to":"0xd46e8dd67c5d32be8058bb8eb970870f07244567","gas":"0x76c0","gasPrice":"0x9184e72a000","value":"0x9184e72a","input":"0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}]}
{"jsonrpc":"2.0","id":30,"method":"eth_estimateGas","params":[{"from":"0xb60e8dd61c5d32be8058bb8eb970870f07233155","to":"0xd46e8dd67c5d32be8058bb8eb970870f07244567","gas":"0x76c0","gasPrice":"0x9184e72a000","value":"0x9184e72a","input":"0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}]}
{"jsonrpc":"2.0","id":31,"method":"eth_getUncleByBlockHashAndIndex","params":["0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", "0x45"]}
{"jsonrpc":"2.0","id":32,"method":"eth_getUncleByBlockNumberAndIndex","params":["0xe8", "0x45"]}
{"jsonrpc":"2.0","id":32,"method":"eth_getUncleByBlockNumberAndIndex","params":["0xe8", "0x45"]}
{"jsonrpc":"2.0","id":33,"method":"eth_sign","params":["0x9b2055d370f73ec7d8a03e965129118dc8f5bf83","0xdeadbeaf"]}
{"jsonrpc":"2.0","id":34,"method":"eth_signTransaction","params":[{"from":"0x3b7252d007059ffc82d16d022da3cbf9992d2f70", "to":"0x0f54f47bf9b8e317b214ccd6a7c3e38b893cd7f0", "value":"0x16345785d8a0000", "gasLimit":"0x5208", "gasPrice":"0x55ae82600"}]}
{"jsonrpc":"2.0","id":35,"method":"eth_sendTransaction","params":[{"from":"0x3b7252d007059ffc82d16d022da3cbf9992d2f70", "to":"0x0f54f47bf9b8e317b214ccd6a7c3e38b893cd7f0", "value":"0x16345785d8a0000", "gasLimit":"0x5208", "gasPrice":"0x55ae82600"}]}
7 changes: 5 additions & 2 deletions api/fixtures/eth_json_rpc_responses.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
{"jsonrpc":"2.0","id":25,"result":[{"address":"0x16c5785ac562ff41e2dcfdf829c5a142f1fccd7d","topics":["0x59ebeb90bc63057b6515673c3ecf9438e5058bca0f92585014eced636878c9a5"],"data":"0x000000","blockNumber":"0x1b4","transactionHash":"0x00df829c5a142f1fccd7d8216c5785ac562ff41e2dcfdf5785ac562ff41e2dcf","transactionIndex":"0x0","blockHash":"0x008216c5785ac562ff41e2dcfdf5785ac562ff41e2dcfdf829c5a142f1fccd7d","logIndex":"0x1","removed":false}]}
{"jsonrpc":"2.0","id":26,"result":"block_filter"}
{"jsonrpc":"2.0","id":27,"result":"pending_tx_filter"}
{"jsonrpc":"2.0","id":28,"result":["0x407d73d8a49eeb85d32cf465507dd71d507100c1"]}
{"jsonrpc":"2.0","id":28,"result":[]}
{"jsonrpc":"2.0","id":29,"result":"0x00010203040506070809"}
{"jsonrpc":"2.0","id":30,"result":"0x69"}
{"jsonrpc":"2.0","id":31,"result":{}}
{"jsonrpc":"2.0","id":32,"result":{}}
{"jsonrpc":"2.0","id":32,"result":{}}
{"jsonrpc":"2.0","id":33,"error":{"code":-32000,"message":"method is not implemented"}}
{"jsonrpc":"2.0","id":34,"error":{"code":-32000,"message":"method is not implemented"}}
{"jsonrpc":"2.0","id":35,"error":{"code":-32000,"message":"method is not implemented"}}

0 comments on commit 1b1548a

Please sign in to comment.