diff --git a/src/frontend/src/eth/services/send.services.ts b/src/frontend/src/eth/services/send.services.ts index 041d845065..aad1074a5a 100644 --- a/src/frontend/src/eth/services/send.services.ts +++ b/src/frontend/src/eth/services/send.services.ts @@ -19,7 +19,7 @@ import { toCkErc20HelperContractAddress, toCkEthHelperContractAddress } from '$icp-eth/utils/cketh.utils'; -import { signTransaction } from '$lib/api/backend.api'; +import { signTransaction } from '$lib/api/signer.api'; import { DEFAULT_ETHEREUM_NETWORK } from '$lib/constants/networks.constants'; import { ProgressStepsSend } from '$lib/enums/progress-steps'; import { i18n } from '$lib/stores/i18n.store'; diff --git a/src/frontend/src/eth/services/wallet-connect.services.ts b/src/frontend/src/eth/services/wallet-connect.services.ts index 7a3496a97a..5af68671b7 100644 --- a/src/frontend/src/eth/services/wallet-connect.services.ts +++ b/src/frontend/src/eth/services/wallet-connect.services.ts @@ -7,7 +7,7 @@ import { getSignParamsMessageTypedDataV4Hash } from '$eth/utils/wallet-connect.utils'; import { assertCkEthMinterInfoLoaded } from '$icp-eth/services/cketh.services'; -import { signMessage as signMessageApi, signPrehash } from '$lib/api/backend.api'; +import { signMessage as signMessageApi, signPrehash } from '$lib/api/signer.api'; import { TRACK_COUNT_WC_ETH_SEND_ERROR, TRACK_COUNT_WC_ETH_SEND_SUCCESS, diff --git a/src/frontend/src/lib/actors/actors.ic.ts b/src/frontend/src/lib/actors/actors.ic.ts index 5f078ffd94..a246c4497c 100644 --- a/src/frontend/src/lib/actors/actors.ic.ts +++ b/src/frontend/src/lib/actors/actors.ic.ts @@ -1,7 +1,10 @@ import type { _SERVICE as BackendActor } from '$declarations/backend/backend.did'; import { idlFactory as idlCertifiedFactoryBackend } from '$declarations/backend/backend.factory.certified.did'; import { idlFactory as idlFactoryBackend } from '$declarations/backend/backend.factory.did'; -import { BACKEND_CANISTER_ID } from '$lib/constants/app.constants'; +import type { _SERVICE as SignerActor } from '$declarations/signer/signer.did'; +import { idlFactory as idlCertifiedFactorySigner } from '$declarations/signer/signer.factory.certified.did'; +import { idlFactory as idlFactorySigner } from '$declarations/signer/signer.factory.did'; +import { BACKEND_CANISTER_ID, SIGNER_CANISTER_ID } from '$lib/constants/app.constants'; import { i18n } from '$lib/stores/i18n.store'; import type { OptionIdentity } from '$lib/types/identity'; import { Actor, type ActorMethod, type ActorSubclass, type Identity } from '@dfinity/agent'; @@ -11,7 +14,7 @@ import { assertNonNullish, isNullish } from '@dfinity/utils'; import { get } from 'svelte/store'; import { getAgent } from './agents.ic'; -let actors: { backend?: BackendActor } | undefined | null = undefined; +let actors: { backend?: BackendActor; signer?: SignerActor } | undefined | null = undefined; export const getBackendActor = async ({ identity, @@ -42,6 +45,36 @@ export const getBackendActor = async ({ return backend; }; +export const getSignerActor = async ({ + identity, + certified = true +}: { + identity: OptionIdentity; + certified?: boolean; +}): Promise => { + // TODO: Delete this dependency once the signer has been stripped down. + assertNonNullish(identity, get(i18n).auth.error.no_internet_identity); + + const { signer } = actors ?? { signer: undefined }; + + if (isNullish(signer)) { + const actor = await createActor({ + canisterId: SIGNER_CANISTER_ID, + idlFactory: certified ? idlCertifiedFactorySigner : idlFactorySigner, + identity + }); + + actors = { + ...(actors ?? {}), + signer: actor + }; + + return actor; + } + + return signer; +}; + export const clearActors = () => (actors = null); const createActor = async >({ diff --git a/src/frontend/src/lib/api/backend.api.ts b/src/frontend/src/lib/api/backend.api.ts index 196f3f869b..ec321243dc 100644 --- a/src/frontend/src/lib/api/backend.api.ts +++ b/src/frontend/src/lib/api/backend.api.ts @@ -1,69 +1,17 @@ import type { AddUserCredentialError, - BitcoinNetwork, CredentialSpec, CustomToken, GetUserProfileError, - SignRequest, UserProfile, UserToken } from '$declarations/backend/backend.did'; import { getBackendActor } from '$lib/actors/actors.ic'; -import type { EthAddress } from '$lib/types/address'; import type { OptionIdentity } from '$lib/types/identity'; import type { Identity } from '@dfinity/agent'; import type { Principal } from '@dfinity/principal'; import { toNullable, type QueryParams } from '@dfinity/utils'; -export const getBtcAddress = async ({ - identity, - network -}: { - identity: OptionIdentity; - network: BitcoinNetwork; -}): Promise => { - const { caller_btc_address } = await getBackendActor({ identity }); - return caller_btc_address(network); -}; - -export const getEthAddress = async (identity: OptionIdentity): Promise => { - const { caller_eth_address } = await getBackendActor({ identity }); - return caller_eth_address(); -}; - -export const signTransaction = async ({ - transaction, - identity -}: { - transaction: SignRequest; - identity: OptionIdentity; -}): Promise => { - const { sign_transaction } = await getBackendActor({ identity }); - return sign_transaction(transaction); -}; - -export const signMessage = async ({ - message, - identity -}: { - message: string; - identity: OptionIdentity; -}): Promise => { - const { personal_sign } = await getBackendActor({ identity }); - return personal_sign(message); -}; - -export const signPrehash = async ({ - hash, - identity -}: { - hash: string; - identity: OptionIdentity; -}): Promise => { - const { sign_prehash } = await getBackendActor({ identity }); - return sign_prehash(hash); -}; - export const listUserTokens = async ({ identity, certified = true diff --git a/src/frontend/src/lib/api/signer.api.ts b/src/frontend/src/lib/api/signer.api.ts new file mode 100644 index 0000000000..a24de3bd27 --- /dev/null +++ b/src/frontend/src/lib/api/signer.api.ts @@ -0,0 +1,53 @@ +import type { BitcoinNetwork, SignRequest } from '$declarations/signer/signer.did'; +import { getBackendActor } from '$lib/actors/actors.ic'; +import type { EthAddress } from '$lib/types/address'; +import type { OptionIdentity } from '$lib/types/identity'; + +export const getBtcAddress = async ({ + identity, + network +}: { + identity: OptionIdentity; + network: BitcoinNetwork; +}): Promise => { + const { caller_btc_address } = await getBackendActor({ identity }); + return caller_btc_address(network); +}; + +export const getEthAddress = async (identity: OptionIdentity): Promise => { + const { caller_eth_address } = await getBackendActor({ identity }); + return caller_eth_address(); +}; + +export const signTransaction = async ({ + transaction, + identity +}: { + transaction: SignRequest; + identity: OptionIdentity; +}): Promise => { + const { sign_transaction } = await getBackendActor({ identity }); + return sign_transaction(transaction); +}; + +export const signMessage = async ({ + message, + identity +}: { + message: string; + identity: OptionIdentity; +}): Promise => { + const { personal_sign } = await getBackendActor({ identity }); + return personal_sign(message); +}; + +export const signPrehash = async ({ + hash, + identity +}: { + hash: string; + identity: OptionIdentity; +}): Promise => { + const { sign_prehash } = await getBackendActor({ identity }); + return sign_prehash(hash); +}; diff --git a/src/frontend/src/lib/constants/app.constants.ts b/src/frontend/src/lib/constants/app.constants.ts index 5f9d2ace2f..e9a82c2d93 100644 --- a/src/frontend/src/lib/constants/app.constants.ts +++ b/src/frontend/src/lib/constants/app.constants.ts @@ -43,6 +43,12 @@ export const BACKEND_CANISTER_ID = LOCAL ? import.meta.env.VITE_STAGING_BACKEND_CANISTER_ID : import.meta.env.VITE_IC_BACKEND_CANISTER_ID; +export const SIGNER_CANISTER_ID = LOCAL + ? import.meta.env.VITE_LOCAL_SIGNER_CANISTER_ID + : STAGING + ? import.meta.env.VITE_STAGING_SIGNER_CANISTER_ID + : import.meta.env.VITE_IC_SIGNER_CANISTER_ID; + // How long the delegation identity should remain valid? // e.g. BigInt(60 * 60 * 1000 * 1000 * 1000) = 1 hour in nanoseconds export const AUTH_MAX_TIME_TO_LIVE = BigInt(60 * 60 * 1000 * 1000 * 1000); diff --git a/src/frontend/src/lib/services/address.services.ts b/src/frontend/src/lib/services/address.services.ts index afd3908488..cb2cc052a2 100644 --- a/src/frontend/src/lib/services/address.services.ts +++ b/src/frontend/src/lib/services/address.services.ts @@ -1,6 +1,5 @@ import { BTC_MAINNET_TOKEN_ID } from '$env/tokens.btc.env'; import { ETHEREUM_TOKEN_ID } from '$env/tokens.env'; -import { getBtcAddress, getEthAddress } from '$lib/api/backend.api'; import { getIdbBtcAddressMainnet, getIdbEthAddress, @@ -9,6 +8,7 @@ import { updateIdbBtcAddressMainnetLastUsage, updateIdbEthAddressLastUsage } from '$lib/api/idb.api'; +import { getBtcAddress, getEthAddress } from '$lib/api/signer.api'; import { addressStore } from '$lib/stores/address.store'; import { authStore } from '$lib/stores/auth.store'; import { i18n } from '$lib/stores/i18n.store';