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

Example txn signing #32

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ Writings:
```bash
# Clone this repository
git clone https://github.com/flashbots/suapp-examples.git

# Checkout the suave-geth submodule
git submodule init
git submodule update
```

---
Expand All @@ -38,6 +34,12 @@ curl -L https://foundry.paradigm.xyz | bash
foundryup
```

Install dependencies:

```bash
forge install
```

Compile:

```bash
Expand Down
17 changes: 17 additions & 0 deletions examples/std-transaction-signing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Example Suapp to sign a transaction

This example shows how to use the `suave-std` library to create and sign transactions with the `signTxn` method. Internally, the method uses the `signMessage` precompile available in the Suave MEVM.

## How to use

Run `Suave` in development mode:

```
$ suave --suave.dev
```

Execute the deployment script:

```
$ go run main.go
```
27 changes: 27 additions & 0 deletions examples/std-transaction-signing/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"encoding/hex"
"log"

"github.com/flashbots/suapp-examples/framework"
)

func main() {
fr := framework.New()
priv := "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291"

contract := fr.Suave.DeployContract("transaction-signing.sol/TransactionSigning.json")
receipt := contract.SendTransaction("example", nil, []byte(priv))

// validate the signature
txnSignatureEvent, _ := contract.Abi.Events["TxnSignature"].ParseLog(receipt.Logs[0])
ferranbt marked this conversation as resolved.
Show resolved Hide resolved
var r, s = txnSignatureEvent["r"].([32]byte), txnSignatureEvent["s"].([32]byte)

if hex.EncodeToString(r[:]) != "eebcfac0def6db5649d0ae6b52ed3b8ba1f5c6c428588df125461113ba8c6749" {
log.Fatal("wrong r signature")
}
if hex.EncodeToString(s[:]) != "5d5e1aafa0c964b43c251b6a525d49572968f2cebc5868c58bcc9281b9a07505" {
log.Fatal("wrong s signature")
}
}
32 changes: 32 additions & 0 deletions examples/std-transaction-signing/transaction-signing.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
pragma solidity ^0.8.8;

import "suave-std/suavelib/Suave.sol";
import "suave-std/Transactions.sol";

contract TransactionSigning {
using Transactions for *;

event TxnSignature(bytes32 r, bytes32 s);

function callback(bytes32 r, bytes32 s) public payable {
emit TxnSignature(r, s);
}

function example() public payable returns (bytes memory) {
string memory signingKey = string(Suave.confidentialInputs());

Transactions.EIP155Request memory txnWithToAddress = Transactions.EIP155Request({
to: address(0x00000000000000000000000000000000DeaDBeef),
gas: 1000000,
gasPrice: 500,
value: 1,
nonce: 1,
data: bytes(""),
chainId: 1337
});

Transactions.EIP155 memory txn = Transactions.signTxn(txnWithToAddress, string(signingKey));

return abi.encodeWithSelector(this.callback.selector, txn.r, txn.s);
}
}
12 changes: 6 additions & 6 deletions framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ type Contract struct {
kettleAddr common.Address

addr common.Address
abi *abi.ABI
Abi *abi.ABI
}

func (c *Contract) Call(methodName string) []interface{} {
input, err := c.abi.Pack(methodName)
input, err := c.Abi.Pack(methodName)
if err != nil {
panic(err)
}
Expand All @@ -131,7 +131,7 @@ func (c *Contract) Call(methodName string) []interface{} {
panic(err)
}

results, err := c.abi.Methods[methodName].Outputs.Unpack(output)
results, err := c.Abi.Methods[methodName].Outputs.Unpack(output)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -251,16 +251,16 @@ func (c *Chain) DeployContract(path string) *Contract {
log.Printf("deployed contract at %s", receipt.ContractAddress.Hex())

contract := sdk.GetContract(receipt.ContractAddress, artifact.Abi, c.clt)
return &Contract{addr: receipt.ContractAddress, clt: c.clt, kettleAddr: c.kettleAddr, abi: artifact.Abi, Contract: contract}
return &Contract{addr: receipt.ContractAddress, clt: c.clt, kettleAddr: c.kettleAddr, Abi: artifact.Abi, Contract: contract}
}

func (c *Contract) Ref(acct *PrivKey) *Contract {
clt := sdk.NewClient(c.clt.RPC().Client(), acct.Priv, c.kettleAddr)

cc := &Contract{
addr: c.addr,
abi: c.abi,
Contract: sdk.GetContract(c.addr, c.abi, clt),
Abi: c.Abi,
Contract: sdk.GetContract(c.addr, c.Abi, clt),
}
return cc
}
Expand Down
Loading