forked from wealdtech/go-ens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contenthash.go
236 lines (224 loc) · 6.61 KB
/
contenthash.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
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright 2019-2023 Weald Technology Trading.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ens
import (
"encoding/base32"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"fmt"
"strings"
"github.com/ipfs/go-cid"
"github.com/multiformats/go-multibase"
"github.com/multiformats/go-multihash"
"github.com/pkg/errors"
"github.com/wealdtech/go-multicodec"
)
// StringToContenthash turns EIP-1577 text format in to EIP-1577 binary format.
func StringToContenthash(text string) ([]byte, error) {
if text == "" {
return nil, errors.New("no content hash")
}
var codec string
var data string
if strings.Contains(text, "://") {
// URL style.
bits := strings.Split(text, "://")
if len(bits) != 2 {
return nil, fmt.Errorf("invalid content hash")
}
codec = bits[0]
data = bits[1]
} else {
// Path style.
bits := strings.Split(text, "/")
if len(bits) != 3 {
return nil, errors.New("invalid content hash")
}
codec = bits[1]
data = bits[2]
}
if codec == "" {
return nil, errors.New("codec missing")
}
if data == "" {
return nil, errors.New("data missing")
}
res := make([]byte, 0)
switch codec {
case "ipfs":
content, err := cid.Parse(data)
if err != nil {
return nil, errors.Wrap(err, "invalid IPFS data")
}
// Namespace.
buf := make([]byte, binary.MaxVarintLen64)
size := binary.PutUvarint(buf, multicodec.MustID("ipfs-ns"))
res = append(res, buf[0:size]...)
if data[0:2] == "Qm" {
// CID v0 needs additional headers.
size = binary.PutUvarint(buf, 1)
res = append(res, buf[0:size]...)
size = binary.PutUvarint(buf, multicodec.MustID("dag-pb"))
res = append(res, buf[0:size]...)
res = append(res, content.Bytes()...)
} else {
res = append(res, content.Bytes()...)
}
case "ipns":
content, err := cid.Parse(data)
if err != nil {
return nil, errors.Wrap(err, "invalid IPNS data")
}
// Namespace.
buf := make([]byte, binary.MaxVarintLen64)
size := binary.PutUvarint(buf, multicodec.MustID("ipns-ns"))
res = append(res, buf[0:size]...)
if data[0:2] == "Qm" {
// CID v0 needs additional headers.
size = binary.PutUvarint(buf, 1)
res = append(res, buf[0:size]...)
size = binary.PutUvarint(buf, multicodec.MustID("dag-pb"))
res = append(res, buf[0:size]...)
res = append(res, content.Bytes()...)
} else {
res = append(res, content.Bytes()...)
}
case "swarm", "bzz":
// Namespace.
buf := make([]byte, binary.MaxVarintLen64)
size := binary.PutUvarint(buf, multicodec.MustID("swarm-ns"))
res = append(res, buf[0:size]...)
size = binary.PutUvarint(buf, 1)
res = append(res, buf[0:size]...)
size = binary.PutUvarint(buf, multicodec.MustID("swarm-manifest"))
res = append(res, buf[0:size]...)
// Hash.
hashData, err := hex.DecodeString(data)
if err != nil {
return nil, errors.Wrap(err, "invalid hex")
}
hash, err := multihash.Encode(hashData, multihash.KECCAK_256)
if err != nil {
return nil, errors.Wrap(err, "failed to hash")
}
res = append(res, hash...)
case "onion":
// Codec.
buf := make([]byte, binary.MaxVarintLen64)
size := binary.PutUvarint(buf, multicodec.MustID("onion"))
res = append(res, buf[0:size]...)
// Address.
if len(data) != 16 {
return nil, errors.New("onion address should be 16 characters")
}
res = append(res, []byte(data)...)
case "onion3":
// Codec.
buf := make([]byte, binary.MaxVarintLen64)
size := binary.PutUvarint(buf, multicodec.MustID("onion3"))
res = append(res, buf[0:size]...)
// Address.
if len(data) != 56 {
return nil, errors.New("onion address should be 56 characters")
}
res = append(res, []byte(data)...)
case "sia":
// Codec.
buf := make([]byte, binary.MaxVarintLen64)
size := binary.PutUvarint(buf, multicodec.MustID("skynet-ns"))
res = append(res, buf[0:size]...)
// Skylink.
var err error
var decoded []byte
switch len(data) {
case 46:
decoded, err = base64.RawURLEncoding.DecodeString(data)
if err != nil {
return nil, errors.New("skylink not correctly encoded")
}
case 55:
decoded, err = base32.HexEncoding.WithPadding(base32.NoPadding).DecodeString(strings.ToUpper(data))
if err != nil {
return nil, errors.New("skylink not correctly encoded")
}
default:
return nil, errors.New("skylinks should be either 46 or 55 characters, depending on whether it is base64 or base32 encoded")
}
res = append(res, decoded...)
default:
return nil, fmt.Errorf("unknown codec %s", codec)
}
return res, nil
}
// ContenthashToString turns EIP-1577 binary format in to EIP-1577 text format.
func ContenthashToString(bytes []byte) (string, error) {
data, codec, err := multicodec.RemoveCodec(bytes)
if err != nil {
return "", err
}
codecName, err := multicodec.Name(codec)
if err != nil {
return "", err
}
switch codecName {
case "ipfs-ns":
thisCID, err := cid.Parse(data)
if err != nil {
return "", errors.Wrap(err, "failed to parse CID")
}
str, err := thisCID.StringOfBase(multibase.Base36)
if err != nil {
return "", errors.Wrap(err, "failed to obtain base36 representation")
}
return fmt.Sprintf("/ipfs/%s", str), nil
case "ipns-ns":
thisCID, err := cid.Parse(data)
if err != nil {
return "", errors.Wrap(err, "failed to parse CID")
}
res, err := multibase.Encode(multibase.Base36, thisCID.Bytes())
if err != nil {
return "", errors.Wrap(err, "unknown multibase")
}
return fmt.Sprintf("/ipns/%s", res), nil
case "swarm-ns":
id, offset := binary.Uvarint(data)
if id == 0 {
return "", fmt.Errorf("unknown CID")
}
data, subCodec, err := multicodec.RemoveCodec(data[offset:])
if err != nil {
return "", err
}
_, err = multicodec.Name(subCodec)
if err != nil {
return "", err
}
decodedMHash, err := multihash.Decode(data)
if err != nil {
return "", err
}
return fmt.Sprintf("bzz://%x", decodedMHash.Digest), nil
case "onion":
return fmt.Sprintf("onion://%s", string(data)), nil
case "onion3":
return fmt.Sprintf("onion3://%s", string(data)), nil
case "skynet-ns":
skylink := base64.RawURLEncoding.EncodeToString(data)
return fmt.Sprintf("sia://%s", skylink), nil
default:
return "", fmt.Errorf("unknown codec name %s", codecName)
}
}