forked from rubblelabs/ripple
/
signing.go
66 lines (62 loc) · 1.7 KB
/
signing.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
55
56
57
58
59
60
61
62
63
64
65
66
package data
import (
"github.com/RaghavSood/ripple/crypto"
)
func Sign(s SignerAgent, key crypto.Key, sequence *uint32) error {
s.InitialiseForSigning()
copy(s.GetPublicKey().Bytes(), key.Public(sequence))
hash, msg, err := SigningHash(s, nil)
if err != nil {
return err
}
sig, err := crypto.Sign(key.Private(sequence), hash.Bytes(), append(s.SigningPrefix().Bytes(), msg...))
if err != nil {
return err
}
*s.GetSignature() = VariableLength(sig)
hash, _, err = Raw(s)
if err != nil {
return err
}
copy(s.GetHash().Bytes(), hash.Bytes())
return nil
}
func SignFor(s SignerAgent, key crypto.Key, sequence *uint32) error {
s.InitialiseForMultiSigning()
hash, msg, err := SigningHash(s, key.Id(sequence))
if err != nil {
return err
}
sig, err := crypto.Sign(key.Private(sequence), hash.Bytes(), append(s.SigningPrefix().Bytes(), msg...))
if err != nil {
return err
}
signer := &Signer{
Signer: struct {
Account *Account `json:",omitempty"`
SigningPubKey *PublicKey `json:",omitempty"`
TxnSignature *VariableLength `json:",omitempty"`
}{
Account: new(Account),
SigningPubKey: new(PublicKey),
TxnSignature: new(VariableLength),
},
}
copy(signer.Signer.Account[:], key.Id(sequence))
copy(signer.Signer.SigningPubKey.Bytes(), key.Public(sequence))
*signer.Signer.TxnSignature = VariableLength(sig)
s.AddSignature(signer)
hash, _, err = Raw(s)
if err != nil {
return err
}
copy(s.GetHash().Bytes(), hash.Bytes())
return nil
}
func CheckSignature(s SignerAgent) (bool, error) {
hash, msg, err := SigningHash(s, nil)
if err != nil {
return false, err
}
return crypto.Verify(s.GetPublicKey().Bytes(), hash.Bytes(), msg, s.GetSignature().Bytes())
}