diff --git a/packages/pkh-ethereum/src/authmethod.ts b/packages/pkh-ethereum/src/authmethod.ts index 2c32ec2e..ebdde514 100644 --- a/packages/pkh-ethereum/src/authmethod.ts +++ b/packages/pkh-ethereum/src/authmethod.ts @@ -12,19 +12,32 @@ export const VERSION = '1' */ export const CHAIN_NAMESPACE = 'eip155' +function toAccountId(didOrAccount: string | AccountId): AccountId { + if (typeof didOrAccount === 'string') { + if (!didOrAccount.startsWith(`did:pkh:${CHAIN_NAMESPACE}`)) { + throw new Error(`Invalid DID string: ${didOrAccount}`) + } + return new AccountId(didOrAccount.slice(8)) + } + return didOrAccount +} + export namespace EthereumWebAuth { /** * Get a configured authMethod for an Ethereum account in a web based environment */ // eslint-disable-next-line @typescript-eslint/require-await - export async function getAuthMethod(ethProvider: any, account: AccountId): Promise { + export async function getAuthMethod( + ethProvider: any, + account: AccountId | string + ): Promise { if (typeof window === 'undefined') throw new Error('Web Auth method requires browser environment') const domain = (window as Window).location.hostname return async (opts: AuthMethodOpts): Promise => { opts.domain = domain - return createCACAO(opts, ethProvider, account) + return createCACAO(opts, ethProvider, toAccountId(account)) } } } @@ -43,7 +56,7 @@ export namespace EthereumNodeAuth { return async (opts: AuthMethodOpts): Promise => { opts.domain = domain - return createCACAO(opts, ethProvider, account) + return createCACAO(opts, ethProvider, toAccountId(account)) } } } @@ -89,3 +102,10 @@ export async function getAccountId(ethProvider: any, address: string): Promise { + return `did:pkh:${(await getAccountId(ethProvider, address)).toString()}` +} diff --git a/packages/pkh-solana/src/authmethod.ts b/packages/pkh-solana/src/authmethod.ts index 62b41690..4a30e6a3 100644 --- a/packages/pkh-solana/src/authmethod.ts +++ b/packages/pkh-solana/src/authmethod.ts @@ -17,19 +17,32 @@ export const chainIdMap = { type SolanaNetwork = 'mainnet' | 'testnet' | 'devnet' +function toAccountId(didOrAccount: string | AccountId): AccountId { + if (typeof didOrAccount === 'string') { + if (!didOrAccount.startsWith(`did:pkh:${CHAIN_NAMESPACE}`)) { + throw new Error(`Invalid DID string: ${didOrAccount}`) + } + return new AccountId(didOrAccount.slice(8)) + } + return didOrAccount +} + export namespace SolanaWebAuth { /** * Get a configured authMethod for a Solana account in a web based environment */ // eslint-disable-next-line @typescript-eslint/require-await - export async function getAuthMethod(solProvider: any, account: AccountId): Promise { + export async function getAuthMethod( + solProvider: any, + account: AccountId | string + ): Promise { if (typeof window === 'undefined') throw new Error('Web Auth method requires browser environment') const domain = (window as Window).location.hostname return async (opts: AuthMethodOpts): Promise => { opts.domain = domain - return createCACAO(opts, solProvider, account) + return createCACAO(opts, solProvider, toAccountId(account)) } } } @@ -41,14 +54,14 @@ export namespace SolanaNodeAuth { // eslint-disable-next-line @typescript-eslint/require-await export async function getAuthMethod( ethProvider: any, - account: AccountId, + account: AccountId | string, appName: string ): Promise { const domain = appName return async (opts: AuthMethodOpts): Promise => { opts.domain = domain - return createCACAO(opts, ethProvider, account) + return createCACAO(opts, ethProvider, toAccountId(account)) } } } @@ -127,6 +140,13 @@ export async function getAccountId(solConnection: any, address: string): Promise return new AccountId({ address, chainId }) } +/** + * Helper function to get a DID for an Solana account by Solana Connection interface, Connection must implement 'getGenesisHash()' + */ +export async function getDID(solConnection: any, address: string): Promise { + return `did:pkh:${(await getAccountId(solConnection, address)).toString()}` +} + /** * Helper function to get an accountId (CAIP10) for an Solana account by network string 'mainet' | 'testnet' | 'devenet' */ @@ -134,3 +154,10 @@ export function getAccountIdByNetwork(network: SolanaNetwork, address: string): const chainId = `${CHAIN_NAMESPACE}:${chainIdMap[network]}` return new AccountId({ address, chainId }) } + +/** + * Helper function to get a DID for an Solana account by network string 'mainet' | 'testnet' | 'devenet' + */ +export function getDIDByNetwork(network: SolanaNetwork, address: string): string { + return `did:pkh:${getAccountIdByNetwork(network, address).toString()}` +} diff --git a/packages/pkh-tezos/src/authmethod.ts b/packages/pkh-tezos/src/authmethod.ts index b0e1e710..c9dbf4f7 100644 --- a/packages/pkh-tezos/src/authmethod.ts +++ b/packages/pkh-tezos/src/authmethod.ts @@ -14,6 +14,16 @@ export const chainIdMap = { type TezosNetwork = 'mainnet' | 'devnet' +function toAccountId(didOrAccount: string | AccountId): AccountId { + if (typeof didOrAccount === 'string') { + if (!didOrAccount.startsWith(`did:pkh:${CHAIN_NAMESPACE}`)) { + throw new Error(`Invalid DID string: ${didOrAccount}`) + } + return new AccountId(didOrAccount.slice(8)) + } + return didOrAccount +} + export namespace TezosWebAuth { // eslint-disable-next-line @typescript-eslint/require-await export async function getAuthMethod(tzProvider: any, account: AccountId): Promise { @@ -23,7 +33,7 @@ export namespace TezosWebAuth { return async (opts: AuthMethodOpts): Promise => { opts.domain = domain - return createCACAO(opts, tzProvider, account) + return createCACAO(opts, tzProvider, toAccountId(account)) } } } @@ -102,6 +112,10 @@ export async function getAccountId(tzProvider: any, address: string): Promise { + return `did:pkh:${(await getAccountId(tzProvider, address)).toString()}` +} + export function getAccountIdByNetwork(network: TezosNetwork, address: string): AccountId { const chainId = `${CHAIN_NAMESPACE}:${chainIdMap[network]}` return new AccountId({ address, chainId })