/
signature.go
59 lines (53 loc) · 1.75 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
55
56
57
58
59
package signature
import (
"fmt"
"github.com/DSiSc/craft/types"
"github.com/DSiSc/validator/common"
"github.com/DSiSc/validator/tools"
"github.com/DSiSc/validator/tools/account"
"github.com/DSiSc/validator/tools/signature/keypair"
)
type Signature struct {
Scheme keypair.SignatureScheme
Value interface{}
}
// Sing the data by singer
func Sign(signer Signer, data []byte) ([]byte, error) {
// TODO: adding key
signatures := make([]byte, len(data))
address := signer.(*account.Account).Address
for i := 0; i < len(address); i++ {
signatures[i] = address[i] ^ data[i]
}
return signatures, nil
}
func verifySpecifiedAddress(pubKey []byte, signData []byte, validatorAddress types.Address) bool {
var decode = make([]byte, len(signData))
for i := 0; i < len(validatorAddress); i++ {
decode[i] = pubKey[i] ^ signData[i]
if validatorAddress[i] != decode[i] {
return false
}
}
return true
}
// Verify check the signature of data using pubKey
func Verify(pubKey keypair.PublicKey, signature []byte) (types.Address, error) {
var validators = []types.Address{
tools.HexToAddress("333c3310824b7c685133f2bedb2ca4b8b4df633d"),
tools.HexToAddress("343c3310824b7c685133f2bedb2ca4b8b4df633d"),
tools.HexToAddress("353c3310824b7c685133f2bedb2ca4b8b4df633d"),
tools.HexToAddress("363c3310824b7c685133f2bedb2ca4b8b4df633d"),
}
pkey := common.HashToByte(pubKey.(types.Hash))
for i := 0; i < len(validators); i++ {
if verifySpecifiedAddress(pkey, signature, validators[i]) {
return validators[i], nil
}
}
return *new(types.Address), fmt.Errorf("invalid signData")
}
// VerifyMultiSignature check whether more than m sigs are signed by the keys
func VerifyMultiSignature(data []byte, keys []keypair.PublicKey, m int, sigs [][]byte) error {
return nil
}