From 1cd37fcde7e9497ed703af46f7067e771cfbfc09 Mon Sep 17 00:00:00 2001 From: Tilen Marc Date: Tue, 11 Jun 2024 11:51:22 +0200 Subject: [PATCH] Changing signature encoding. --- go-client/README.md | 5 +++-- go-client/keygen/keygen.go | 26 +++++++++++++++++++------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/go-client/README.md b/go-client/README.md index ae624c7..8020ee6 100644 --- a/go-client/README.md +++ b/go-client/README.md @@ -155,8 +155,9 @@ to be replaced by the actual address that will be used to sign the updates. Alternatively, one can save and read the (encrypted) key from a file and save the signature with: ```bash -go run keygen/keygen.go --key_out keys.out --pass secret_password -go run keygen/keygen.go --key_file keys.out --pass secret_password --address 0xd4e934C2749CA8C1618659D02E7B28B074bf4df7 --sig_out sig.out +go run keygen/keygen.go --key_out keys.out --pass secret_password # generate and save a key to file +go run keygen/keygen.go --key_file keys.out --pass secret_password --address 0xd4e934C2749CA8C1618659D02E7B28B074bf4df7 --sig_out sig.out # read and decrypt a key from a file, sign and write the signature to a file +go run keygen/keygen.go --key_file keys.out --pass secret_password # read and decrypt a key from file, just print it out ``` where the address value needs to be replaced by the actual address that will be used to sign diff --git a/go-client/keygen/keygen.go b/go-client/keygen/keygen.go index 1ce6e9e..5854b12 100644 --- a/go-client/keygen/keygen.go +++ b/go-client/keygen/keygen.go @@ -18,6 +18,7 @@ import ( "github.com/consensys/gnark-crypto/ecc/bn254" "github.com/consensys/gnark-crypto/ecc/bn254/fp" + "github.com/ethereum/go-ethereum/accounts/abi" "golang.org/x/crypto/scrypt" ) @@ -27,6 +28,7 @@ var InFileFlag = flag.String("key_file", "", "File to load private and public ke var KeyOutFlag = flag.String("key_out", "", "File to save a freshly generated private and public key") var PassFlag = flag.String("pass", "", "Password for encrypting/decrypting private key") var SigOutFlag = flag.String("sig_out", "", "File to save a signature") +var uint256Ty, _ = abi.NewType("uint256", "uint256", nil) type keyStrings struct { PublicKeyX string @@ -41,9 +43,7 @@ type keyEncrypted struct { } type sigStrings struct { - RX string - RY string - S string + Signature string } func main() { @@ -170,21 +170,33 @@ func main() { if err != nil { log.Fatal(err) } - sigStrings := sigStrings{S: "0x" + signature.S.Text(16), RX: "0x" + signature.R.X.Text(16), RY: "0x" + signature.R.Y.Text(16)} - sigBytes, err := json.Marshal(sigStrings) + + arguments := abi.Arguments{ + {Type: uint256Ty}, {Type: uint256Ty}, {Type: uint256Ty}, + } + sigBytes, err := arguments.Pack( + signature.S, + signature.R.X.BigInt(new(big.Int)), + signature.R.Y.BigInt(new(big.Int)), + ) if err != nil { log.Fatal(err) } if *SigOutFlag == "" { - logger.Info("Signature generated: " + string(sigBytes)) + logger.Info("Signature generated: 0x" + hex.EncodeToString(sigBytes)) } else { f, err := os.Create(*SigOutFlag) if err != nil { log.Fatal(err) } - _, err = f.Write(sigBytes) + sigStruct := sigStrings{Signature: "0x" + hex.EncodeToString(sigBytes)} + sigToWrite, err := json.Marshal(sigStruct) + if err != nil { + log.Fatal(err) + } + _, err = f.Write(sigToWrite) if err != nil { log.Fatal(err) }