|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + ethAbi "github.com/ethereum/go-ethereum/accounts/abi" |
| 12 | + "github.com/oasisprotocol/oasis-core/go/common/cbor" |
| 13 | + "github.com/oasisprotocol/oasis-core/go/common/quantity" |
| 14 | + |
| 15 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config" |
| 16 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/types" |
| 17 | +) |
| 18 | + |
| 19 | +// subcallAddress is the address of the "Subcall" precompile that dispatches |
| 20 | +// ABI-encoded (method, CBOR body) calldata as an SDK runtime call. See |
| 21 | +// https://github.com/oasisprotocol/oasis-sdk/blob/main/runtime-sdk/modules/evm/src/precompile/subcall.rs. |
| 22 | +const subcallAddress = "0x0100000000000000000000000000000000000103" |
| 23 | + |
| 24 | +// safeTxBuilderTransaction is a single call within a Safe Transaction Builder batch. |
| 25 | +type safeTxBuilderTransaction struct { |
| 26 | + To string `json:"to"` |
| 27 | + Value string `json:"value"` |
| 28 | + Data string `json:"data"` |
| 29 | + ContractMethod interface{} `json:"contractMethod"` |
| 30 | + ContractInputsValues interface{} `json:"contractInputsValues"` |
| 31 | +} |
| 32 | + |
| 33 | +// safeTxBuilderBatch is the file format understood by Safe's Transaction Builder app. |
| 34 | +// See https://help.safe.global/en/articles/40841-transaction-builder. |
| 35 | +type safeTxBuilderBatch struct { |
| 36 | + Version string `json:"version"` |
| 37 | + ChainID string `json:"chainId"` |
| 38 | + CreatedAt int64 `json:"createdAt"` |
| 39 | + Meta struct { |
| 40 | + Name string `json:"name"` |
| 41 | + Description string `json:"description"` |
| 42 | + } `json:"meta"` |
| 43 | + Transactions []safeTxBuilderTransaction `json:"transactions"` |
| 44 | +} |
| 45 | + |
| 46 | +// exportSafe wraps an unsigned Oasis transaction into a calldata for the |
| 47 | +// Subcall precompile, and packages it for Safe. |
| 48 | +func exportSafe(network *config.Network, sigTx interface{}) ([]byte, error) { |
| 49 | + tx, ok := sigTx.(*types.Transaction) |
| 50 | + if !ok { |
| 51 | + return nil, fmt.Errorf("--format safe is only supported for unsigned ParaTime transactions") |
| 52 | + } |
| 53 | + if network == nil { |
| 54 | + return nil, fmt.Errorf("--format safe requires a network to be configured") |
| 55 | + } |
| 56 | + var chainID uint64 = 23294 // Use Safe on Sapphire Mainnet by default. |
| 57 | + if network.ChainContext != config.DefaultNetworks.All["mainnet"].ChainContext { |
| 58 | + chainID = 23295 // Sapphire Testnet |
| 59 | + } |
| 60 | + stringType, err := ethAbi.NewType("string", "", nil) |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + bytesType, err := ethAbi.NewType("bytes", "", nil) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + calldata, err := ethAbi.Arguments{{Type: stringType}, {Type: bytesType}}.Pack(string(tx.Call.Method), []byte(tx.Call.Body)) |
| 69 | + if err != nil { |
| 70 | + return nil, fmt.Errorf("failed to ABI-encode subcall: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + batch := safeTxBuilderBatch{ |
| 74 | + Version: "1.0", |
| 75 | + ChainID: strconv.FormatUint(chainID, 10), |
| 76 | + CreatedAt: time.Now().UnixMilli(), |
| 77 | + Transactions: []safeTxBuilderTransaction{{ |
| 78 | + To: subcallAddress, |
| 79 | + Value: "0", |
| 80 | + Data: "0x" + hex.EncodeToString(calldata), |
| 81 | + }}, |
| 82 | + } |
| 83 | + batch.Meta.Name = string(tx.Call.Method) |
| 84 | + batch.Meta.Description = fmt.Sprintf("Oasis CLI-generated Subcall wrapping '%s' for proposal via Safe.", tx.Call.Method) |
| 85 | + |
| 86 | + return json.MarshalIndent(batch, "", " ") |
| 87 | +} |
| 88 | + |
| 89 | +// DecodeSafe attempts to decode raw bytes as a Safe Transaction Builder batch |
| 90 | +// and unwraps Subcall precompile calldata back into an unsigned ParaTime |
| 91 | +// transaction. |
| 92 | +// |
| 93 | +// The resulting transaction only has Call.Method and Call.Body populated, since |
| 94 | +// the Safe batch does not carry sender, nonce, or fee information. |
| 95 | +func DecodeSafe(raw []byte) (*types.Transaction, error) { |
| 96 | + var batch safeTxBuilderBatch |
| 97 | + if err := json.Unmarshal(raw, &batch); err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + if batch.Version == "" || len(batch.Transactions) != 1 { |
| 101 | + return nil, fmt.Errorf("file not a Safe Transaction Builder batch with a single transaction") |
| 102 | + } |
| 103 | + |
| 104 | + txn := batch.Transactions[0] |
| 105 | + if !strings.EqualFold(txn.To, subcallAddress) { |
| 106 | + return nil, fmt.Errorf("transaction does not call the Subcall precompile at %s", subcallAddress) |
| 107 | + } |
| 108 | + |
| 109 | + calldata, err := hex.DecodeString(strings.TrimPrefix(txn.Data, "0x")) |
| 110 | + if err != nil { |
| 111 | + return nil, fmt.Errorf("malformed calldata: %w", err) |
| 112 | + } |
| 113 | + |
| 114 | + stringTy, err := ethAbi.NewType("string", "", nil) |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + bytesTy, err := ethAbi.NewType("bytes", "", nil) |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + vals, err := (ethAbi.Arguments{{Type: stringTy}, {Type: bytesTy}}).Unpack(calldata) |
| 123 | + if err != nil { |
| 124 | + return nil, fmt.Errorf("failed to ABI-decode subcall data: %w", err) |
| 125 | + } |
| 126 | + |
| 127 | + tx := &types.Transaction{Versioned: cbor.NewVersioned(types.LatestTransactionVersion)} |
| 128 | + tx.Call.Method = types.MethodName(vals[0].(string)) |
| 129 | + tx.Call.Body = vals[1].([]byte) |
| 130 | + tx.AuthInfo.Fee.Amount = types.NewBaseUnits(*quantity.NewFromUint64(0), types.NativeDenomination) |
| 131 | + return tx, nil |
| 132 | +} |
0 commit comments