Skip to content

Commit e7d1867

Browse files
acudzelig
authored andcommitted
contracts, swarm: implement EIP-1577 (#19285)
* contracts/ens: update public resolver solidity code * contracts/ens: update public resolver, update go bindings * update build * fix ens.sol * contracts/ens: change contract interface * contracts/ens: implement public resolver changes * contracts/ens: added ENSRegistry contract * contracts/ens: reinstate old contract code * contracts/ens: update README.md * contracts/ens: added test coverage for fallback contract * contracts/ens: added support for fallback contract * contracts/ens: removed unused contract code * contracts/ens: add todo and decode multicodec stub * add encode * vendor: add ipfs cid libraries * contracts/ens: cid sanity tests * contracts/ens: more cid sanity checks * contracts/ens: wip integration * wip * Revert "vendor: add ipfs cid libraries" This reverts commit 29d9b6b294ded903a1065d96c8149119713cfd12. * contracts/ens: removed multiformats dependencies * contracts/ens: added decode tests * contracts/ens: added eip spec test, minor changes to exiting tests * contracts/ens: moved cid decoding to own file * contracts/ens: added unit test to encode hash to content hash * contracts/ens: removed unused code * contracts/ens: fix ens tests to use cid decode and encode * contracts/ens: adjust swarm multicodecs after pr merge * contracts/ens: fix linter error * constracts/ens: address PR comments * cmd, contracts: make peoples lives easier * contracts/ens: fix linter error * contracts/ens: address PR comments
1 parent fb45828 commit e7d1867

17 files changed

Lines changed: 3336 additions & 434 deletions

‎cmd/swarm/hash.go‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ package main
1919

2020
import (
2121
"context"
22+
"encoding/hex"
2223
"fmt"
2324
"os"
2425

2526
"github.com/ethereum/go-ethereum/cmd/utils"
27+
"github.com/ethereum/go-ethereum/common"
28+
"github.com/ethereum/go-ethereum/contracts/ens"
2629
"github.com/ethereum/go-ethereum/swarm/storage"
2730
"gopkg.in/urfave/cli.v1"
2831
)
@@ -34,7 +37,33 @@ var hashCommand = cli.Command{
3437
Usage: "print the swarm hash of a file or directory",
3538
ArgsUsage: "<file>",
3639
Description: "Prints the swarm hash of file or directory",
37-
}
40+
Subcommands: []cli.Command{
41+
{
42+
CustomHelpTemplate: helpTemplate,
43+
Name: "ens",
44+
Usage: "converts a swarm hash to an ens EIP1577 compatible CIDv1 hash",
45+
ArgsUsage: "<ref>",
46+
Description: "",
47+
Subcommands: []cli.Command{
48+
{
49+
Action: encodeEipHash,
50+
CustomHelpTemplate: helpTemplate,
51+
Name: "contenthash",
52+
Usage: "converts a swarm hash to an ens EIP1577 compatible CIDv1 hash",
53+
ArgsUsage: "<ref>",
54+
Description: "",
55+
},
56+
{
57+
Action: ensNodeHash,
58+
CustomHelpTemplate: helpTemplate,
59+
Name: "node",
60+
Usage: "converts an ens name to an ENS node hash",
61+
ArgsUsage: "<ref>",
62+
Description: "",
63+
},
64+
},
65+
},
66+
}}
3867

3968
func hash(ctx *cli.Context) {
4069
args := ctx.Args()
@@ -56,3 +85,31 @@ func hash(ctx *cli.Context) {
5685
fmt.Printf("%v\n", addr)
5786
}
5887
}
88+
func ensNodeHash(ctx *cli.Context) {
89+
args := ctx.Args()
90+
if len(args) < 1 {
91+
utils.Fatalf("Usage: swarm hash ens node <ens name>")
92+
}
93+
ensName := args[0]
94+
95+
hash := ens.EnsNode(ensName)
96+
97+
stringHex := hex.EncodeToString(hash[:])
98+
fmt.Println(stringHex)
99+
}
100+
func encodeEipHash(ctx *cli.Context) {
101+
args := ctx.Args()
102+
if len(args) < 1 {
103+
utils.Fatalf("Usage: swarm hash ens <swarm hash>")
104+
}
105+
swarmHash := args[0]
106+
107+
hash := common.HexToHash(swarmHash)
108+
ensHash, err := ens.EncodeSwarmHash(hash)
109+
if err != nil {
110+
utils.Fatalf("error converting swarm hash", err)
111+
}
112+
113+
stringHex := hex.EncodeToString(ensHash)
114+
fmt.Println(stringHex)
115+
}

‎contracts/ens/README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,13 @@ The go bindings for ENS contracts are generated using `abigen` via the go genera
1818
```shell
1919
go generate ./contracts/ens
2020
```
21+
22+
## Fallback contract support
23+
24+
In order to better support content resolution on different service providers (such as Swarm and IPFS), [EIP-1577](https://eips.ethereum.org/EIPS/eip-1577)
25+
was introduced and with it changes that allow applications to know _where_ content hashes are stored (i.e. if the
26+
requested hash resides on Swarm or IPFS).
27+
28+
The code under `contracts/ens/contract` reflects the new Public Resolver changes and the code under `fallback_contract` allows
29+
us to support the old contract resolution in cases where the ENS name owner did not update her Resolver contract, until the migration
30+
period ends (date arbitrarily set to June 1st, 2019).

‎contracts/ens/cid.go‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Copyright 2016 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package ens
18+
19+
import (
20+
"encoding/binary"
21+
"errors"
22+
"fmt"
23+
24+
"github.com/ethereum/go-ethereum/common"
25+
)
26+
27+
const (
28+
cidv1 = 0x1
29+
30+
nsIpfs = 0xe3
31+
nsSwarm = 0xe4
32+
33+
swarmTypecode = 0xfa //swarm manifest, see https://github.com/multiformats/multicodec/blob/master/table.csv
34+
swarmHashtype = 0xd6 // BMT, see https://github.com/multiformats/multicodec/blob/master/table.csv
35+
36+
hashLength = 32
37+
)
38+
39+
// deocodeEIP1577ContentHash decodes a chain-stored content hash from an ENS record according to EIP-1577
40+
// a successful decode will result the different parts of the content hash in accordance to the CID spec
41+
// Note: only CIDv1 is supported
42+
func decodeEIP1577ContentHash(buf []byte) (storageNs, contentType, hashType, hashLength uint64, hash []byte, err error) {
43+
if len(buf) < 10 {
44+
return 0, 0, 0, 0, nil, errors.New("buffer too short")
45+
}
46+
47+
storageNs, n := binary.Uvarint(buf)
48+
49+
buf = buf[n:]
50+
vers, n := binary.Uvarint(buf)
51+
52+
if vers != 1 {
53+
return 0, 0, 0, 0, nil, fmt.Errorf("expected cid v1, got: %d", vers)
54+
}
55+
buf = buf[n:]
56+
contentType, n = binary.Uvarint(buf)
57+
58+
buf = buf[n:]
59+
hashType, n = binary.Uvarint(buf)
60+
61+
buf = buf[n:]
62+
hashLength, n = binary.Uvarint(buf)
63+
64+
hash = buf[n:]
65+
66+
if len(hash) != int(hashLength) {
67+
return 0, 0, 0, 0, nil, errors.New("hash length mismatch")
68+
}
69+
return storageNs, contentType, hashType, hashLength, hash, nil
70+
}
71+
72+
func extractContentHash(buf []byte) (common.Hash, error) {
73+
storageNs, _ /*contentType*/, _ /* hashType*/, decodedHashLength, hashBytes, err := decodeEIP1577ContentHash(buf)
74+
75+
if err != nil {
76+
return common.Hash{}, err
77+
}
78+
79+
if storageNs != nsSwarm {
80+
return common.Hash{}, errors.New("unknown storage system")
81+
}
82+
83+
//todo: for the time being we implement loose enforcement for the EIP rules until ENS manager is updated
84+
/*if contentType != swarmTypecode {
85+
return common.Hash{}, errors.New("unknown content type")
86+
}
87+
88+
if hashType != swarmHashtype {
89+
return common.Hash{}, errors.New("unknown multihash type")
90+
}*/
91+
92+
if decodedHashLength != hashLength {
93+
return common.Hash{}, errors.New("odd hash length, swarm expects 32 bytes")
94+
}
95+
96+
if len(hashBytes) != int(hashLength) {
97+
return common.Hash{}, errors.New("hash length mismatch")
98+
}
99+
100+
return common.BytesToHash(buf), nil
101+
}
102+
103+
func EncodeSwarmHash(hash common.Hash) ([]byte, error) {
104+
var cidBytes []byte
105+
var headerBytes = []byte{
106+
nsSwarm, //swarm namespace
107+
cidv1, // CIDv1
108+
swarmTypecode, // swarm hash
109+
swarmHashtype, // swarm bmt hash
110+
hashLength, //hash length. 32 bytes
111+
}
112+
113+
varintbuf := make([]byte, binary.MaxVarintLen64)
114+
for _, v := range headerBytes {
115+
n := binary.PutUvarint(varintbuf, uint64(v))
116+
cidBytes = append(cidBytes, varintbuf[:n]...)
117+
}
118+
119+
cidBytes = append(cidBytes, hash[:]...)
120+
return cidBytes, nil
121+
}

‎contracts/ens/cid_test.go‎

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
// Copyright 2016 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package ens
18+
19+
import (
20+
"bytes"
21+
"encoding/binary"
22+
"encoding/hex"
23+
"fmt"
24+
"testing"
25+
26+
"github.com/ethereum/go-ethereum/common"
27+
)
28+
29+
// Tests for the decoding of the example ENS
30+
func TestEIPSpecCidDecode(t *testing.T) {
31+
const (
32+
eipSpecHash = "e3010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f"
33+
eipHash = "29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f"
34+
dagPb = 0x70
35+
sha2256 = 0x12
36+
)
37+
b, err := hex.DecodeString(eipSpecHash)
38+
if err != nil {
39+
t.Fatal(err)
40+
}
41+
hashBytes, err := hex.DecodeString(eipHash)
42+
43+
if err != nil {
44+
t.Fatal(err)
45+
}
46+
47+
storageNs, contentType, hashType, hashLength, decodedHashBytes, err := decodeEIP1577ContentHash(b)
48+
49+
if err != nil {
50+
t.Fatal(err)
51+
}
52+
if storageNs != nsIpfs {
53+
t.Fatal("wrong ns")
54+
}
55+
if contentType != dagPb {
56+
t.Fatal("should be ipfs typecode")
57+
}
58+
if hashType != sha2256 {
59+
t.Fatal("should be sha2-256")
60+
}
61+
if hashLength != 32 {
62+
t.Fatal("should be 32")
63+
}
64+
if !bytes.Equal(hashBytes, decodedHashBytes) {
65+
t.Fatal("should be equal")
66+
}
67+
68+
}
69+
func TestManualCidDecode(t *testing.T) {
70+
// call cid encode method with hash. expect byte slice returned, compare according to spec
71+
72+
for _, v := range []struct {
73+
name string
74+
headerBytes []byte
75+
wantErr bool
76+
}{
77+
{
78+
name: "values correct, should not fail",
79+
headerBytes: []byte{0xe4, 0x01, 0xfa, 0xd6, 0x20},
80+
wantErr: false,
81+
},
82+
{
83+
name: "cid version wrong, should fail",
84+
headerBytes: []byte{0xe4, 0x00, 0xfa, 0xd6, 0x20},
85+
wantErr: true,
86+
},
87+
{
88+
name: "hash length wrong, should fail",
89+
headerBytes: []byte{0xe4, 0x01, 0xfa, 0xd6, 0x1f},
90+
wantErr: true,
91+
},
92+
{
93+
name: "values correct for ipfs, should fail",
94+
headerBytes: []byte{0xe3, 0x01, 0x70, 0x12, 0x20},
95+
wantErr: true,
96+
},
97+
{
98+
name: "loose values for swarm, todo remove, should not fail",
99+
headerBytes: []byte{0xe4, 0x01, 0x70, 0x12, 0x20},
100+
wantErr: false,
101+
},
102+
{
103+
name: "loose values for swarm, todo remove, should not fail",
104+
headerBytes: []byte{0xe4, 0x01, 0x99, 0x99, 0x20},
105+
wantErr: false,
106+
},
107+
} {
108+
t.Run(v.name, func(t *testing.T) {
109+
const eipHash = "29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f"
110+
111+
var bb []byte
112+
buf := make([]byte, binary.MaxVarintLen64)
113+
for _, vv := range v.headerBytes {
114+
n := binary.PutUvarint(buf, uint64(vv))
115+
bb = append(bb, buf[:n]...)
116+
}
117+
118+
h := common.HexToHash(eipHash)
119+
bb = append(bb, h[:]...)
120+
str := hex.EncodeToString(bb)
121+
fmt.Println(str)
122+
decodedHash, e := extractContentHash(bb)
123+
switch v.wantErr {
124+
case true:
125+
if e == nil {
126+
t.Fatal("the decode should fail")
127+
}
128+
case false:
129+
if e != nil {
130+
t.Fatalf("the deccode shouldnt fail: %v", e)
131+
}
132+
if !bytes.Equal(decodedHash[:], h[:]) {
133+
t.Fatal("hashes not equal")
134+
}
135+
}
136+
})
137+
}
138+
}
139+
140+
func TestManuelCidEncode(t *testing.T) {
141+
// call cid encode method with hash. expect byte slice returned, compare according to spec
142+
const eipHash = "29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f"
143+
cidBytes, err := EncodeSwarmHash(common.HexToHash(eipHash))
144+
if err != nil {
145+
t.Fatal(err)
146+
}
147+
148+
// logic in extractContentHash is unit tested thoroughly
149+
// hence we just check that the returned hash is equal
150+
h, err := extractContentHash(cidBytes)
151+
if err != nil {
152+
t.Fatal(err)
153+
}
154+
155+
if bytes.Equal(h[:], cidBytes) {
156+
t.Fatal("should be equal")
157+
}
158+
}

‎contracts/ens/contract/AbstractENS.sol‎

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)