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

Add google identity in enum #3170

Merged
merged 1 commit into from
Nov 7, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions common/primitives/core/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,19 +272,21 @@ impl Debug for Address33 {
Encode, Decode, Clone, Debug, PartialEq, Eq, TypeInfo, MaxEncodedLen, EnumIter, Ord, PartialOrd,
)]
pub enum Identity {
// web2
#[codec(index = 0)]
Twitter(IdentityString),

#[codec(index = 1)]
Discord(IdentityString),

#[codec(index = 2)]
Github(IdentityString),

// web3
#[codec(index = 3)]
Substrate(Address32),

#[codec(index = 4)]
Evm(Address20),

// bitcoin addresses are derived (one-way hash) from the pubkey
// by using `Address33` as the Identity handle, it requires that pubkey
// is retrievable by the wallet API when verifying the bitcoin account.
Expand All @@ -297,13 +299,20 @@ pub enum Identity {

#[codec(index = 7)]
Email(IdentityString),

#[codec(index = 8)]
Google(IdentityString),
}

impl Identity {
pub fn is_web2(&self) -> bool {
matches!(
self,
Self::Twitter(..) | Self::Discord(..) | Self::Github(..) | Self::Email(..)
Self::Twitter(..)
| Self::Discord(..)
| Self::Github(..)
| Self::Email(..)
| Self::Google(..)
)
}

Expand Down Expand Up @@ -339,7 +348,8 @@ impl Identity {
Identity::Twitter(_)
| Identity::Discord(_)
| Identity::Github(_)
| Identity::Email(_) => Vec::new(),
| Identity::Email(_)
| Identity::Google(_) => Vec::new(),
}
}

Expand All @@ -355,7 +365,8 @@ impl Identity {
Identity::Twitter(_)
| Identity::Discord(_)
| Identity::Github(_)
| Identity::Email(_) => networks.is_empty(),
| Identity::Email(_)
| Identity::Google(_) => networks.is_empty(),
}
}

Expand All @@ -376,7 +387,8 @@ impl Identity {
Identity::Twitter(_)
| Identity::Discord(_)
| Identity::Github(_)
| Identity::Email(_) => None,
| Identity::Email(_)
| Identity::Google(_) => None,
}
}

Expand Down Expand Up @@ -439,6 +451,10 @@ impl Identity {
return Ok(Identity::Email(IdentityString::new(
v[1].as_bytes().to_vec(),
)));
} else if v[0] == "google" {
return Ok(Identity::Google(IdentityString::new(
v[1].as_bytes().to_vec(),
)));
} else {
return Err("Unknown did type");
}
Expand Down Expand Up @@ -479,6 +495,11 @@ impl Identity {
str::from_utf8(handle.inner_ref())
.map_err(|_| "email handle conversion error")?
),
Identity::Google(handle) => format!(
"google:{}",
str::from_utf8(handle.inner_ref())
.map_err(|_| "google handle conversion error")?
),
}
))
}
Expand Down Expand Up @@ -566,6 +587,7 @@ mod tests {
Identity::Evm(..) => false,
Identity::Bitcoin(..) => false,
Identity::Solana(..) => false,
Identity::Google(..) => true,
}
)
})
Expand All @@ -585,6 +607,7 @@ mod tests {
Identity::Evm(..) => true,
Identity::Bitcoin(..) => true,
Identity::Solana(..) => true,
Identity::Google(..) => false,
}
)
})
Expand All @@ -604,6 +627,7 @@ mod tests {
Identity::Evm(..) => false,
Identity::Bitcoin(..) => false,
Identity::Solana(..) => false,
Identity::Google(..) => false,
}
)
})
Expand All @@ -623,6 +647,7 @@ mod tests {
Identity::Evm(..) => true,
Identity::Bitcoin(..) => false,
Identity::Solana(..) => false,
Identity::Google(..) => false,
}
)
})
Expand All @@ -642,6 +667,7 @@ mod tests {
Identity::Evm(..) => false,
Identity::Bitcoin(..) => true,
Identity::Solana(..) => false,
Identity::Google(..) => false,
}
)
})
Expand All @@ -661,6 +687,7 @@ mod tests {
Identity::Evm(..) => false,
Identity::Bitcoin(..) => false,
Identity::Solana(..) => true,
Identity::Google(..) => false,
}
)
})
Expand Down Expand Up @@ -795,4 +822,12 @@ mod tests {
assert_eq!(identity.to_did().unwrap(), did.as_str());
assert_eq!(Identity::from_did(did.as_str()).unwrap(), identity);
}

#[test]
fn test_google_did() {
let identity = Identity::Google(IdentityString::new("test@gmail.com".as_bytes().to_vec()));
let did_str = "did:litentry:google:test@gmail.com";
assert_eq!(identity.to_did().unwrap(), did_str);
assert_eq!(Identity::from_did(did_str).unwrap(), identity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ pub fn identity_with_networks_to_token(identity: &IdentityNetworkTuple) -> Token
Identity::Bitcoin(addr) => (5, addr.as_ref().to_vec()),
Identity::Solana(addr) => (6, addr.as_ref().to_vec()),
Identity::Email(str) => (7, str.inner_ref().to_vec()),
Identity::Google(str) => (8, str.inner_ref().to_vec()),
};
let networks: Vec<Token> = identity.1.iter().map(network_to_token).collect();
Token::Tuple(vec![Token::Uint(type_index.into()), Token::Bytes(value), Token::Array(networks)])
Expand Down
1 change: 1 addition & 0 deletions tee-worker/identity/service/src/prometheus_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ fn handle_stf_call_request(req: RequestType, time: f64) {
Identity::Evm(_) => "Evm".into(),
Identity::Bitcoin(_) => "Bitcoin".into(),
Identity::Solana(_) => "Solana".into(),
Identity::Google(_) => "Google".into(),
},
};
inc_stf_calls(category, &label);
Expand Down
Loading