Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: add ‘private_key’ alias for ‘secret_key’ field #7035

Merged
merged 2 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
RocksDB configurable via `config.json` file (at `store.path` path)
rather than being hard-coded to `data` directory in neard home
directory [#6938](https://github.com/near/nearcore/pull/6938)
* Removed `testnet` alias for `localnet` command; the alias has been
deprecated since 1.24 [#7033](https://github.com/near/nearcore/pull/7033)
* Removed `testnet` alias for `localnet` command; it’s been deprecated
since 1.24 [#7033](https://github.com/near/nearcore/pull/7033)
* Removed undocumented `unsafe_reset_all` and `unsafe_reset_data`
commands. They were deprecated since 1.25
commands; they were deprecated since 1.25
* Key files can use `private_key` field instead of `secret_key` now;
this improves interoperability with near cli which uses the former
name [#7030](https://github.com/near/nearcore/issues/7030)

## 1.26.0 [2022-05-18]

Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ deepsize = { version = "0.2.0", optional = true }
[dev-dependencies]
hex-literal = "0.2"
sha2 = ">=0.8,<=0.10"
tempfile = "3.3"

[features]
deepsize_feature = [
Expand Down
37 changes: 37 additions & 0 deletions core/crypto/src/key_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ use near_account_id::AccountId;
pub struct KeyFile {
pub account_id: AccountId,
pub public_key: PublicKey,
// Credential files generated which near cli works with have private_key
// rather than secret_key field. To make it possible to read those from
// neard add private_key as an alias to this field so either will work.
#[serde(alias = "private_key")]
pub secret_key: SecretKey,
}

Expand All @@ -35,3 +39,36 @@ impl KeyFile {
Ok(serde_json::from_str(&content)?)
}
}

#[test]
fn test_from_file() {
fn load(contents: &[u8]) -> io::Result<KeyFile> {
let tmp = tempfile::NamedTempFile::new().unwrap();
tmp.as_file().write_all(contents).unwrap();
let result = KeyFile::from_file(tmp.path());
tmp.close().unwrap();
result
}

load(br#"{
"account_id": "example",
"public_key": "ed25519:6DSjZ8mvsRZDvFqFxo8tCKePG96omXW7eVYVSySmDk8e",
"secret_key": "ed25519:3D4YudUahN1nawWogh8pAKSj92sUNMdbZGjn7kERKzYoTy8tnFQuwoGUC51DowKqorvkr2pytJSnwuSbsNVfqygr"
}"#).unwrap();

load(br#"{
"account_id": "example",
"public_key": "ed25519:6DSjZ8mvsRZDvFqFxo8tCKePG96omXW7eVYVSySmDk8e",
"private_key": "ed25519:3D4YudUahN1nawWogh8pAKSj92sUNMdbZGjn7kERKzYoTy8tnFQuwoGUC51DowKqorvkr2pytJSnwuSbsNVfqygr"
}"#).unwrap();

let err = load(br#"{
"account_id": "example",
"public_key": "ed25519:6DSjZ8mvsRZDvFqFxo8tCKePG96omXW7eVYVSySmDk8e",
"secret_key": "ed25519:3D4YudUahN1nawWogh8pAKSj92sUNMdbZGjn7kERKzYoTy8tnFQuwoGUC51DowKqorvkr2pytJSnwuSbsNVfqygr",
"private_key": "ed25519:3D4YudUahN1nawWogh8pAKSj92sUNMdbZGjn7kERKzYoTy8tnFQuwoGUC51DowKqorvkr2pytJSnwuSbsNVfqygr"
}"#).map(|key| key.account_id).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::InvalidData);
let inner_msg = err.into_inner().unwrap().to_string();
assert!(inner_msg.contains("duplicate field"));
}