Skip to content

Commit

Permalink
Merge pull request #782 from airgap-it/fix/public_key_resolution
Browse files Browse the repository at this point in the history
fix: publicKey resolution
  • Loading branch information
jsamol authored Jun 21, 2024
2 parents fbc47b6 + 3d6426a commit 71d9169
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 24 deletions.
2 changes: 1 addition & 1 deletion examples/dapp.html
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@
},
featuredWallets: ['kukai', 'metamask', 'airgap'],
network: {
type: beacon.NetworkType.MAINNET
type: beacon.NetworkType.GHOSTNET
},
enableMetrics: true
// matrixNodes: ['test.papers.tech', 'test2.papers.tech', 'matrix.papers.tech']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
Serializer,
ClientEvents,
Logger,
WCStorage
WCStorage,
SDK_VERSION
} from '@airgap/beacon-core'
import Client from '@walletconnect/sign-client'
import { ProposalTypes, SessionTypes, SignClientTypes } from '@walletconnect/types'
Expand Down Expand Up @@ -45,9 +46,10 @@ import {
StorageKey,
TransportType
} from '@airgap/beacon-types'
import { generateGUID, getAddressFromPublicKey } from '@airgap/beacon-utils'
import { generateGUID, getAddressFromPublicKey, isPublicKeySC } from '@airgap/beacon-utils'

const TEZOS_PLACEHOLDER = 'tezos'
const BEACON_SDK_VERSION = 'beacon_sdk_version'
const logger = new Logger('WalletConnectCommunicationClient')

export interface PermissionScopeParam {
Expand Down Expand Up @@ -99,7 +101,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
public signClient: Client | undefined
public storage: WCStorage = new WCStorage()
private session: SessionTypes.Struct | undefined
private activeAccount: string | undefined
private activeAccountOrPbk: string | undefined
private activeNetwork: string | undefined
readonly disconnectionEvents: Set<string> = new Set()
private pingInterval: NodeJS.Timeout | undefined
Expand Down Expand Up @@ -349,7 +351,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
const accounts = this.getTezosNamespace(session.namespaces).accounts
const addressOrPbk = accounts[0].split(':', 3)[2]

if (addressOrPbk.startsWith('edpk')) {
if (isPublicKeySC(addressOrPbk)) {
publicKey = addressOrPbk
} else {
if (network.type !== this.wcOptions.network) {
Expand Down Expand Up @@ -401,7 +403,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
throw new MissingRequiredScope(PermissionScopeMethods.GET_ACCOUNTS)
}

if (this.activeAccount) {
if (this.activeAccountOrPbk) {
try {
await this.openSession()
} catch (error: any) {
Expand Down Expand Up @@ -443,7 +445,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
request: {
method: PermissionScopeMethods.SIGN,
params: {
account: account,
account: isPublicKeySC(account) ? await getAddressFromPublicKey(account) : account,
payload: signPayloadRequest.payload
}
}
Expand Down Expand Up @@ -509,7 +511,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
request: {
method: PermissionScopeMethods.OPERATION_REQUEST,
params: {
account,
account: isPublicKeySC(account) ? await getAddressFromPublicKey(account) : account,
operations: operationRequest.operationDetails
}
}
Expand Down Expand Up @@ -623,6 +625,9 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
},
optionalNamespaces: {
[TEZOS_PLACEHOLDER]: this.permissionScopeParamsToNamespaces(optionalPermissionScopeParams)
},
sessionProperties: {
[BEACON_SDK_VERSION]: SDK_VERSION
}
}

Expand Down Expand Up @@ -674,7 +679,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient {

if (session?.controller !== this.session?.controller) {
logger.debug('Controller doesnt match, closing active session', [session.pairingTopic])
this.activeAccount && this.closeActiveSession(this.activeAccount, false)
this.activeAccountOrPbk && this.closeActiveSession(this.activeAccountOrPbk, false)
this.session = undefined // close the previous session
}

Expand Down Expand Up @@ -794,15 +799,13 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
let publicKey: string | undefined

this.activeNetwork = chainId
this.activeAccountOrPbk = addressOrPbk

if (addressOrPbk.startsWith('edpk')) {
publicKey = addressOrPbk
this.activeAccount = await getAddressFromPublicKey(publicKey)
} else {
this.activeAccount = addressOrPbk
if (!isPublicKeySC(addressOrPbk)) {
const result = await this.fetchAccounts(session.topic, `${TEZOS_PLACEHOLDER}:${chainId}`)

publicKey = result?.find(({ address: _address }) => addressOrPbk === _address)?.pubkey
} else {
publicKey = addressOrPbk
}

if (!publicKey) {
Expand Down Expand Up @@ -842,7 +845,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
session = await this.onPairingClosed(signClient, trigger.topic)
}

if (!this.activeAccount) {
if (!this.activeAccountOrPbk) {
const fun = this.eventHandlers.get(ClientEvents.RESET_STATE)
fun && fun(TransportType.WALLETCONNECT)
}
Expand Down Expand Up @@ -1015,6 +1018,9 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
optionalNamespaces: {
[TEZOS_PLACEHOLDER]: this.permissionScopeParamsToNamespaces(optionalPermissionScopeParams)
},
sessionProperties: {
[BEACON_SDK_VERSION]: SDK_VERSION
},
pairingTopic
}

Expand All @@ -1031,7 +1037,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
// if I have successfully opened a session and I already have one opened
if (session?.controller !== this.session?.controller) {
logger.debug('Controller doesnt match, closing active session', [pairingTopic])
this.activeAccount && this.closeActiveSession(this.activeAccount, false)
this.activeAccountOrPbk && this.closeActiveSession(this.activeAccountOrPbk, false)
this.session = undefined // close the previous session
}

Expand Down Expand Up @@ -1261,7 +1267,7 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
private setDefaultAccountAndNetwork() {
const activeAccount = this.getAccounts()
if (activeAccount.length) {
this.activeAccount = activeAccount[0]
this.activeAccountOrPbk = activeAccount[0]
}
const activeNetwork = this.getNetworks()
if (activeNetwork.length) {
Expand Down Expand Up @@ -1368,16 +1374,16 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
* @error ActiveAccountUnspecified thrown when there are multiple Tezos account in the session and none is set as the active one
*/
async getPKH() {
if (!this.activeAccount) {
if (!this.activeAccountOrPbk) {
this.getSession()
throw new ActiveAccountUnspecified()
}
return this.activeAccount
return this.activeAccountOrPbk
}

private clearState() {
this.session = undefined
this.activeAccount = undefined
this.activeAccountOrPbk = undefined
this.activeNetwork = undefined
}
}
4 changes: 2 additions & 2 deletions packages/beacon-transport-walletconnect/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class ActiveAccountUnspecified extends Error {

/**
* @category Error
* @description Error that indicates the combinaison pkh-network is not part of the active session
* @description Error that indicates the combination pkh-network is not part of the active session
*/
export class InvalidNetworkOrAccount extends Error {
name = 'InvalidNetworkOrAccount'
Expand All @@ -95,7 +95,7 @@ export class InvalidNetworkOrAccount extends Error {
public pkh: string
) {
super(
`No permission. The combinaison "${network}" and "${pkh}" is not part of the active session.`
`No permission. The combination "${network}" and "${pkh}" is not part of the active session.`
)
}
}
3 changes: 2 additions & 1 deletion packages/beacon-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export {
signMessage,
isValidAddress,
prefixPublicKey,
encodePoeChallengePayload
encodePoeChallengePayload,
isPublicKeySC
} from './utils/crypto'
export { generateGUID } from './utils/generate-uuid'

Expand Down
24 changes: 24 additions & 0 deletions packages/beacon-utils/src/utils/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ export async function getAddressFromPublicKey(publicKey: string): Promise<string
p2pk: {
length: 55,
prefix: Buffer.from(new Uint8Array([6, 161, 164]))
},
// tz4...
BLpk: {
length: 55,
prefix: Buffer.from(new Uint8Array([6, 161, 166]))
}
}

Expand Down Expand Up @@ -292,4 +297,23 @@ export function encodePoeChallengePayload(payload: string) {
)
}

/**
* Shallow Check (SC): Perform a superficial check to determine if the string contains a public key.
* Do not use this function to validate the key itself.
* @param publicKey the public key to analyze
* @returns true if it contains a known prefix, false otherwise
*/
export function isPublicKeySC(publicKey: string): boolean {
if (!publicKey) {
return false
}

return (
publicKey.startsWith('edpk') ||
publicKey.startsWith('sppk') ||
publicKey.startsWith('p2pk') ||
publicKey.startsWith('BLpk')
)
}

/* eslint-enable prefer-arrow/prefer-arrow-functions */

0 comments on commit 71d9169

Please sign in to comment.