mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
cmd, contracts: make peoples lives easier
This commit is contained in:
parent
f8e76bc085
commit
2d1fffe6b8
4 changed files with 62 additions and 5 deletions
|
|
@ -19,10 +19,13 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/cmd/utils"
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/contracts/ens"
|
||||||
"github.com/ethereum/go-ethereum/swarm/storage"
|
"github.com/ethereum/go-ethereum/swarm/storage"
|
||||||
"gopkg.in/urfave/cli.v1"
|
"gopkg.in/urfave/cli.v1"
|
||||||
)
|
)
|
||||||
|
|
@ -34,7 +37,33 @@ var hashCommand = cli.Command{
|
||||||
Usage: "print the swarm hash of a file or directory",
|
Usage: "print the swarm hash of a file or directory",
|
||||||
ArgsUsage: "<file>",
|
ArgsUsage: "<file>",
|
||||||
Description: "Prints the swarm hash of file or directory",
|
Description: "Prints the swarm hash of file or directory",
|
||||||
}
|
Subcommands: []cli.Command{
|
||||||
|
{
|
||||||
|
CustomHelpTemplate: helpTemplate,
|
||||||
|
Name: "ens",
|
||||||
|
Usage: "converts a swarm hash to an ens EIP1577 compatible CIDv1 hash",
|
||||||
|
ArgsUsage: "<ref>",
|
||||||
|
Description: "",
|
||||||
|
Subcommands: []cli.Command{
|
||||||
|
{
|
||||||
|
Action: encodeEipHash,
|
||||||
|
CustomHelpTemplate: helpTemplate,
|
||||||
|
Name: "contenthash",
|
||||||
|
Usage: "converts a swarm hash to an ens EIP1577 compatible CIDv1 hash",
|
||||||
|
ArgsUsage: "<ref>",
|
||||||
|
Description: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Action: ensNodeHash,
|
||||||
|
CustomHelpTemplate: helpTemplate,
|
||||||
|
Name: "node",
|
||||||
|
Usage: "converts an ens name to an ENS node hash",
|
||||||
|
ArgsUsage: "<ref>",
|
||||||
|
Description: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
|
||||||
func hash(ctx *cli.Context) {
|
func hash(ctx *cli.Context) {
|
||||||
args := ctx.Args()
|
args := ctx.Args()
|
||||||
|
|
@ -56,3 +85,31 @@ func hash(ctx *cli.Context) {
|
||||||
fmt.Printf("%v\n", addr)
|
fmt.Printf("%v\n", addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func ensNodeHash(ctx *cli.Context) {
|
||||||
|
args := ctx.Args()
|
||||||
|
if len(args) < 1 {
|
||||||
|
utils.Fatalf("Usage: swarm hash ens node <ens name>")
|
||||||
|
}
|
||||||
|
ensName := args[0]
|
||||||
|
|
||||||
|
hash := ens.EnsNode(ensName)
|
||||||
|
|
||||||
|
stringHex := hex.EncodeToString(hash[:])
|
||||||
|
fmt.Println(stringHex)
|
||||||
|
}
|
||||||
|
func encodeEipHash(ctx *cli.Context) {
|
||||||
|
args := ctx.Args()
|
||||||
|
if len(args) < 1 {
|
||||||
|
utils.Fatalf("Usage: swarm hash ens <swarm hash>")
|
||||||
|
}
|
||||||
|
swarmHash := args[0]
|
||||||
|
|
||||||
|
hash := common.HexToHash(swarmHash)
|
||||||
|
ensHash, err := ens.EncodeSwarmHash(hash)
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatalf("error converting swarm hash", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
stringHex := hex.EncodeToString(ensHash)
|
||||||
|
fmt.Println(stringHex)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -99,7 +99,7 @@ func extractContentHash(buf []byte) (common.Hash, error) {
|
||||||
return common.BytesToHash(buf), nil
|
return common.BytesToHash(buf), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func encodeSwarmHash(hash common.Hash) ([]byte, error) {
|
func EncodeSwarmHash(hash common.Hash) ([]byte, error) {
|
||||||
var cidBytes []byte
|
var cidBytes []byte
|
||||||
var headerBytes = []byte{
|
var headerBytes = []byte{
|
||||||
nsSwarm, //swarm namespace
|
nsSwarm, //swarm namespace
|
||||||
|
|
|
||||||
|
|
@ -132,7 +132,7 @@ func TestManualCidDecode(t *testing.T) {
|
||||||
func TestManuelCidEncode(t *testing.T) {
|
func TestManuelCidEncode(t *testing.T) {
|
||||||
// call cid encode method with hash. expect byte slice returned, compare according to spec
|
// call cid encode method with hash. expect byte slice returned, compare according to spec
|
||||||
const eipHash = "29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f"
|
const eipHash = "29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f"
|
||||||
cidBytes, err := encodeSwarmHash(common.HexToHash(eipHash))
|
cidBytes, err := EncodeSwarmHash(common.HexToHash(eipHash))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -59,14 +59,14 @@ func TestENS(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("can't deploy resolver: %v", err)
|
t.Fatalf("can't deploy resolver: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := ens.SetResolver(EnsNode(name), resolverAddr); err != nil {
|
if _, err := ens.SetResolver(EnsNode(name), resolverAddr); err != nil {
|
||||||
t.Fatalf("can't set resolver: %v", err)
|
t.Fatalf("can't set resolver: %v", err)
|
||||||
}
|
}
|
||||||
contractBackend.Commit()
|
contractBackend.Commit()
|
||||||
|
|
||||||
// Set the content hash for the name.
|
// Set the content hash for the name.
|
||||||
|
cid, err := EncodeSwarmHash(hash)
|
||||||
cid, err := encodeSwarmHash(hash)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue