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

Support passing DID in getAuthMethod #132

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 23 additions & 3 deletions packages/pkh-ethereum/src/authmethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuthMethod> {
export async function getAuthMethod(
ethProvider: any,
account: AccountId | string
): Promise<AuthMethod> {
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<Cacao> => {
opts.domain = domain
return createCACAO(opts, ethProvider, account)
return createCACAO(opts, ethProvider, toAccountId(account))
}
}
}
Expand All @@ -43,7 +56,7 @@ export namespace EthereumNodeAuth {

return async (opts: AuthMethodOpts): Promise<Cacao> => {
opts.domain = domain
return createCACAO(opts, ethProvider, account)
return createCACAO(opts, ethProvider, toAccountId(account))
}
}
}
Expand Down Expand Up @@ -89,3 +102,10 @@ export async function getAccountId(ethProvider: any, address: string): Promise<A
const chainId = `${CHAIN_NAMESPACE}:${ethChainId}`
return new AccountId({ address, chainId })
}

/**
* Helper function to get a PKH DID for an Ethereum account, uses ethProvider to get chainId/network
*/
export async function getDID(ethProvider: any, address: string): Promise<string> {
return `did:pkh:${(await getAccountId(ethProvider, address)).toString()}`
}
35 changes: 31 additions & 4 deletions packages/pkh-solana/src/authmethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuthMethod> {
export async function getAuthMethod(
solProvider: any,
account: AccountId | string
): Promise<AuthMethod> {
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<Cacao> => {
opts.domain = domain
return createCACAO(opts, solProvider, account)
return createCACAO(opts, solProvider, toAccountId(account))
}
}
}
Expand All @@ -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<AuthMethod> {
const domain = appName

return async (opts: AuthMethodOpts): Promise<Cacao> => {
opts.domain = domain
return createCACAO(opts, ethProvider, account)
return createCACAO(opts, ethProvider, toAccountId(account))
}
}
}
Expand Down Expand Up @@ -127,10 +140,24 @@ 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<string> {
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'
*/
export function getAccountIdByNetwork(network: SolanaNetwork, address: string): AccountId {
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()}`
}
16 changes: 15 additions & 1 deletion packages/pkh-tezos/src/authmethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuthMethod> {
Expand All @@ -23,7 +33,7 @@ export namespace TezosWebAuth {

return async (opts: AuthMethodOpts): Promise<Cacao> => {
opts.domain = domain
return createCACAO(opts, tzProvider, account)
return createCACAO(opts, tzProvider, toAccountId(account))
}
}
}
Expand Down Expand Up @@ -102,6 +112,10 @@ export async function getAccountId(tzProvider: any, address: string): Promise<Ac
return new AccountId({ address, chainId })
}

export async function getDID(tzProvider: any, address: string): Promise<string> {
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 })
Expand Down