Skip to content

Commit

Permalink
Merge branch 'tilen/signature_encoding' into 'main'
Browse files Browse the repository at this point in the history
Changing signature encoding.

See merge request flarenetwork/fast-updates!29
  • Loading branch information
tilenflare committed Jun 11, 2024
2 parents 825c958 + 1cd37fc commit 703aaa2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
5 changes: 3 additions & 2 deletions go-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 19 additions & 7 deletions go-client/keygen/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand All @@ -41,9 +43,7 @@ type keyEncrypted struct {
}

type sigStrings struct {
RX string
RY string
S string
Signature string
}

func main() {
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 703aaa2

Please sign in to comment.