From 39ece86484909a6fdb29d7b8e0abedff1e521856 Mon Sep 17 00:00:00 2001 From: Kerem Date: Thu, 27 Apr 2017 19:45:18 +0300 Subject: [PATCH 1/3] add PeerID lookup enhance key lookup logic and enable PeerID lookup in addition to the key name License: MIT Signed-off-by: Kerem Gocen --- core/commands/publish.go | 42 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/core/commands/publish.go b/core/commands/publish.go index 63462950908..70eca056ec7 100644 --- a/core/commands/publish.go +++ b/core/commands/publish.go @@ -48,6 +48,11 @@ Publish an with another name, added by an 'ipfs key' command: > ipfs name publish --key=mykey /ipfs/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy Published to QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n: /ipfs/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy +Alternatively, publish an using a valid PeerID(as listed by 'ipfs key list -l'): + + > ipfs name publish --key=QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n /ipfs/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy + Published to QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n: /ipfs/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy + `, }, @@ -61,7 +66,7 @@ Publish an with another name, added by an 'ipfs key' command: This accepts durations such as "300s", "1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".`).Default("24h"), cmds.StringOption("ttl", "Time duration this record should be cached for (caution: experimental)."), - cmds.StringOption("key", "k", "Name of the key to be used, as listed by 'ipfs key list'. Default: <>.").Default("self"), + cmds.StringOption("key", "k", "Name of the key to be used or a valid PeerID, as listed by 'ipfs key list -l'. Default: <>.").Default("self"), }, Run: func(req cmds.Request, res cmds.Response) { log.Debug("begin publish") @@ -116,7 +121,7 @@ Publish an with another name, added by an 'ipfs key' command: } kname, _, _ := req.Option("key").String() - k, err := n.GetKey(kname) + k, err := keylookup(kname, n) if err != nil { res.SetError(err, cmds.ErrNormal) return @@ -176,3 +181,36 @@ func publish(ctx context.Context, n *core.IpfsNode, k crypto.PrivKey, ref path.P Value: ref.String(), }, nil } + +func keylookup(k string, n *core.IpfsNode) (crypto.PrivKey, error) { + + res, err := n.GetKey(k) + if res != nil { + return res, nil + } + + keys, err := n.Repo.Keystore().List() + if err != nil { + return nil, err + } + + for _, key := range keys { + privKey, err := n.Repo.Keystore().Get(key) + if err != nil { + return nil, err + } + + pubKey := privKey.GetPublic() + + pid, err := peer.IDFromPublicKey(pubKey) + if err != nil { + return nil, err + } + + if pid.Pretty() == k { + return privKey, nil + } + } + + return nil, fmt.Errorf("no key by the given name or PeerID was found") +} From 76e227d499d75180db491d276db86b54e8aac6d8 Mon Sep 17 00:00:00 2001 From: Kerem Date: Thu, 27 Apr 2017 21:00:48 +0300 Subject: [PATCH 2/3] code review improvements change core.IpfsNode as first arg check keylookup err to propagate validation errors from lower levels License: MIT Signed-off-by: Kerem Gocen --- core/commands/publish.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/core/commands/publish.go b/core/commands/publish.go index 70eca056ec7..9db8b7bcc60 100644 --- a/core/commands/publish.go +++ b/core/commands/publish.go @@ -10,6 +10,7 @@ import ( cmds "github.com/ipfs/go-ipfs/commands" core "github.com/ipfs/go-ipfs/core" + keystore "github.com/ipfs/go-ipfs/keystore" path "github.com/ipfs/go-ipfs/path" crypto "gx/ipfs/QmP1DfoUjiWH2ZBo1PBH6FupdBucbDepx3HpWmEY6JMUpY/go-libp2p-crypto" @@ -121,7 +122,7 @@ Alternatively, publish an using a valid PeerID(as listed by 'ipfs ke } kname, _, _ := req.Option("key").String() - k, err := keylookup(kname, n) + k, err := keylookup(n, kname) if err != nil { res.SetError(err, cmds.ErrNormal) return @@ -182,13 +183,17 @@ func publish(ctx context.Context, n *core.IpfsNode, k crypto.PrivKey, ref path.P }, nil } -func keylookup(k string, n *core.IpfsNode) (crypto.PrivKey, error) { +func keylookup(n *core.IpfsNode, k string) (crypto.PrivKey, error) { res, err := n.GetKey(k) if res != nil { return res, nil } + if err != nil && err != keystore.ErrNoSuchKey { + return nil, err + } + keys, err := n.Repo.Keystore().List() if err != nil { return nil, err From fca98aea9d5a5d23240f9fc2e9895cfead74ab21 Mon Sep 17 00:00:00 2001 From: Kerem Date: Wed, 3 May 2017 06:19:30 +0300 Subject: [PATCH 3/3] add sharness tests test for `ipfs name publish` using PeerID as key name License: MIT Signed-off-by: Kerem Gocen --- test/sharness/t0100-name.sh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/sharness/t0100-name.sh b/test/sharness/t0100-name.sh index 688dcbd1ef3..38dcc27f34e 100755 --- a/test/sharness/t0100-name.sh +++ b/test/sharness/t0100-name.sh @@ -74,4 +74,20 @@ test_expect_failure "publish with our explicit node ID looks good" ' test_cmp expected_node_id_publish actual_node_id_publish ' +# publish with an explicit node ID as key name + +test_expect_success "generate and verify a new key" ' + NEWID=`ipfs key gen --type=rsa --size=2048 keyname` && + test_check_peerid "${NEWID}" +' + +test_expect_success "'ipfs name publish --key= ' succeeds" ' + ipfs name publish --key=${NEWID} "/ipfs/$HASH_WELCOME_DOCS" >actual_node_id_publish +' + +test_expect_success "publish an explicit node ID as key name looks good" ' + echo "Published to ${NEWID}: /ipfs/$HASH_WELCOME_DOCS" >expected_node_id_publish && + test_cmp expected_node_id_publish actual_node_id_publish +' + test_done