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

Rename SecretKey to PrivateKey throughout identity.rs code base. #409

Merged
merged 3 commits into from Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
760 changes: 380 additions & 380 deletions bindings/wasm/docs/api-reference.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bindings/wasm/examples/src/create_vc.js
Expand Up @@ -45,7 +45,7 @@ async function createVC(clientConfig) {
const signedVc = issuer.doc.signCredential(unsignedVc, {
method: issuer.doc.id.toString() + "#newKey",
public: issuer.newKey.public,
secret: issuer.newKey.secret,
private: issuer.newKey.private,
});

// Check if the credential is verifiable.
Expand Down
2 changes: 1 addition & 1 deletion bindings/wasm/examples/src/create_vp.js
Expand Up @@ -27,7 +27,7 @@ async function createVP(clientConfig) {

const signedVp = alice.doc.signPresentation(unsignedVp, {
method: "#key",
secret: alice.key.secret,
private: alice.key.private,
})

// Check the validation status of the Verifiable Presentation
Expand Down
2 changes: 1 addition & 1 deletion bindings/wasm/examples/src/merkle_key.js
Expand Up @@ -70,7 +70,7 @@ async function merkleKey(clientConfig) {
const signedVc = issuer.doc.signCredential(unsignedVc, {
method: method.id.toString(),
public: keys.public(0),
secret: keys.secret(0),
private: keys.private(0),
proof: keys.merkleProof(Digest.Sha256, 0)
});

Expand Down
24 changes: 12 additions & 12 deletions bindings/wasm/src/crypto/key_collection.rs
Expand Up @@ -7,8 +7,8 @@ use identity::crypto::merkle_key::Blake2b256;
use identity::crypto::merkle_key::Sha256;
use identity::crypto::merkle_tree::Proof;
use identity::crypto::KeyCollection as KeyCollection_;
use identity::crypto::PrivateKey;
use identity::crypto::PublicKey;
use identity::crypto::SecretKey;
use wasm_bindgen::prelude::*;

use crate::crypto::Digest;
Expand All @@ -26,7 +26,7 @@ struct JsonData {
#[derive(Deserialize, Serialize)]
struct KeyData {
public: String,
secret: String,
private: String,
}

// =============================================================================
Expand Down Expand Up @@ -68,10 +68,10 @@ impl KeyCollection {
self.0.public(index).map(encode_b58)
}

/// Returns the secret key at the specified `index` as a base58-encoded string.
/// Returns the private key at the specified `index` as a base58-encoded string.
#[wasm_bindgen]
pub fn secret(&self, index: usize) -> Option<String> {
self.0.secret(index).map(encode_b58)
pub fn private(&self, index: usize) -> Option<String> {
self.0.private(index).map(encode_b58)
}

#[wasm_bindgen(js_name = merkleRoot)]
Expand Down Expand Up @@ -108,13 +108,13 @@ impl KeyCollection {
#[wasm_bindgen(js_name = toJSON)]
pub fn to_json(&self) -> Result<JsValue, JsValue> {
let public: _ = self.0.iter_public();
let secret: _ = self.0.iter_secret();
let private: _ = self.0.iter_private();

let keys: Vec<KeyData> = public
.zip(secret)
.map(|(public, secret)| KeyData {
.zip(private)
.map(|(public, private)| KeyData {
public: encode_b58(public),
secret: encode_b58(secret),
private: encode_b58(private),
})
.collect();

Expand All @@ -132,10 +132,10 @@ impl KeyCollection {
let data: JsonData = json.into_serde().map_err(wasm_error)?;

let iter: _ = data.keys.iter().flat_map(|data| {
let pk: PublicKey = decode_b58(&data.public).ok()?.into();
let sk: SecretKey = decode_b58(&data.secret).ok()?.into();
let public_key: PublicKey = decode_b58(&data.public).ok()?.into();
let private_key: PrivateKey = decode_b58(&data.private).ok()?.into();

Some((pk, sk))
Some((public_key, private_key))
});

KeyCollection_::from_iterator(data.type_.into(), iter)
Expand Down
22 changes: 11 additions & 11 deletions bindings/wasm/src/crypto/key_pair.rs
Expand Up @@ -4,8 +4,8 @@
use identity::core::decode_b58;
use identity::core::encode_b58;
use identity::crypto::KeyPair as KeyPair_;
use identity::crypto::PrivateKey;
use identity::crypto::PublicKey;
use identity::crypto::SecretKey;
use wasm_bindgen::prelude::*;

use crate::crypto::KeyType;
Expand All @@ -16,7 +16,7 @@ struct JsonData {
#[serde(rename = "type")]
type_: KeyType,
public: String,
secret: String,
private: String,
}

// =============================================================================
Expand All @@ -34,13 +34,13 @@ impl KeyPair {
KeyPair_::new(type_.into()).map_err(wasm_error).map(Self)
}

/// Parses a `KeyPair` object from base58-encoded public/secret keys.
/// Parses a `KeyPair` object from base58-encoded public/private keys.
#[wasm_bindgen(js_name = fromBase58)]
pub fn from_base58(type_: KeyType, public_key: &str, secret_key: &str) -> Result<KeyPair, JsValue> {
pub fn from_base58(type_: KeyType, public_key: &str, private_key: &str) -> Result<KeyPair, JsValue> {
let public: PublicKey = decode_b58(public_key).map_err(wasm_error)?.into();
let secret: SecretKey = decode_b58(secret_key).map_err(wasm_error)?.into();
let private: PrivateKey = decode_b58(private_key).map_err(wasm_error)?.into();

Ok(Self((type_.into(), public, secret).into()))
Ok(Self((type_.into(), public, private).into()))
}

/// Returns the public key as a base58-encoded string.
Expand All @@ -49,10 +49,10 @@ impl KeyPair {
encode_b58(self.0.public())
}

/// Returns the secret key as a base58-encoded string.
/// Returns the private key as a base58-encoded string.
#[wasm_bindgen(getter)]
pub fn secret(&self) -> String {
encode_b58(self.0.secret())
pub fn private(&self) -> String {
encode_b58(self.0.private())
}

/// Serializes a `KeyPair` object as a JSON object.
Expand All @@ -61,7 +61,7 @@ impl KeyPair {
let data: JsonData = JsonData {
type_: self.0.type_().into(),
public: self.public(),
secret: self.secret(),
private: self.private(),
};

JsValue::from_serde(&data).map_err(wasm_error)
Expand All @@ -72,6 +72,6 @@ impl KeyPair {
pub fn from_json(json: &JsValue) -> Result<KeyPair, JsValue> {
let data: JsonData = json.into_serde().map_err(wasm_error)?;

Self::from_base58(data.type_, &data.public, &data.secret)
Self::from_base58(data.type_, &data.public, &data.private)
}
}
22 changes: 11 additions & 11 deletions bindings/wasm/src/did/wasm_document.rs
Expand Up @@ -8,8 +8,8 @@ use identity::crypto::merkle_key::MerkleDigestTag;
use identity::crypto::merkle_key::MerkleKey;
use identity::crypto::merkle_key::Sha256;
use identity::crypto::merkle_tree::Proof;
use identity::crypto::PrivateKey;
use identity::crypto::PublicKey;
use identity::crypto::SecretKey;
use identity::did::verifiable;
use identity::did::MethodScope;
use identity::iota::Error;
Expand Down Expand Up @@ -209,7 +209,7 @@ impl WasmDocument {
/// Signs the DID Document with the default authentication method.
#[wasm_bindgen]
pub fn sign(&mut self, key: &KeyPair) -> Result<()> {
self.0.sign(key.0.secret()).wasm_result()
self.0.sign(key.0.private()).wasm_result()
}

/// Verify the signature with the authentication_key
Expand Down Expand Up @@ -247,12 +247,12 @@ impl WasmDocument {
MerkleKey {
method: String,
public: String,
secret: String,
private: String,
proof: String,
},
Default {
method: String,
secret: String,
private: String,
},
}

Expand All @@ -263,7 +263,7 @@ impl WasmDocument {
Args::MerkleKey {
method,
public,
secret,
private,
proof,
} => {
let merkle_key: Vec<u8> = self
Expand All @@ -273,12 +273,12 @@ impl WasmDocument {
.wasm_result()?;

let public: PublicKey = decode_b58(&public).map(Into::into).wasm_result()?;
let secret: SecretKey = decode_b58(&secret).map(Into::into).wasm_result()?;
let private: PrivateKey = decode_b58(&private).map(Into::into).wasm_result()?;

let digest: MerkleDigestTag = MerkleKey::extract_tags(&merkle_key).wasm_result()?.1;
let proof: Vec<u8> = decode_b58(&proof).wasm_result()?;

let signer: _ = self.0.signer(&secret).method(&method);
let signer: _ = self.0.signer(&private).method(&method);

match digest {
MerkleDigestTag::SHA256 => match Proof::<Sha256>::decode(&proof) {
Expand All @@ -288,10 +288,10 @@ impl WasmDocument {
_ => return Err("Invalid Merkle Key Digest".into()),
}
}
Args::Default { method, secret } => {
let secret: SecretKey = decode_b58(&secret).wasm_result().map(Into::into)?;
Args::Default { method, private } => {
let private: PrivateKey = decode_b58(&private).wasm_result().map(Into::into)?;

self.0.signer(&secret).method(&method).sign(&mut data).wasm_result()?;
self.0.signer(&private).method(&method).sign(&mut data).wasm_result()?;
}
}

Expand Down Expand Up @@ -332,7 +332,7 @@ impl WasmDocument {
pub fn diff(&self, other: &WasmDocument, message: &str, key: &KeyPair) -> Result<WasmDocumentDiff> {
self
.0
.diff(&other.0, MessageId::from_str(message).wasm_result()?, key.0.secret())
.diff(&other.0, MessageId::from_str(message).wasm_result()?, key.0.private())
.map(WasmDocumentDiff::from)
.wasm_result()
}
Expand Down
14 changes: 7 additions & 7 deletions bindings/wasm/tests/wasm.rs
Expand Up @@ -15,9 +15,9 @@ use std::borrow::Cow;
#[wasm_bindgen_test]
fn test_keypair() {
let key1 = KeyPair::new(KeyType::Ed25519).unwrap();
let pk = key1.public();
let sk = key1.secret();
let key2 = KeyPair::from_base58(KeyType::Ed25519, &pk, &sk).unwrap();
let public_key = key1.public();
let private_key = key1.private();
let key2 = KeyPair::from_base58(KeyType::Ed25519, &public_key, &private_key).unwrap();

let json1 = key1.to_json().unwrap();
let json2 = key2.to_json().unwrap();
Expand All @@ -26,10 +26,10 @@ fn test_keypair() {
let from2 = KeyPair::from_json(&json2).unwrap();

assert_eq!(from1.public(), key1.public());
assert_eq!(from1.secret(), key1.secret());
assert_eq!(from1.private(), key1.private());

assert_eq!(from2.public(), key2.public());
assert_eq!(from2.secret(), key2.secret());
assert_eq!(from2.private(), key2.private());
}

#[wasm_bindgen_test]
Expand All @@ -44,7 +44,7 @@ fn test_key_collection() {
let key = keys.keypair(index).unwrap();

assert_eq!(key.public(), keys.public(index).unwrap());
assert_eq!(key.secret(), keys.secret(index).unwrap());
assert_eq!(key.private(), keys.private(index).unwrap());

assert!(keys.merkle_proof(Digest::Sha256, index).is_some());
}
Expand All @@ -57,7 +57,7 @@ fn test_key_collection() {

for index in 0..keys.length() {
assert_eq!(keys.public(index).unwrap(), from.public(index).unwrap());
assert_eq!(keys.secret(index).unwrap(), from.secret(index).unwrap());
assert_eq!(keys.private(index).unwrap(), from.private(index).unwrap());
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/low-level-api/common.rs
Expand Up @@ -76,7 +76,7 @@ pub async fn add_new_key(
// Prepare the update
updated_doc.set_previous_message_id(*receipt.message_id());
updated_doc.set_updated(Timestamp::now_utc());
updated_doc.sign(key.secret())?;
updated_doc.sign(key.private())?;

// Publish the update to the Tangle
let update_receipt: Receipt = client.publish_document(&updated_doc).await?;
Expand Down
2 changes: 1 addition & 1 deletion examples/low-level-api/create_did.rs
Expand Up @@ -21,7 +21,7 @@ pub async fn run() -> Result<(IotaDocument, KeyPair, Receipt)> {
let mut document: IotaDocument = IotaDocument::from_keypair(&keypair)?;

// Sign the DID Document with the default authentication key.
document.sign(keypair.secret())?;
document.sign(keypair.private())?;

println!("DID Document JSON > {:#}", document);

Expand Down
4 changes: 2 additions & 2 deletions examples/low-level-api/create_vc.rs
Expand Up @@ -29,8 +29,8 @@ pub async fn create_vc() -> Result<()> {
// Create an unsigned Credential with claims about `subject` specified by `issuer`.
let mut credential: Credential = common::issue_degree(&issuer_doc, &subject_doc)?;

// Sign the Credential with the issuer's secret key
issuer_doc.sign_data(&mut credential, issuer_key.secret())?;
// Sign the Credential with the issuer's private key.
issuer_doc.sign_data(&mut credential, issuer_key.private())?;

println!("Credential JSON > {:#}", credential);

Expand Down
8 changes: 4 additions & 4 deletions examples/low-level-api/create_vp.rs
Expand Up @@ -30,8 +30,8 @@ pub async fn create_vp() -> Result<Presentation> {
// Create an unsigned Credential with claims about `subject` specified by `issuer`.
let mut credential: Credential = common::issue_degree(&doc_iss, &doc_sub)?;

// Sign the Credential with the issuers secret key
doc_iss.sign_data(&mut credential, key_iss.secret())?;
// Sign the Credential with the issuers private key.
doc_iss.sign_data(&mut credential, key_iss.private())?;

// Create an unsigned Presentation from the previously issued Verifiable Credential.
let mut presentation: Presentation = PresentationBuilder::default()
Expand All @@ -40,8 +40,8 @@ pub async fn create_vp() -> Result<Presentation> {
.credential(credential)
.build()?;

// Sign the presentation with the holders secret key
doc_sub.sign_data(&mut presentation, key_sub.secret())?;
// Sign the presentation with the holders private key.
doc_sub.sign_data(&mut presentation, key_sub.private())?;

Ok(presentation)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/low-level-api/diff_chain.rs
Expand Up @@ -38,7 +38,7 @@ async fn main() -> Result<()> {
};

// Generate a signed diff object.
let diff: DocumentDiff = document.diff(&updated_document, *receipt.message_id(), keypair.secret())?;
let diff: DocumentDiff = document.diff(&updated_document, *receipt.message_id(), keypair.private())?;

println!("Diff > {:#?}", diff);

Expand Down
2 changes: 1 addition & 1 deletion examples/low-level-api/manipulate_did.rs
Expand Up @@ -44,7 +44,7 @@ pub async fn run() -> Result<(IotaDocument, KeyPair, KeyPair, Receipt, Receipt)>
document.set_updated(Timestamp::now_utc());

// Sign the DID Document with the original private key.
document.sign(keypair.secret())?;
document.sign(keypair.private())?;

// Publish the updated DID Document to the Tangle.
let update_receipt: Receipt = client.publish_document(&document).await?;
Expand Down
12 changes: 6 additions & 6 deletions examples/low-level-api/merkle_key.rs
Expand Up @@ -13,8 +13,8 @@ use identity::credential::Credential;
use identity::crypto::merkle_key::Sha256;
use identity::crypto::merkle_tree::Proof;
use identity::crypto::KeyCollection;
use identity::crypto::PrivateKey;
use identity::crypto::PublicKey;
use identity::crypto::SecretKey;
use identity::did::MethodScope;
use identity::iota::ClientMap;
use identity::iota::CredentialValidation;
Expand Down Expand Up @@ -46,7 +46,7 @@ async fn main() -> Result<()> {
// Add to the DID Document as a general-purpose verification method
issuer_doc.insert_method(MethodScope::VerificationMethod, method);
issuer_doc.set_previous_message_id(*issuer_receipt.message_id());
issuer_doc.sign(issuer_key.secret())?;
issuer_doc.sign(issuer_key.private())?;

// Publish the Identity to the IOTA Network and log the results.
// This may take a few seconds to complete proof-of-work.
Expand All @@ -60,14 +60,14 @@ async fn main() -> Result<()> {
let index: usize = OsRng.gen_range(0..keys.len());

let public: &PublicKey = keys.public(index).unwrap();
let secret: &SecretKey = keys.secret(index).unwrap();
let private: &PrivateKey = keys.private(index).unwrap();

// Generate an inclusion proof for the selected key
let proof: Proof<Sha256> = keys.merkle_proof(index).unwrap();

// Sign the Credential with the issuers secret key
// Sign the Credential with the issuers private key
issuer_doc
.signer(secret)
.signer(private)
.method("merkle-key")
.merkle_key((public, &proof))
.sign(&mut credential)?;
Expand All @@ -89,7 +89,7 @@ async fn main() -> Result<()> {
.and_then(IotaVerificationMethod::try_from_mut)?
.revoke_merkle_key(index)?;
issuer_doc.set_previous_message_id(*receipt.message_id());
issuer_doc.sign(issuer_key.secret())?;
issuer_doc.sign(issuer_key.private())?;

let receipt: Receipt = client.publish_document(&issuer_doc).await?;

Expand Down