From a12cb3542a7e944788927324238acb2ccba9b04f Mon Sep 17 00:00:00 2001 From: Joel Thorstensson Date: Mon, 2 Jan 2023 11:12:56 +0000 Subject: [PATCH 1/6] Update authmethod.ts --- packages/pkh-ethereum/src/authmethod.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/pkh-ethereum/src/authmethod.ts b/packages/pkh-ethereum/src/authmethod.ts index 2c32ec2e..95ce17fa 100644 --- a/packages/pkh-ethereum/src/authmethod.ts +++ b/packages/pkh-ethereum/src/authmethod.ts @@ -17,10 +17,16 @@ 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 + if (typeof account === 'string') { + if (!account.startsWith(`did:pkh:${CHAIN_NAMESPACE}`)) { + throw new Error(`invalid DID string: ${account}`) + } + account = AccountId.parse(account.slice(8)) + } return async (opts: AuthMethodOpts): Promise => { opts.domain = domain From 26d66a0e2bd65be9eb19ebe49d9b6bc13814c8c6 Mon Sep 17 00:00:00 2001 From: Joel Thorstensson Date: Mon, 2 Jan 2023 11:16:37 +0000 Subject: [PATCH 2/6] Update authmethod.ts --- packages/pkh-ethereum/src/authmethod.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pkh-ethereum/src/authmethod.ts b/packages/pkh-ethereum/src/authmethod.ts index 95ce17fa..b6b6b4fa 100644 --- a/packages/pkh-ethereum/src/authmethod.ts +++ b/packages/pkh-ethereum/src/authmethod.ts @@ -25,7 +25,7 @@ export namespace EthereumWebAuth { if (!account.startsWith(`did:pkh:${CHAIN_NAMESPACE}`)) { throw new Error(`invalid DID string: ${account}`) } - account = AccountId.parse(account.slice(8)) + account = new AccountId(account.slice(8)) } return async (opts: AuthMethodOpts): Promise => { From 930428ad8a3391cd01140714a9c87585c6b3c165 Mon Sep 17 00:00:00 2001 From: Joel Thorstensson Date: Mon, 2 Jan 2023 11:52:11 +0000 Subject: [PATCH 3/6] Add support in all pkh providers --- packages/pkh-ethereum/src/authmethod.ts | 20 ++++++++++++-------- packages/pkh-solana/src/authmethod.ts | 18 ++++++++++++++---- packages/pkh-tezos/src/authmethod.ts | 12 +++++++++++- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/packages/pkh-ethereum/src/authmethod.ts b/packages/pkh-ethereum/src/authmethod.ts index b6b6b4fa..90c1cd5e 100644 --- a/packages/pkh-ethereum/src/authmethod.ts +++ b/packages/pkh-ethereum/src/authmethod.ts @@ -12,6 +12,16 @@ 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 as AccountId +} + export namespace EthereumWebAuth { /** * Get a configured authMethod for an Ethereum account in a web based environment @@ -21,16 +31,10 @@ export namespace EthereumWebAuth { if (typeof window === 'undefined') throw new Error('Web Auth method requires browser environment') const domain = (window as Window).location.hostname - if (typeof account === 'string') { - if (!account.startsWith(`did:pkh:${CHAIN_NAMESPACE}`)) { - throw new Error(`invalid DID string: ${account}`) - } - account = new AccountId(account.slice(8)) - } return async (opts: AuthMethodOpts): Promise => { opts.domain = domain - return createCACAO(opts, ethProvider, account) + return createCACAO(opts, ethProvider, toAccountId(account)) } } } @@ -49,7 +53,7 @@ export namespace EthereumNodeAuth { return async (opts: AuthMethodOpts): Promise => { opts.domain = domain - return createCACAO(opts, ethProvider, account) + return createCACAO(opts, ethProvider, toAccountId(account)) } } } diff --git a/packages/pkh-solana/src/authmethod.ts b/packages/pkh-solana/src/authmethod.ts index 62b41690..99817ca4 100644 --- a/packages/pkh-solana/src/authmethod.ts +++ b/packages/pkh-solana/src/authmethod.ts @@ -17,19 +17,29 @@ 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 as AccountId +} + 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 +51,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)) } } } diff --git a/packages/pkh-tezos/src/authmethod.ts b/packages/pkh-tezos/src/authmethod.ts index b0e1e710..1911fc5b 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 as AccountId +} + 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)) } } } From 54d33ffaca0504e927220dae76cb033888e69c5e Mon Sep 17 00:00:00 2001 From: Joel Thorstensson Date: Mon, 2 Jan 2023 11:58:27 +0000 Subject: [PATCH 4/6] Add functions to get DID --- packages/pkh-ethereum/src/authmethod.ts | 7 +++++++ packages/pkh-solana/src/authmethod.ts | 14 ++++++++++++++ packages/pkh-tezos/src/authmethod.ts | 4 ++++ 3 files changed, 25 insertions(+) diff --git a/packages/pkh-ethereum/src/authmethod.ts b/packages/pkh-ethereum/src/authmethod.ts index 90c1cd5e..62756159 100644 --- a/packages/pkh-ethereum/src/authmethod.ts +++ b/packages/pkh-ethereum/src/authmethod.ts @@ -99,3 +99,10 @@ export async function getAccountId(ethProvider: any, address: string): Promise { + return `did:pkh:${getAccountId(ethProvider, address).toString()}` +} diff --git a/packages/pkh-solana/src/authmethod.ts b/packages/pkh-solana/src/authmethod.ts index 99817ca4..e9158865 100644 --- a/packages/pkh-solana/src/authmethod.ts +++ b/packages/pkh-solana/src/authmethod.ts @@ -137,6 +137,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:${getAccountId(solConnection, address).toString()}` +} + /** * Helper function to get an accountId (CAIP10) for an Solana account by network string 'mainet' | 'testnet' | 'devenet' */ @@ -144,3 +151,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 async function getDIDByNetwork(network: SolanaNetwork, address: string): Promise { + return `did:pkh:${getAccountIdByNetwork(network, address).toString()}` +} diff --git a/packages/pkh-tezos/src/authmethod.ts b/packages/pkh-tezos/src/authmethod.ts index 1911fc5b..e433ce99 100644 --- a/packages/pkh-tezos/src/authmethod.ts +++ b/packages/pkh-tezos/src/authmethod.ts @@ -112,6 +112,10 @@ export async function getAccountId(tzProvider: any, address: string): Promise { + return `did:pkh:${getAccountId(tzProvider, address).toString()}` +} + export function getAccountIdByNetwork(network: TezosNetwork, address: string): AccountId { const chainId = `${CHAIN_NAMESPACE}:${chainIdMap[network]}` return new AccountId({ address, chainId }) From 9247dafb81f5c233b5ff4bd1744344e496679932 Mon Sep 17 00:00:00 2001 From: Joel Thorstensson Date: Mon, 2 Jan 2023 12:10:30 +0000 Subject: [PATCH 5/6] Fix lint --- packages/pkh-ethereum/src/authmethod.ts | 11 +++++++---- packages/pkh-solana/src/authmethod.ts | 13 ++++++++----- packages/pkh-tezos/src/authmethod.ts | 6 +++--- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/packages/pkh-ethereum/src/authmethod.ts b/packages/pkh-ethereum/src/authmethod.ts index 62756159..ebdde514 100644 --- a/packages/pkh-ethereum/src/authmethod.ts +++ b/packages/pkh-ethereum/src/authmethod.ts @@ -12,14 +12,14 @@ export const VERSION = '1' */ export const CHAIN_NAMESPACE = 'eip155' -function toAccountId (didOrAccount: string | AccountId): AccountId { +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 as AccountId + return didOrAccount } export namespace EthereumWebAuth { @@ -27,7 +27,10 @@ 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 | string): 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 @@ -104,5 +107,5 @@ export async function getAccountId(ethProvider: any, address: string): Promise { - return `did:pkh:${getAccountId(ethProvider, address).toString()}` + 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 e9158865..7a406c89 100644 --- a/packages/pkh-solana/src/authmethod.ts +++ b/packages/pkh-solana/src/authmethod.ts @@ -17,14 +17,14 @@ export const chainIdMap = { type SolanaNetwork = 'mainnet' | 'testnet' | 'devnet' -function toAccountId (didOrAccount: string | AccountId): AccountId { +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 as AccountId + return didOrAccount } export namespace SolanaWebAuth { @@ -32,7 +32,10 @@ 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 | string): 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 @@ -141,7 +144,7 @@ export async function getAccountId(solConnection: any, address: string): Promise * 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:${getAccountId(solConnection, address).toString()}` + return `did:pkh:${(await getAccountId(solConnection, address)).toString()}` } /** @@ -155,6 +158,6 @@ export function getAccountIdByNetwork(network: SolanaNetwork, address: string): /** * Helper function to get a DID for an Solana account by network string 'mainet' | 'testnet' | 'devenet' */ -export async function getDIDByNetwork(network: SolanaNetwork, address: string): Promise { +export function getDIDByNetwork(network: SolanaNetwork, address: string): Promise { return `did:pkh:${getAccountIdByNetwork(network, address).toString()}` } diff --git a/packages/pkh-tezos/src/authmethod.ts b/packages/pkh-tezos/src/authmethod.ts index e433ce99..c9dbf4f7 100644 --- a/packages/pkh-tezos/src/authmethod.ts +++ b/packages/pkh-tezos/src/authmethod.ts @@ -14,14 +14,14 @@ export const chainIdMap = { type TezosNetwork = 'mainnet' | 'devnet' -function toAccountId (didOrAccount: string | AccountId): AccountId { +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 as AccountId + return didOrAccount } export namespace TezosWebAuth { @@ -113,7 +113,7 @@ export async function getAccountId(tzProvider: any, address: string): Promise { - return `did:pkh:${getAccountId(tzProvider, address).toString()}` + return `did:pkh:${(await getAccountId(tzProvider, address)).toString()}` } export function getAccountIdByNetwork(network: TezosNetwork, address: string): AccountId { From 589c904847b2a16f34941892e925d435bad3a037 Mon Sep 17 00:00:00 2001 From: Joel Thorstensson Date: Mon, 2 Jan 2023 12:12:20 +0000 Subject: [PATCH 6/6] Fix type --- packages/pkh-solana/src/authmethod.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pkh-solana/src/authmethod.ts b/packages/pkh-solana/src/authmethod.ts index 7a406c89..4a30e6a3 100644 --- a/packages/pkh-solana/src/authmethod.ts +++ b/packages/pkh-solana/src/authmethod.ts @@ -158,6 +158,6 @@ export function getAccountIdByNetwork(network: SolanaNetwork, address: string): /** * Helper function to get a DID for an Solana account by network string 'mainet' | 'testnet' | 'devenet' */ -export function getDIDByNetwork(network: SolanaNetwork, address: string): Promise { +export function getDIDByNetwork(network: SolanaNetwork, address: string): string { return `did:pkh:${getAccountIdByNetwork(network, address).toString()}` }