The following code segment demonstrates a Golang implementation for transaction cancellation.
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
"math/big"
)
var privateKey = "01833a126ec45d0191519748146b9e35647aab7fed28de1c8e17824970f964a3"
type CancellationMessage struct {
ChainID *big.Int
Nonce uint64
}
type Signature struct {
R string
S string
V uint64
}
func SignMessage(msg *CancellationMessage) (*Signature, error) {
encoded, err := rlp.EncodeToBytes(msg)
if err != nil {
return nil, err
}
hash := crypto.Keccak256(encoded)
fmt.Printf("Signature Hash: %s\n", common.BytesToHash(hash))
key, err := crypto.HexToECDSA(privateKey)
if err != nil {
return nil, err
}
sign, err := crypto.Sign(hash, key)
if err != nil {
return nil, err
}
fmt.Printf("Signature: 0x%x\n", sign)
fmt.Printf("Raw bytes: %v\n", sign)
r := sign[:32]
s := sign[32:64]
v := sign[64]
return &Signature{
R: common.BytesToHash(r).Hex(),
S: common.BytesToHash(s).Hex(),
V: uint64(v),
}, nil
}
func main() {
cancel := &CancellationMessage{
ChainID: big.NewInt(1212101),
Nonce: 0,
}
signature, err := SignMessage(cancel)
if err != nil {
panic(fmt.Sprintf("sign cancel msg error: %v", err))
}
fmt.Printf("Cancellation msg Signature: %v\n", signature)
}