-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathbundleItem.go
131 lines (120 loc) · 3.41 KB
/
bundleItem.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package goar
import (
"crypto/sha256"
"errors"
"io"
"github.com/everFinance/goar/types"
"github.com/everFinance/goar/utils"
"github.com/everFinance/goether"
)
type ItemSigner struct {
signType int
signer interface{}
owner string // only rsa has owner
signerAddr string
}
func NewItemSigner(signer interface{}) (*ItemSigner, error) {
signType, signerAddr, owner, err := reflectSigner(signer)
if err != nil {
return nil, err
}
return &ItemSigner{
signType: signType,
signer: signer,
owner: owner,
signerAddr: signerAddr,
}, nil
}
func (i *ItemSigner) CreateAndSignItem(data []byte, target string, anchor string, tags []types.Tag) (types.BundleItem, error) {
bundleItem, err := utils.NewBundleItem(i.owner, i.signType, target, anchor, data, tags)
if err != nil {
return types.BundleItem{}, err
}
// sign
if err := SignBundleItem(i.signType, i.signer, bundleItem); err != nil {
return types.BundleItem{}, err
}
// get itemBinary
itemBinary, err := utils.GenerateItemBinary(bundleItem)
if err != nil {
return types.BundleItem{}, err
}
bundleItem.ItemBinary = itemBinary
return *bundleItem, nil
}
func (i *ItemSigner) CreateAndSignNestedItem(target string, anchor string, tags []types.Tag, items ...types.BundleItem) (types.BundleItem, error) {
bundleTags := []types.Tag{
{Name: "Bundle-Format", Value: "binary"},
{Name: "Bundle-Version", Value: "2.0.0"},
}
tags = append(tags, bundleTags...)
bundle, err := utils.NewBundle(items...)
if err != nil {
return types.BundleItem{}, err
}
return i.CreateAndSignItem(bundle.BundleBinary, target, anchor, tags)
}
func (i *ItemSigner) CreateAndSignItemStream(data io.Reader, target string, anchor string, tags []types.Tag) (types.BundleItem, error) {
bundleItem, err := utils.NewBundleItemStream(i.owner, i.signType, target, anchor, data, tags)
if err != nil {
return types.BundleItem{}, err
}
// sign
if err := SignBundleItem(i.signType, i.signer, bundleItem); err != nil {
return types.BundleItem{}, err
}
if _, err := bundleItem.DataReader.Seek(0, 0); err != nil {
return types.BundleItem{}, err
}
return *bundleItem, nil
}
func reflectSigner(signer interface{}) (signType int, signerAddr, owner string, err error) {
if s, ok := signer.(*Signer); ok {
signType = types.ArweaveSignType
signerAddr = s.Address
owner = s.Owner()
return
}
if s, ok := signer.(*goether.Signer); ok {
signType = types.EthereumSignType
signerAddr = s.Address.String()
owner = utils.Base64Encode(s.GetPublicKey())
return
}
err = errors.New("not support this signer")
return
}
func SignBundleItem(signatureType int, signer interface{}, item *types.BundleItem) error {
signMsg, err := utils.BundleItemSignData(*item)
if err != nil {
return err
}
var sigData []byte
switch signatureType {
case types.ArweaveSignType:
arSigner, ok := signer.(*Signer)
if !ok {
return errors.New("signer must be goar signer")
}
sigData, err = utils.Sign(signMsg, arSigner.PrvKey)
if err != nil {
return err
}
case types.EthereumSignType:
ethSigner, ok := signer.(*goether.Signer)
if !ok {
return errors.New("signer not goether signer")
}
sigData, err = ethSigner.SignMsg(signMsg)
if err != nil {
return err
}
default:
// todo come soon support ed25519
return errors.New("not support this signType")
}
id := sha256.Sum256(sigData)
item.Id = utils.Base64Encode(id[:])
item.Signature = utils.Base64Encode(sigData)
return nil
}