/
signature.go
54 lines (43 loc) · 1.2 KB
/
signature.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package starknet
import (
"fmt"
"math/big"
"google.golang.org/protobuf/proto"
"github.com/O1MaGnUmO1/chainlink-common/pkg/loop/internal/pb"
)
type Signature struct {
sig *pb.StarknetSignature
}
func (s *Signature) Bytes() ([]byte, error) {
return proto.Marshal(s.sig)
}
func (s *Signature) Ints() (x *big.Int, y *big.Int, err error) {
if s.sig == nil {
return nil, nil, fmt.Errorf("signature uninitialized")
}
return s.sig.X.Int(), s.sig.Y.Int(), nil
}
// b is expected to encode x,y components in accordance with [signature.Bytes]
func SignatureFromBytes(b []byte) (*Signature, error) {
starkPb := &pb.StarknetSignature{}
err := proto.Unmarshal(b, starkPb)
if err != nil {
return nil, fmt.Errorf("error unmarshaling bytes to signature: %w", err)
}
return &Signature{
sig: starkPb,
}, nil
}
// x,y must be non-negative numbers
func SignatureFromBigInts(x *big.Int, y *big.Int) (*Signature, error) {
if x.Cmp(big.NewInt(0)) < 0 || y.Cmp(big.NewInt(0)) < 0 {
return nil, fmt.Errorf("Cannot create signature from negative values (x,y), (%v, %v)", x, y)
}
starkPb := &pb.StarknetSignature{
X: pb.NewBigIntFromInt(x),
Y: pb.NewBigIntFromInt(y),
}
return &Signature{
sig: starkPb,
}, nil
}