Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
whqwjb
go-ethereum
提交
6f004c46
G
go-ethereum
项目概览
whqwjb
/
go-ethereum
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
go-ethereum
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
6f004c46
编写于
9月 19, 2018
作者:
M
Martin Holst Swende
提交者:
Felix Lange
9月 19, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
accounts/keystore: double-check keystore file after creation (#17348)
上级
16e95f33
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
41 addition
and
10 deletion
+41
-10
accounts/keystore/key.go
accounts/keystore/key.go
+13
-5
accounts/keystore/keystore.go
accounts/keystore/keystore.go
+1
-1
accounts/keystore/keystore_passphrase.go
accounts/keystore/keystore_passphrase.go
+25
-2
accounts/keystore/keystore_plain_test.go
accounts/keystore/keystore_plain_test.go
+2
-2
未找到文件。
accounts/keystore/key.go
浏览文件 @
6f004c46
...
...
@@ -179,26 +179,34 @@ func storeNewKey(ks keyStore, rand io.Reader, auth string) (*Key, accounts.Accou
return
key
,
a
,
err
}
func
write
KeyFile
(
file
string
,
content
[]
byte
)
error
{
func
write
TemporaryKeyFile
(
file
string
,
content
[]
byte
)
(
string
,
error
)
{
// Create the keystore directory with appropriate permissions
// in case it is not present yet.
const
dirPerm
=
0700
if
err
:=
os
.
MkdirAll
(
filepath
.
Dir
(
file
),
dirPerm
);
err
!=
nil
{
return
err
return
""
,
err
}
// Atomic write: create a temporary hidden file first
// then move it into place. TempFile assigns mode 0600.
f
,
err
:=
ioutil
.
TempFile
(
filepath
.
Dir
(
file
),
"."
+
filepath
.
Base
(
file
)
+
".tmp"
)
if
err
!=
nil
{
return
err
return
""
,
err
}
if
_
,
err
:=
f
.
Write
(
content
);
err
!=
nil
{
f
.
Close
()
os
.
Remove
(
f
.
Name
())
return
err
return
""
,
err
}
f
.
Close
()
return
os
.
Rename
(
f
.
Name
(),
file
)
return
f
.
Name
(),
nil
}
func
writeKeyFile
(
file
string
,
content
[]
byte
)
error
{
name
,
err
:=
writeTemporaryKeyFile
(
file
,
content
)
if
err
!=
nil
{
return
err
}
return
os
.
Rename
(
name
,
file
)
}
// keyFileName implements the naming convention for keyfiles:
...
...
accounts/keystore/keystore.go
浏览文件 @
6f004c46
...
...
@@ -78,7 +78,7 @@ type unlocked struct {
// NewKeyStore creates a keystore for the given directory.
func
NewKeyStore
(
keydir
string
,
scryptN
,
scryptP
int
)
*
KeyStore
{
keydir
,
_
=
filepath
.
Abs
(
keydir
)
ks
:=
&
KeyStore
{
storage
:
&
keyStorePassphrase
{
keydir
,
scryptN
,
scryptP
}}
ks
:=
&
KeyStore
{
storage
:
&
keyStorePassphrase
{
keydir
,
scryptN
,
scryptP
,
false
}}
ks
.
init
(
keydir
)
return
ks
}
...
...
accounts/keystore/keystore_passphrase.go
浏览文件 @
6f004c46
...
...
@@ -35,6 +35,7 @@ import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/ethereum/go-ethereum/common"
...
...
@@ -72,6 +73,10 @@ type keyStorePassphrase struct {
keysDirPath
string
scryptN
int
scryptP
int
// skipKeyFileVerification disables the security-feature which does
// reads and decrypts any newly created keyfiles. This should be 'false' in all
// cases except tests -- setting this to 'true' is not recommended.
skipKeyFileVerification
bool
}
func
(
ks
keyStorePassphrase
)
GetKey
(
addr
common
.
Address
,
filename
,
auth
string
)
(
*
Key
,
error
)
{
...
...
@@ -93,7 +98,7 @@ func (ks keyStorePassphrase) GetKey(addr common.Address, filename, auth string)
// StoreKey generates a key, encrypts with 'auth' and stores in the given directory
func
StoreKey
(
dir
,
auth
string
,
scryptN
,
scryptP
int
)
(
common
.
Address
,
error
)
{
_
,
a
,
err
:=
storeNewKey
(
&
keyStorePassphrase
{
dir
,
scryptN
,
scryptP
},
rand
.
Reader
,
auth
)
_
,
a
,
err
:=
storeNewKey
(
&
keyStorePassphrase
{
dir
,
scryptN
,
scryptP
,
false
},
rand
.
Reader
,
auth
)
return
a
.
Address
,
err
}
...
...
@@ -102,7 +107,25 @@ func (ks keyStorePassphrase) StoreKey(filename string, key *Key, auth string) er
if
err
!=
nil
{
return
err
}
return
writeKeyFile
(
filename
,
keyjson
)
// Write into temporary file
tmpName
,
err
:=
writeTemporaryKeyFile
(
filename
,
keyjson
)
if
err
!=
nil
{
return
err
}
if
!
ks
.
skipKeyFileVerification
{
// Verify that we can decrypt the file with the given password.
_
,
err
=
ks
.
GetKey
(
key
.
Address
,
tmpName
,
auth
)
if
err
!=
nil
{
msg
:=
"An error was encountered when saving and verifying the keystore file.
\n
"
+
"This indicates that the keystore is corrupted.
\n
"
+
"The corrupted file is stored at
\n
%v
\n
"
+
"Please file a ticket at:
\n\n
"
+
"https://github.com/ethereum/go-ethereum/issues."
+
"The error was : %s"
return
fmt
.
Errorf
(
msg
,
tmpName
,
err
)
}
}
return
os
.
Rename
(
tmpName
,
filename
)
}
func
(
ks
keyStorePassphrase
)
JoinPath
(
filename
string
)
string
{
...
...
accounts/keystore/keystore_plain_test.go
浏览文件 @
6f004c46
...
...
@@ -37,7 +37,7 @@ func tmpKeyStoreIface(t *testing.T, encrypted bool) (dir string, ks keyStore) {
t
.
Fatal
(
err
)
}
if
encrypted
{
ks
=
&
keyStorePassphrase
{
d
,
veryLightScryptN
,
veryLightScryptP
}
ks
=
&
keyStorePassphrase
{
d
,
veryLightScryptN
,
veryLightScryptP
,
true
}
}
else
{
ks
=
&
keyStorePlain
{
d
}
}
...
...
@@ -191,7 +191,7 @@ func TestV1_1(t *testing.T) {
func
TestV1_2
(
t
*
testing
.
T
)
{
t
.
Parallel
()
ks
:=
&
keyStorePassphrase
{
"testdata/v1"
,
LightScryptN
,
LightScryptP
}
ks
:=
&
keyStorePassphrase
{
"testdata/v1"
,
LightScryptN
,
LightScryptP
,
true
}
addr
:=
common
.
HexToAddress
(
"cb61d5a9c4896fb9658090b597ef0e7be6f7b67e"
)
file
:=
"testdata/v1/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e"
k
,
err
:=
ks
.
GetKey
(
addr
,
file
,
"g"
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录