EOS 公私钥和钱包

EOS 公私钥在 EOS 系统中用于创建账号,也可以给不同账号指定多个公钥的权限,在做 EOS 主网映射时也需要使用 EOS 公钥(关于账号的创建在后续文章中会有介绍)。EOS 钱包主要负责保存公私钥对和签名。

下面主要通过 cleos 命令的使用了解如何创建公私钥以及与 keosd 进行交互(不含与 nodeos 的交互)。

cleos create key

create key 命令可以创建对应的公私钥对,通过指定 --to-console 显示在命令行或 -f 指定文件名保存于文件。如果需要经常使用的建议保存到文件中以备以后使用,如果两次指定同一个文件,则会覆盖第一次的结果。

$ cleos create key --to-console
Private key: 5KChzvYMAYQziSJx1RucoMQmn6yX3GrXH6rkZQzBo18i8pZeVDr
Public key: EOS8M9ixQkrTh8GkcE4h6nggY4LQfd6tejL2nQSanq7KZS7hH7NjL

$ cleos create key -f ~/keys/key.txt
saving keys to /Users/bitson/keys/key.txt

通常情况下使用 secp256k1 曲线的椭圆曲线,还可以指定 --r1 来使用 secp256r1 曲线加密算法来生成公私钥,secp256r1 生成的公私钥对可以通过形式与 secp256k1 的进行区分。

$ cleos create key --to-console --r1
Private key: PVT_R1_2v1mkQGoHqKfdSL3vA2UjFaaMJgfNNs6sBCsh5AQpQMgUHiLCx
Public key: PUB_R1_52nDZ2wVgB6TbbNxBUcuxpvCiQTNSmrc6yCqdjkLgVcqJeZ4ni

比特币和以太坊使用的椭圆曲线是 secp256k1,该曲线的参数非常简单,因大家比较愿意相信不是为了设计一条弱曲线而被精心构造的;美国国家标准与技术研究院(NIST)和美国国家安全局(NSA)推荐过的 secp256r1、secp521r1 等椭圆曲线,参数更加复杂,因此其运算速度也更慢。并且有些人怀疑曲线中参数有可能被精心选取,导致曲线加密性能弱化。

EOSIO supports the R1 elliptic curve which means every iMac Pro, MacBook Pro, iPhone and iPad can use the secure enclave as biometric secured hardware wallet.

The same R1 curve is used on many smart-cards and Android devices.

cleos wallet *

使用 cleos wallet 命令,如果当前系统没有运行 keosd 将自动启动 keosd。wallet 主要用于存储公私钥对,可以供以后签名使用。

cleos create key 命令并不需要自动启动 keosd。

create

$ cleos wallet create --to-console
"/usr/local/eosio/bin/keosd" launched
Creating wallet: default
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.
"PW5JnqYauSS9jm9SqDok4o6SewTSYtwoJZzFYTE9nguFzz6bkNc7r"

"/usr/local/eosio/bin/keosd" launched 显示上述代码自动启动 keosd。

cleos wallet create 命令可以创建钱包PW5JnqYauSS9jm9SqDok4o6SewTSYtwoJZzFYTE9nguFzz6bkNc7r 为钱包密码,供以后解锁钱包使用。默认创建的钱包名称为 default。也可以通过 -n 指定钱包名称,-file 指定钱包密码保存文件路径(同上)。

list

$ cleos wallet list
Wallets:
[
 "default *"
]

cleos wallet list 显示所有创建的钱包列表,钱包名称后面的 * 号表示当前钱包处于解锁状态。

再创建钱包 test,使用文件的方式保存钱包密码

$ cleos wallet create -n test --file test.txt
Creating wallet: test
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.
saving password to test.txt

调用 cleos wallet list 可以看到现在下面有default 和 test 两个钱包均处于解锁状态。

$ cleos wallet list
Wallets:
[
  "default *",
  "test *"
]

lock,lock_all

cleos wallet lock 可以锁定钱包。锁定后我们可以看到钱包后面的 * 就不见了。对此钱包进行的秘钥相关操作将都不可以进行。

cleos wallet lock_all 可以将所有钱包锁定,自己尝试

$ cleos wallet lock
Locked: default

$ cleos wallet list
Wallets:
[
  "default",
  "test *"
]

$ cleos wallet create_key
Error 3120003: Locked wallet
Ensure that your wallet is unlocked before using it!

钱包默认在 900 秒(15 分钟)后如果没有任何调用将自动锁定。

unlock

可以通过 cleos wallet unlock 来解锁钱包。输入创建钱包时的密码 PW5JnqYauSS9jm9SqDok4o6SewTSYtwoJZzFYTE9nguFzz6bkNc7r 即可(如果是 --file 则打开指定文件查看密码)。

$ cleos wallet unlock
password: Unlocked: default

create_key, import

可以通过 cleso wallet create_key 在钱包中创建新的公私钥对(与 cleos create key 不同的是不会显示出私钥,其他参数意义一致),cleos wallet import 则可以将已有的公私钥对导入钱包管理。本例中导入上面使用 r1 算法创建的私钥。

$ cleos wallet create_key
Created new private key with a public key of: "EOS8QfDjTc8gWebdf3NzhivQ4bbCZ6NMdNUDSLEAY2c1BeLy6iYxL"

$ cleos wallet import
private key: imported private key for: PUB_R1_52nDZ2wVgB6TbbNxBUcuxpvCiQTNSmrc6yCqdjkLgVcqJeZ4ni

keys, private_keys

可以通过 cleos wallet keys 来显示当前所有解锁钱包的公钥。

$ cleos wallet keys
[
  "EOS8QfDjTc8gWebdf3NzhivQ4bbCZ6NMdNUDSLEAY2c1BeLy6iYxL",
  "PUB_R1_52nDZ2wVgB6TbbNxBUcuxpvCiQTNSmrc6yCqdjkLgVcqJeZ4ni"
]

cleos wallet private_keys 则会显示指定钱包下的公私钥对,需要输入指定钱包的密码来验证(未指定钱包名称默认为 default)。

$ cleos wallet private_keys
password: [[
    "EOS8QfDjTc8gWebdf3NzhivQ4bbCZ6NMdNUDSLEAY2c1BeLy6iYxL",
    "5KG1ry9ZkKggZGcJFnsooX2tVCojyco9k8iH4mni4vWeYRgQ8d5"
  ],[
    "PUB_R1_52nDZ2wVgB6TbbNxBUcuxpvCiQTNSmrc6yCqdjkLgVcqJeZ4ni",
    "PVT_R1_2v1mkQGoHqKfdSL3vA2UjFaaMJgfNNs6sBCsh5AQpQMgUHiLCx"
  ]
]

remove_key

最后使用 cleos wallet remove_key 来删除公私钥对,指定 r1 的公钥来删除公私钥对,同样也需要输入钱包密码。

可以使用 --password 来指定钱包密码

$ cleos wallet remove_key PUB_R1_52nDZ2wVgB6TbbNxBUcuxpvCiQTNSmrc6yCqdjkLgVcqJeZ4ni --password PW5JnqYauSS9jm9SqDok4o6SewTSYtwoJZzFYTE9nguFzz6bkNc7r
removed private key for: PUB_R1_52nDZ2wVgB6TbbNxBUcuxpvCiQTNSmrc6yCqdjkLgVcqJeZ4ni

删除后钱包中已经没有原来 r1 的公私钥对了

$ cleos wallet keys
[
  "EOS8QfDjTc8gWebdf3NzhivQ4bbCZ6NMdNUDSLEAY2c1BeLy6iYxL"
]

stop

使用 cleos wallet stop 可以停止钱包运行(停止前后我们可以通过 ps 查看下当前进程)。

$ ps -ef | grep keosd
  501 39585     1   0 11:16PM ttys001    0:00.05 /usr/local/eosio/bin/keosd --http-server-address  --https-server-address  --unix-socket-path keosd.sock
  501 40123 39116   0 11:56PM ttys001    0:00.00 grep keosd

$ cleos wallet stop
OK

$ ps -ef | grep keosd
  501 40130 39116   0 11:57PM ttys001    0:00.00 grep keosd

总结

至此所有 wallet 相关的命令都演示完毕了。

命令 子命令
wallet create
wallet open
wallet lock
wallet lock_all
wallet unlock
wallet import
wallet remove_key
wallet create_key
wallet list
wallet keys
wallet private_keys
wallet stop

Tips:在命令执行前手动启动 keosd,则执行命令式会检测到 keosd 已经在运行,将不会再次启动 keosd。手动启动 keosd 可以指定一些自定义的配置信息,例如将自动锁定时间指定为 9000 秒。

$ keosd --unlock-timeout=9000 &
[1] 40234
localhost:~ $ info  2019-01-21T16:03:43.241 thread-0  wallet_plugin.cpp:42          plugin_initialize    ] initializing wallet plugin
warn  2019-01-21T16:03:43.243 thread-0  se_wallet.cpp:283             check_signed         ] Application does not have a valid signature; Secure Enclave support disabled
info  2019-01-21T16:03:43.243 thread-0  http_plugin.cpp:554           add_handler          ] add api url: /v1/keosd/stop
info  2019-01-21T16:03:43.243 thread-0  wallet_api_plugin.cpp:73      plugin_startup       ] starting wallet_api_plugin
info  2019-01-21T16:03:43.243 thread-0  http_plugin.cpp:554           add_handler          ] add api url: /v1/wallet/create
info  2019-01-21T16:03:43.243 thread-0  http_plugin.cpp:554           add_handler          ] add api url: /v1/wallet/create_key
info  2019-01-21T16:03:43.243 thread-0  http_plugin.cpp:554           add_handler          ] add api url: /v1/wallet/get_public_keys
info  2019-01-21T16:03:43.243 thread-0  http_plugin.cpp:554           add_handler          ] add api url: /v1/wallet/import_key
info  2019-01-21T16:03:43.243 thread-0  http_plugin.cpp:554           add_handler          ] add api url: /v1/wallet/list_keys
info  2019-01-21T16:03:43.243 thread-0  http_plugin.cpp:554           add_handler          ] add api url: /v1/wallet/list_wallets
info  2019-01-21T16:03:43.244 thread-0  http_plugin.cpp:554           add_handler          ] add api url: /v1/wallet/lock
info  2019-01-21T16:03:43.244 thread-0  http_plugin.cpp:554           add_handler          ] add api url: /v1/wallet/lock_all
info  2019-01-21T16:03:43.244 thread-0  http_plugin.cpp:554           add_handler          ] add api url: /v1/wallet/open
info  2019-01-21T16:03:43.244 thread-0  http_plugin.cpp:554           add_handler          ] add api url: /v1/wallet/remove_key
info  2019-01-21T16:03:43.244 thread-0  http_plugin.cpp:554           add_handler          ] add api url: /v1/wallet/set_timeout
info  2019-01-21T16:03:43.244 thread-0  http_plugin.cpp:554           add_handler          ] add api url: /v1/wallet/sign_digest
info  2019-01-21T16:03:43.244 thread-0  http_plugin.cpp:554           add_handler          ] add api url: /v1/wallet/sign_transaction
info  2019-01-21T16:03:43.244 thread-0  http_plugin.cpp:554           add_handler          ] add api url: /v1/wallet/unlock


Reference

https://developers.eos.io/
https://eosio.stackexchange.com/questions/136/hardware-wallet-support/225#225
https://www.reddit.com/r/crypto/comments/2uq5kr/why_did_satoshi_use_k1_vs_r1/


相关链接
EOS 源码编译
EOS 组件介绍

猜你喜欢

转载自blog.csdn.net/weixin_33949359/article/details/87561804
eos
今日推荐