Skip to content

Commit

Permalink
crypto: add ‘private_key’ alias for ‘secret_key’ field
Browse files Browse the repository at this point in the history
To make it easier to use credential files that near cli tool works
with, introduce ‘private_key’ alias for the ‘secret_key’ field in
KeyFile struct.  This way, the JSON file can have either of those
fields set and it’ll be read correctly.

Fixes: near#7030
  • Loading branch information
mina86 committed Jun 14, 2022
1 parent 426acb2 commit 70fb952
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
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"));
}

0 comments on commit 70fb952

Please sign in to comment.