Skip to content

Commit a158036

Browse files
committed
feat: allow key export in online mode
Export does not require repo lock and it is safe to do even when ipfs daemon is running. This enables apps like Brave browser to do import/export without stopping/starting daemon. Ref. brave/brave-browser#15422
1 parent b8de0e8 commit a158036

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

core/commands/keystore.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
cmds "github.com/ipfs/go-ipfs-cmds"
1414
config "github.com/ipfs/go-ipfs-config"
15+
keystore "github.com/ipfs/go-ipfs-keystore"
1516
oldcmds "github.com/ipfs/go-ipfs/commands"
1617
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
1718
"github.com/ipfs/go-ipfs/core/commands/e"
@@ -150,7 +151,6 @@ path can be specified with '--output=<path>' or '-o=<path>'.
150151
cmds.StringOption(outputOptionName, "o", "The path where the output should be stored."),
151152
},
152153
NoRemote: true,
153-
PreRun: DaemonNotRunning,
154154
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
155155
name := req.Arguments[0]
156156

@@ -163,13 +163,15 @@ path can be specified with '--output=<path>' or '-o=<path>'.
163163
return err
164164
}
165165

166-
r, err := fsrepo.Open(cfgRoot)
166+
// Export is read-only: safe to read it without acquiring repo lock
167+
// (this makes export work when ipfs daemon is already running)
168+
ksp := filepath.Join(cfgRoot, "keystore")
169+
ks, err := keystore.NewFSKeystore(ksp)
167170
if err != nil {
168171
return err
169172
}
170-
defer r.Close()
171173

172-
sk, err := r.Keystore().Get(name)
174+
sk, err := ks.Get(name)
173175
if err != nil {
174176
return fmt.Errorf("key with name '%s' doesn't exist", name)
175177
}

test/sharness/t0165-keystore.sh

+7-2
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,13 @@ ipfs key rm key_ed25519
175175
test_cmp rsa_key_id roundtrip_rsa_key_id
176176
'
177177

178-
test_expect_success "online export rsa key" '
179-
test_must_fail ipfs key export generated_rsa_key
178+
test_expect_success "export and import ed25519 key while daemon is running" '
179+
edhash=$(ipfs key gen exported_ed25519_key --type=ed25519)
180+
echo $edhash > ed25519_key_id
181+
ipfs key export exported_ed25519_key &&
182+
ipfs key rm exported_ed25519_key &&
183+
ipfs key import exported_ed25519_key exported_ed25519_key.key > roundtrip_ed25519_key_id &&
184+
test_cmp ed25519_key_id roundtrip_ed25519_key_id
180185
'
181186

182187
test_expect_success "online rotate rsa key" '

0 commit comments

Comments
 (0)