forked from ethersphere/bee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoc.go
223 lines (190 loc) · 5.16 KB
/
soc.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package soc provides the single-owner chunk implementation
// and validator.
package soc
import (
"bytes"
"errors"
"github.com/ethersphere/bee/v2/pkg/cac"
"github.com/ethersphere/bee/v2/pkg/crypto"
"github.com/ethersphere/bee/v2/pkg/swarm"
)
var (
errInvalidAddress = errors.New("soc: invalid address")
errWrongChunkSize = errors.New("soc: chunk length is less than minimum")
)
// ID is a SOC identifier
type ID []byte
// SOC wraps a content-addressed chunk.
type SOC struct {
id ID
owner []byte // owner is the address in bytes of SOC owner.
signature []byte
chunk swarm.Chunk // wrapped chunk.
}
// New creates a new SOC representation from arbitrary id and
// a content-addressed chunk.
func New(id ID, ch swarm.Chunk) *SOC {
return &SOC{
id: id,
chunk: ch,
}
}
// NewSigned creates a single-owner chunk based on already signed data.
func NewSigned(id ID, ch swarm.Chunk, owner, sig []byte) (*SOC, error) {
s := New(id, ch)
if len(owner) != crypto.AddressSize {
return nil, errInvalidAddress
}
s.owner = owner
s.signature = sig
return s, nil
}
// Address returns the SOC chunk address.
func (s *SOC) Address() (swarm.Address, error) {
if len(s.owner) != crypto.AddressSize {
return swarm.ZeroAddress, errInvalidAddress
}
return CreateAddress(s.id, s.owner)
}
// WrappedChunk returns the chunk wrapped by the SOC.
func (s *SOC) WrappedChunk() swarm.Chunk {
return s.chunk
}
// Chunk returns the SOC chunk.
func (s *SOC) Chunk() (swarm.Chunk, error) {
socAddress, err := s.Address()
if err != nil {
return nil, err
}
return swarm.NewChunk(socAddress, s.toBytes()), nil
}
// Signature returns the SOC signature.
func (s *SOC) Signature() []byte {
return s.signature
}
// OwnerAddress returns the ethereum address of the SOC owner.
func (s *SOC) OwnerAddress() []byte {
return s.owner
}
// ID returns the SOC id.
func (s *SOC) ID() []byte {
return s.id
}
// toBytes is a helper function to convert the SOC data to bytes.
func (s *SOC) toBytes() []byte {
buf := bytes.NewBuffer(nil)
buf.Write(s.id)
buf.Write(s.signature)
buf.Write(s.chunk.Data())
return buf.Bytes()
}
// Sign signs a SOC using the given signer.
// It returns a signed SOC chunk ready for submission to the network.
func (s *SOC) Sign(signer crypto.Signer) (swarm.Chunk, error) {
// create owner
publicKey, err := signer.PublicKey()
if err != nil {
return nil, err
}
ownerAddressBytes, err := crypto.NewEthereumAddress(*publicKey)
if err != nil {
return nil, err
}
if len(ownerAddressBytes) != crypto.AddressSize {
return nil, errInvalidAddress
}
s.owner = ownerAddressBytes
// generate the data to sign
toSignBytes, err := hash(s.id, s.chunk.Address().Bytes())
if err != nil {
return nil, err
}
// sign the chunk
signature, err := signer.Sign(toSignBytes)
if err != nil {
return nil, err
}
s.signature = signature
return s.Chunk()
}
// UnwrapCAC extracts the CAC inside the SOC.
func UnwrapCAC(sch swarm.Chunk) (swarm.Chunk, error) {
chunkData := sch.Data()
if len(chunkData) < swarm.SocMinChunkSize {
return nil, errWrongChunkSize
}
cursor := swarm.HashSize + swarm.SocSignatureSize
ch, err := cac.NewWithDataSpan(chunkData[cursor:])
if err != nil {
return nil, err
}
return ch, nil
}
// FromChunk recreates a SOC representation from swarm.Chunk data.
func FromChunk(sch swarm.Chunk) (*SOC, error) {
chunkData := sch.Data()
if len(chunkData) < swarm.SocMinChunkSize {
return nil, errWrongChunkSize
}
// add all the data fields to the SOC
s := &SOC{}
cursor := 0
s.id = chunkData[cursor:swarm.HashSize]
cursor += swarm.HashSize
s.signature = chunkData[cursor : cursor+swarm.SocSignatureSize]
cursor += swarm.SocSignatureSize
ch, err := cac.NewWithDataSpan(chunkData[cursor:])
if err != nil {
return nil, err
}
toSignBytes, err := hash(s.id, ch.Address().Bytes())
if err != nil {
return nil, err
}
// recover owner information
recoveredOwnerAddress, err := recoverAddress(s.signature, toSignBytes)
if err != nil {
return nil, err
}
if len(recoveredOwnerAddress) != crypto.AddressSize {
return nil, errInvalidAddress
}
s.owner = recoveredOwnerAddress
s.chunk = ch
return s, nil
}
// CreateAddress creates a new SOC address from the id and
// the ethereum address of the owner.
func CreateAddress(id ID, owner []byte) (swarm.Address, error) {
sum, err := hash(id, owner)
if err != nil {
return swarm.ZeroAddress, err
}
return swarm.NewAddress(sum), nil
}
// hash hashes the given values in order.
func hash(values ...[]byte) ([]byte, error) {
h := swarm.NewHasher()
for _, v := range values {
_, err := h.Write(v)
if err != nil {
return nil, err
}
}
return h.Sum(nil), nil
}
// recoverAddress returns the ethereum address of the owner of a SOC.
func recoverAddress(signature, digest []byte) ([]byte, error) {
recoveredPublicKey, err := crypto.Recover(signature, digest)
if err != nil {
return nil, err
}
recoveredEthereumAddress, err := crypto.NewEthereumAddress(*recoveredPublicKey)
if err != nil {
return nil, err
}
return recoveredEthereumAddress, nil
}