Skip to content

Commit

Permalink
Update siwe_login and siwe_prepare_login method names
Browse files Browse the repository at this point in the history
  • Loading branch information
kristoferlund committed Jan 31, 2024
1 parent 092857f commit 7a92176
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/ic-use-siwe-identity/src/ic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function callLogin(
throw new Error("Invalid actor, data or address");
}

const loginReponse = await anonymousActor.login(
const loginReponse = await anonymousActor.siwe_login(
data,
address,
new Uint8Array(sessionPublicKey)
Expand All @@ -82,7 +82,7 @@ export async function callGetDelegation(
throw new Error("Invalid actor or address");
}

const response = await anonymousActor.get_delegation(
const response = await anonymousActor.siwe_get_delegation(
address,
new Uint8Array(sessionPublicKey),
expiration
Expand Down
4 changes: 2 additions & 2 deletions packages/ic-use-siwe-identity/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export function SiweIdentityProvider<T extends SIWE_IDENTITY_SERVICE>({
}));

try {
const response = await state.anonymousActor.prepare_login(address);
const response = await state.anonymousActor.siwe_prepare_login(address);
if ("Ok" in response) {
const siweMessage = response.Ok;
setState((prevState) => ({
Expand Down Expand Up @@ -291,7 +291,7 @@ export function SiweIdentityProvider<T extends SIWE_IDENTITY_SERVICE>({
return;
}

// Call the backend's get_delegation method to get the delegation.
// Call the backend's siwe_get_delegation method to get the delegation.
let signedDelegation: _SignedDelegation;
try {
signedDelegation = await callGetDelegation(
Expand Down
19 changes: 16 additions & 3 deletions packages/ic-use-siwe-identity/src/service.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,46 @@ import type { ActorMethod } from "@dfinity/agent";
import type { Principal } from "@dfinity/principal";

export type Address = string;

export type CanisterPublicKey = PublicKey;

export interface Delegation {
pubkey: PublicKey;
targets: [] | [Array<Principal>];
expiration: Timestamp;
}

export type GetDelegationResponse = { Ok: SignedDelegation } | { Err: string };

export interface LoginOkResponse {
user_canister_pubkey: CanisterPublicKey;
expiration: Timestamp;
}

export type LoginResponse = { Ok: LoginOkResponse } | { Err: string };

export type PrepareLoginResponse = { Ok: SiweMessage } | { Err: string };

export type PublicKey = Uint8Array | number[];

export type SessionKey = PublicKey;

export interface SignedDelegation {
signature: Uint8Array | number[];
delegation: Delegation;
}

export type SiweMessage = string;

export type SiweSignature = string;

export type Timestamp = bigint;

export interface SIWE_IDENTITY_SERVICE {
get_delegation: ActorMethod<
siwe_prepare_login: ActorMethod<[Address], PrepareLoginResponse>;
siwe_login: ActorMethod<[SiweSignature, Address, SessionKey], LoginResponse>;
siwe_get_delegation: ActorMethod<
[Address, SessionKey, Timestamp],
GetDelegationResponse
>;
login: ActorMethod<[SiweSignature, Address, SessionKey], LoginResponse>;
prepare_login: ActorMethod<[Address], PrepareLoginResponse>;
}
3 changes: 2 additions & 1 deletion packages/ic_siwe/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ pub struct Settings {
/// The full URI, potentially including port number of the frontend that uses SIWE.
pub uri: String,

/// The salt is used when generating the seed that uniquely identifies each user principal.
/// The salt is used when generating the seed that uniquely identifies each user principal. The salt can only contain
/// printable ASCII characters.
pub salt: String,

/// The Ethereum chain ID for ic-siwe, defaults to 1 (Ethereum mainnet).
Expand Down
10 changes: 5 additions & 5 deletions packages/ic_siwe_provider/ic_siwe_provider.did
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ type GetPrincipalResponse = variant {
};

type LoginResponse = variant {
Ok : LoginOkResponse;
Ok : LoginDetails;
Err : text;
};

type LoginOkResponse = record {
type LoginDetails = record {
expiration : Timestamp;
user_canister_pubkey : CanisterPublicKey;
};
Expand All @@ -63,8 +63,8 @@ type PrepareLoginResponse = variant {
service : (settings_input : SettingsInput) -> {
"get_address" : (Principal) -> (GetAddressResponse) query;
"get_caller_address" : () -> (GetAddressResponse) query;
"get_delegation" : (Address, SessionKey, Timestamp) -> (GetDelegationResponse) query;
"get_principal" : (Address) -> (GetPrincipalResponse) query;
"login" : (SiweSignature, Address, SessionKey) -> (LoginResponse);
"prepare_login" : (Address) -> (PrepareLoginResponse);
"siwe_prepare_login" : (Address) -> (PrepareLoginResponse);
"siwe_login" : (SiweSignature, Address, SessionKey) -> (LoginResponse);
"siwe_get_delegation" : (Address, SessionKey, Timestamp) -> (GetDelegationResponse) query;
};
3 changes: 2 additions & 1 deletion packages/ic_siwe_provider/src/service/init_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ pub struct SettingsInput {
/// The full URI, potentially including port number of the frontend that uses SIWE.
pub uri: String,

/// The salt is used when generating the seed that uniquely identifies each user principal.
/// The salt is used when generating the seed that uniquely identifies each user principal. The salt can only contain
/// printable ASCII characters.
pub salt: String,

/// The Ethereum chain ID for ic-siwe, defaults to 1 (Ethereum mainnet).
Expand Down
3 changes: 2 additions & 1 deletion packages/ic_siwe_provider/src/service/siwe_get_delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ fn siwe_get_delegation(
expiration: u64,
) -> Result<SignedDelegation, String> {
// Fetches the certificate for the current call, required for creating a certified signature.
let certificate = data_certificate().expect("get_delegation must be called using a query call");
let certificate =
data_certificate().expect("siwe_get_delegation must be called using a query call");

// Create an EthAddress from the string. This validates the address.
let address = EthAddress::new(&address)?;
Expand Down
2 changes: 1 addition & 1 deletion packages/ic_siwe_provider/src/service/siwe_login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde_bytes::ByteBuf;
use crate::{update_root_hash, ADDRESS_PRINCIPAL, PRINCIPAL_ADDRESS, STATE};

/// Authenticates the user by verifying the signature of the SIWE message. This function also
/// prepares the delegation to be fetched in the next step, the `get_delegation` function.
/// prepares the delegation to be fetched in the next step, the `siwe_get_delegation` function.
///
/// # Arguments
/// * `signature` (String): The signature of the SIWE message.
Expand Down
2 changes: 1 addition & 1 deletion packages/ic_siwe_provider/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn valid_settings(canister_id: Principal, targets: Option<Vec<Principal>>) -
SettingsInput {
domain: "127.0.0.1".to_string(),
uri: "http://127.0.0.1:5173".to_string(),
salt: "dummy salt".to_string(),
salt: "dummy-salt".to_string(),
chain_id: Some(10),
scheme: Some("http".to_string()),
statement: Some("Login to the app".to_string()),
Expand Down
2 changes: 1 addition & 1 deletion packages/ic_siwe_provider/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn test_upgrade_with_changed_arguments() {
let settings = SettingsInput {
domain: "192.168.0.1".to_string(),
uri: "http://192.168.0.1:666".to_string(),
salt: "another salt".to_string(),
salt: "another-salt".to_string(),
chain_id: Some(666),
scheme: Some("https".to_string()),
statement: Some("Some login statement".to_string()),
Expand Down

0 comments on commit 7a92176

Please sign in to comment.