Skip to content

Commit

Permalink
Revert/construction endpoints (#127)
Browse files Browse the repository at this point in the history
* Revert "feat: construction endpoints (#111)"

This reverts commit 56744be.

* Temporarily disable linting and re-add gitignore
  • Loading branch information
eelanagaraj authored Dec 9, 2020
1 parent 0fbae79 commit cea2396
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 334 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ issues:
- misspell

exclude:
- composites
- composites
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ GO ?= latest

GITHUB_ORG?=celo-org
GITHUB_REPO?=rosetta
CELO_BLOCKCHAIN_PATH=../celo-blockchain
CELO_MONOREPO_PATH=../celo-monorepo

GOLANGCI_VERSION=1.32.2
GOLANGCI_exists := $(shell command -v golangci-lint 2> /dev/null)
Expand Down
4 changes: 2 additions & 2 deletions airgap/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (tx *Transaction) Deserialize(data []byte, chainId *big.Int) error {
if v == nil || r == nil || s == nil {
return errors.New("can't deserialize unsigned transactions")
}
tx.Signature = ValuesToSignature(chainId, v, r, s)
tx.Signature = valuesToSignature(chainId, v, r, s)
tx.From, err = types.Sender(types.NewEIP155Signer(chainId), gethTx)
if err != nil {
return err
Expand All @@ -230,7 +230,7 @@ func (tx *Transaction) Deserialize(data []byte, chainId *big.Int) error {
return nil
}

func ValuesToSignature(chainId, v, r, s *big.Int) []byte {
func valuesToSignature(chainId, v, r, s *big.Int) []byte {
sig := make([]byte, crypto.SignatureLength)

// doing the inverse of signer.SignatureValues()
Expand Down
2 changes: 1 addition & 1 deletion airgap/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestTransaction(t *testing.T) {
gethTx, _ := signedTx.AsGethTransaction()

v, r, s := gethTx.RawSignatureValues()
sig := ValuesToSignature(tx.ChainId, v, r, s)
sig := valuesToSignature(tx.ChainId, v, r, s)
Ω(sig).Should(Equal(signedTx.Signature))

addr, err := types.NewEIP155Signer(tx.ChainId).Sender(gethTx)
Expand Down
5 changes: 3 additions & 2 deletions analyzer/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,10 @@ func ReconcileLogOpsWithTransfers(logOps, transferOps []Operation, tobinTax *Tob
// append logOp
// append remaining transferOps
i := 0
for index := range logOps {
logOp := logOps[index]
for _, logOp := range logOps {
if logOp.Type.requiresTransfer() {
// TODO - revisit
// nolint:gosec
matchIndex, err := findMatchAndReconcile(transferOps, &logOp, i)
if err != nil {
return nil, err
Expand Down
105 changes: 0 additions & 105 deletions go.sum

Large diffs are not rendered by default.

250 changes: 29 additions & 221 deletions service/rpc/servicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
package rpc

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
Expand All @@ -34,8 +32,6 @@ import (
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
ethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
)

Expand Down Expand Up @@ -176,7 +172,6 @@ func (s *Servicer) AccountBalance(ctx context.Context, request *types.AccountBal
emptyResponse := &types.AccountBalanceResponse{
BlockIdentifier: HeaderToBlockIdentifier(&blockHeader.Header),
}

createResponse := func(amount *types.Amount) *types.AccountBalanceResponse {
return &types.AccountBalanceResponse{
BlockIdentifier: HeaderToBlockIdentifier(&blockHeader.Header),
Expand Down Expand Up @@ -410,8 +405,10 @@ func (s *Servicer) BlockTransaction(ctx context.Context, request *types.BlockTra
return nil, LogErrCeloClient("TraceTransaction", err)
}

for i := range ops {
transferOps := OperationsFromAnalyzer(&ops[i], int64(len(operations)))
for _, aop := range ops {
// TODO - revisit
// nolint:gosec
transferOps := OperationsFromAnalyzer(&aop, int64(len(operations)))
operations = append(operations, transferOps...)
}
}
Expand Down Expand Up @@ -477,228 +474,50 @@ func (s *Servicer) Call(ctx context.Context, request *types.CallRequest) (*types
return nil, LogErrValidation(fmt.Errorf("unsupported method '%s'", request.Method))
}

func (s *Servicer) ConstructionDerive(
ctx context.Context,
req *types.ConstructionDeriveRequest,
) (*types.ConstructionDeriveResponse, *types.Error) {
pubkey, err := crypto.DecompressPubkey(req.PublicKey.Bytes)
if err != nil {
return nil, LogErrInternal(errors.New("unable to decompress public key"), err)
}

addr := crypto.PubkeyToAddress(*pubkey)

return &types.ConstructionDeriveResponse{
AccountIdentifier: &types.AccountIdentifier{
Address: addr.Hex(),
},
}, nil
}

func (s *Servicer) ConstructionCombine(
ctx context.Context,
req *types.ConstructionCombineRequest,
context.Context,
*types.ConstructionCombineRequest,
) (*types.ConstructionCombineResponse, *types.Error) {
var metadata airgap.TxMetadata

if err := json.Unmarshal([]byte(req.UnsignedTransaction), &metadata); err != nil {
return nil, ErrInternal
}

tx := airgap.Transaction{
TxMetadata: &metadata,
Signature: req.Signatures[0].Bytes,
}

signedTx, err := tx.AsGethTransaction()
if err != nil {
return nil, ErrInternal
}

signedTxJSON, err := signedTx.MarshalJSON()
if err != nil {
return nil, ErrInternal
}
return nil, LogErrUnimplemented("ConstructionCombine")
}

return &types.ConstructionCombineResponse{
SignedTransaction: string(signedTxJSON),
}, nil
func (s *Servicer) ConstructionDerive(
context.Context,
*types.ConstructionDeriveRequest,
) (*types.ConstructionDeriveResponse, *types.Error) {
return nil, LogErrUnimplemented("ConstructionDerive")
}

func (s *Servicer) ConstructionHash(
ctx context.Context,
req *types.ConstructionHashRequest,
context.Context,
*types.ConstructionHashRequest,
) (*types.TransactionIdentifierResponse, *types.Error) {
t := new(ethTypes.Transaction)
err := t.UnmarshalJSON([]byte(req.SignedTransaction))

if err != nil {
return nil, ErrInternal
}

return &types.TransactionIdentifierResponse{
TransactionIdentifier: &types.TransactionIdentifier{
Hash: t.Hash().Hex(),
},
}, nil
return nil, LogErrUnimplemented("ConstructionHash")
}

func (s *Servicer) ConstructionParse(
ctx context.Context,
req *types.ConstructionParseRequest,
context.Context,
*types.ConstructionParseRequest,
) (*types.ConstructionParseResponse, *types.Error) {
var tx airgap.Transaction
if !req.Signed {
err := json.Unmarshal([]byte(req.Transaction), &tx)
if err != nil {
return nil, ErrInternal
}
} else {
t := new(ethTypes.Transaction)
err := t.UnmarshalJSON([]byte(req.Transaction))
if err != nil {
return nil, ErrInternal
}

from, err := ethTypes.Sender(ethTypes.NewEIP155Signer(t.ChainId()), t)
if err != nil {
return nil, ErrInternal
}

txMetadata := &airgap.TxMetadata{
To: *t.To(),
From: from,
ChainId: t.ChainId(),
Gas: t.Gas(),
GasPrice: t.GasPrice(),
Nonce: t.Nonce(),
Data: t.Data(),
Value: t.Value(),
FeeCurrency: t.FeeCurrency(),
GatewayFee: t.GatewayFee(),
GatewayFeeRecipient: t.GatewayFeeRecipient(),
}
v, r, s := t.RawSignatureValues()
signature := airgap.ValuesToSignature(t.ChainId(), v, r, s)

tx = airgap.Transaction{
TxMetadata: txMetadata,
Signature: signature,
}
}

ops := []*types.Operation{
{
Type: "transfer",
OperationIdentifier: &types.OperationIdentifier{
Index: 0,
},
Account: &types.AccountIdentifier{
Address: tx.From.Hex(),
},
Amount: &types.Amount{
Value: new(big.Int).Neg(tx.Value).String(),
Currency: CeloGold,
},
},
{
Type: "transfer",
OperationIdentifier: &types.OperationIdentifier{
Index: 1,
},
RelatedOperations: []*types.OperationIdentifier{
{
Index: 0,
},
},
Account: &types.AccountIdentifier{
Address: tx.To.Hex(),
},
Amount: &types.Amount{
Value: tx.Value.String(),
Currency: CeloGold,
},
},
}

var resp *types.ConstructionParseResponse
resp = &types.ConstructionParseResponse{
Operations: ops,
}
if req.Signed {
resp.AccountIdentifierSigners = []*types.AccountIdentifier{
{
Address: tx.From.Hex(),
},
}
}

return resp, nil
return nil, LogErrUnimplemented("ConstructionParse")
}

func (s *Servicer) ConstructionPayloads(
ctx context.Context,
req *types.ConstructionPayloadsRequest,
context.Context,
*types.ConstructionPayloadsRequest,
) (*types.ConstructionPayloadsResponse, *types.Error) {
bz, err := json.Marshal(req.Metadata)
if err != nil {
return nil, ErrInternal
}
var metadata airgap.TxMetadata
err = metadata.UnmarshalJSON(bz)
if err != nil {
return nil, ErrInternal
}

value := req.Operations[0].Amount.Value
valueInt, ok := new(big.Int).SetString(value, 10)
if !ok {
return nil, nil
}
valueInt.Abs(valueInt)
metadata.Value = valueInt

tx := airgap.Transaction{
TxMetadata: &metadata,
Signature: []byte{},
}

gethTx, _ := tx.AsGethTransaction()
signer := ethTypes.NewEIP155Signer(tx.ChainId)

// Construct SigningPayload
payload := &types.SigningPayload{
AccountIdentifier: &types.AccountIdentifier{
Address: tx.From.Hex(),
},
Bytes: signer.Hash(gethTx).Bytes(),
SignatureType: types.EcdsaRecovery,
}

unsignedTxJSON, err := json.Marshal(tx)
if err != nil {
return nil, ErrInternal
}

return &types.ConstructionPayloadsResponse{
UnsignedTransaction: string(unsignedTxJSON),
Payloads: []*types.SigningPayload{payload},
}, nil
return nil, LogErrUnimplemented("ConstructionPayloads")
}

func (s *Servicer) ConstructionPreprocess(
ctx context.Context,
req *types.ConstructionPreprocessRequest,
context.Context,
*types.ConstructionPreprocessRequest,
) (*types.ConstructionPreprocessResponse, *types.Error) {
options := make(map[string]interface{})
options["From"] = req.Operations[0].Account.Address
options["To"] = req.Operations[1].Account.Address
return &types.ConstructionPreprocessResponse{
Options: options,
}, nil
return nil, LogErrUnimplemented("ConstructionPreprocess")
}

func (s *Servicer) ConstructionMetadata(ctx context.Context, request *types.ConstructionMetadataRequest) (*types.ConstructionMetadataResponse, *types.Error) {

var txArgs airgap.TxArgs
if err := airgap.UnmarshallFromMap(request.Options, &txArgs); err != nil {
return nil, LogErrValidation(err)
Expand All @@ -720,21 +539,10 @@ func (s *Servicer) ConstructionMetadata(ctx context.Context, request *types.Cons
return &response, nil
}

func (s *Servicer) ConstructionSubmit(ctx context.Context, req *types.ConstructionSubmitRequest) (*types.TransactionIdentifierResponse, *types.Error) {
t := new(ethTypes.Transaction)

err := t.UnmarshalJSON([]byte(req.SignedTransaction))
if err != nil {
return nil, ErrInternal
}

var buf bytes.Buffer
err = t.EncodeRLP(&buf)
if err != nil {
return nil, ErrInternal
}
func (s *Servicer) ConstructionSubmit(ctx context.Context, request *types.ConstructionSubmitRequest) (*types.TransactionIdentifierResponse, *types.Error) {
rawTx := common.Hex2Bytes(request.SignedTransaction)

txhash, err := s.airgap.SubmitTx(ctx, buf.Bytes())
txhash, err := s.airgap.SubmitTx(ctx, rawTx)
if err != nil {
return nil, LogErrCeloClient("SendRawTx", err)
}
Expand Down

0 comments on commit cea2396

Please sign in to comment.