All files / src index.ts

72.34% Statements 68/94
66.66% Branches 18/27
52.38% Functions 11/21
72.34% Lines 68/94

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 34648x             1x                               1x 1x 1x     1x   1x 3x 3x 3x 3x 3x             1x                         1x           3x 3x 3x 3x           3x                             3x                   3x           3x       3x     3x 3x     3x           3x                     3x 3x     3x   3x   3x                     3x           3x           3x       3x 3x 3x               3x                                                                                   2x 2x               2x   2x 2x       2x           2x 2x   2x         2x                   2x   2x               2x                               1x   1x       1x     1x     1x               1x     1x   1x             1x 1x               1x   1x 1x   1x   1x                           1x  
import {
    createIdentityRequest,
    extractSignaturesFromCallback,
    isCallback,
    LinkInfo,
    setTransactionCallback,
    waitForCallback,
} from '@wharfkit/protocol-esr'
import {ReceiveOptions} from '@greymass/buoy'
import {
    AbstractWalletPlugin,
    CallbackPayload,
    Checksum256,
    LoginContext,
    PermissionLevel,
    ResolvedSigningRequest,
    TransactContext,
    UserInterface,
    WalletPlugin,
    WalletPluginConfig,
    WalletPluginLoginResponse,
    WalletPluginMetadata,
    WalletPluginSignResponse,
} from '@wharfkit/session'
import {PrivateKey, PublicKey, UInt64} from '@wharfkit/antelope'
import {sealMessage} from '@wharfkit/sealed-messages'
import WebSocket from 'isomorphic-ws'
 
import defaultTranslations from './translations'
 
export function openPopupWindow(url: string): Window | null {
    const width = 450
    const height = 750
    const left = Math.round(window.screenX + (window.outerWidth - width) / 2)
    const top = Math.round(window.screenY + (window.outerHeight - height) / 2)
    return window.open(
        url,
        'web-authenticator',
        `width=${width},height=${height},left=${left},top=${top}`
    )
}
 
export function openWallet(url: string): Window | null {
    return openPopupWindow(url)
}
 
interface WebAuthenticatorOptions {
    /** The URLs for the web authenticator service, keyed by chain ID */
    urls: Record<string, string>
    /** The buoy service URL for messaging */
    buoyServiceUrl?: string
    /** The buoy WebSocket for messaging */
    buoyWs?: WebSocket
}
 
export class WalletPluginWebAuthenticator extends AbstractWalletPlugin implements WalletPlugin {
    private urls: Record<string, string>
    private buoyServiceUrl: string
    private buoyWs?: WebSocket
 
    constructor(options: WebAuthenticatorOptions) {
        super()
        this.urls = options.urls
        this.buoyServiceUrl = options.buoyServiceUrl || 'https://cb.anchor.link'
        this.buoyWs = options?.buoyWs
    }
 
    /**
     * The logic configuration for the wallet plugin.
     */
    readonly config: WalletPluginConfig = {
        // Allow chain selection since the web authenticator may support multiple chains
        requiresChainSelect: false,
        // Allow permission selection
        requiresPermissionSelect: false,
        // Currently only supports Jungle 4 and Vaulta:
        supportedChains: [
            '73e4385a2708e6d7048834fbc1079f2fabb17b3c125b146af438971e90716c4d',
            'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906',
        ],
    }
 
    /**
     * The metadata for the wallet plugin to be displayed in the user interface.
     */
    readonly metadata: WalletPluginMetadata = WalletPluginMetadata.from({
        name: 'Web Authenticator',
        description: 'Sign transactions using a web-based authenticator',
        homepage: 'https://github.com/wharfkit/wallet-plugin-web-authenticator',
    })
 
    /**
     * Unique identifier for this wallet plugin
     */
    get id(): string {
        return 'web-authenticator'
    }
 
    /**
     * The translations for this plugin
     */
    translations = defaultTranslations
 
    getChainUrl(context: LoginContext | TransactContext): string {
        // default to first
        let url = this.urls[0]
 
        // override if chain specified
        if (context.chain) {
            url = this.urls[String(context.chain.id)]
        }
 
        Iif (!url) {
            throw new Error(
                `No web authenticator URL configured for chain ID: ${context.chain?.id}`
            )
        }
 
        return url
    }
 
    /**
     * Opens a popup window with the given URL and waits for it to complete
     */
    private async openPopup(
        url: string,
        receiveOptions: ReceiveOptions,
        ui?: UserInterface
    ): Promise<{payload: CallbackPayload}> {
        return new Promise((resolve, reject) => {
            const t = ui?.getTranslate(this.id)
 
            // Show status message using WharfKit UI
            ui?.status('Opening wallet window...')
 
            const popup = openPopupWindow(url)
 
            Iif (!popup) {
                return this.showManualPopupPrompt(url, receiveOptions, ui)
                    .then((response) => {
                        resolve(response)
                    })
                    .catch((error) => {
                        reject(error)
                    })
            }
 
            // Update status
            ui?.prompt({
                title: 'Approve',
                body: 'Please approve the transaction in the wallet window.',
                elements: [],
            })
 
            const checkClosedInterval = setInterval(() => {
                Iif (popup?.closed) {
                    clearInterval(checkClosedInterval)
                    ui?.status('Transaction cancelled')
                    reject(new Error('Transaction cancelled by user'))
                }
            }, 1000)
 
            waitForCallback(receiveOptions, this.buoyWs, t)
                .then((response) => {
                    popup?.close()
                    ui?.status('Transaction approved successfully')
                    resolve({payload: response})
                })
                .catch(() => {
                    popup?.close()
                    ui?.status('Transaction cancelled')
                    reject(new Error('Transaction cancelled by user'))
                })
                .finally(() => {
                    clearInterval(checkClosedInterval)
                })
        })
    }
 
    /**
     * Shows error prompt and handles retry logic
     */
    private showManualPopupPrompt(
        url: string,
        receiveOptions: ReceiveOptions,
        ui?: UserInterface
    ): Promise<{payload: CallbackPayload}> {
        return new Promise((resolve, reject) => {
            ui?.prompt({
                title: 'Pop-up blocked',
                body: `Pop-up blocked by your browser. Open the wallet window manually.`,
                elements: [
                    {
                        type: 'button',
                        data: {
                            label: 'Open Wallet',
                            onClick: () => {
                                this.openPopup(url, receiveOptions, ui)
                                    .then((response) => {
                                        resolve(response)
                                    })
                                    .catch((error) => {
                                        reject(error)
                                    })
                            },
                        },
                    },
                ],
            })
        })
    }
 
    /**
     * Performs login by opening the web authenticator in a popup
     */
    async login(context: LoginContext): Promise<WalletPluginLoginResponse> {
        try {
            context.appName = context.appName || 'Unknown App'
 
            // Create the identity request to be presented to the user
            const {
                callback: receiveOptions,
                request,
                requestKey,
                privateKey,
            } = await createIdentityRequest(context, this.buoyServiceUrl)
 
            const url = this.getChainUrl(context)
            const loginUrl = `${url}/sign?esr=${request.encode()}&chain=${
                context.chain?.id
            }&requestKey=${requestKey}`
 
            const {payload}: {payload: CallbackPayload} = await this.openPopup(
                loginUrl,
                receiveOptions,
                context.ui
            )
 
            this.data.encryptionKey = String(privateKey)
            this.data.messageKey = payload.link_key
 
            Iif (!payload.cid) {
                throw new Error('Login failed: No chain ID returned')
            }
 
            // Prepare the basic login response
            const loginResponse: WalletPluginLoginResponse = {
                chain: Checksum256.from(payload.cid),
                permissionLevel: PermissionLevel.from({
                    actor: payload.sa,
                    permission: payload.sp,
                }),
            }
 
            // Store the identity request and signature for verification
            // The 3rd party app can use this to verify the authentication
            if (payload.sig) {
                // Create identity proof object for third-party verification
                Object.assign(loginResponse, {
                    identityProof: {
                        signature: payload.sig,
                        signedRequest: request.encode(),
                    },
                })
            }
 
            return loginResponse
        } catch (error: unknown) {
            Iif (error instanceof Error) {
                throw new Error(`Login failed: ${error.message}`)
            }
            throw new Error('Login failed: Unknown error')
        }
    }
 
    /**
     * Signs a transaction by opening the web authenticator in a popup
     */
    async sign(
        resolved: ResolvedSigningRequest,
        context: TransactContext
    ): Promise<WalletPluginSignResponse> {
        try {
            // Ensure we have a request key from login
            Iif (!this.data.encryptionKey || !this.data.messageKey) {
                throw new Error('No request keys available - please login first')
            }
 
            const expiration = resolved.transaction.expiration.toDate()
 
            // Create a new signing request based on the existing resolved request
            const modifiedRequest = await context.createRequest({transaction: resolved.transaction})
 
            // Set the expiration on the request LinkInfo
            modifiedRequest.setInfoKey(
                'link',
                LinkInfo.from({
                    expiration,
                })
            )
 
            // Add the callback to the request
            const callback = setTransactionCallback(modifiedRequest, this.buoyServiceUrl)
 
            // Seal the request using the shared secret
            const nonce = UInt64.from(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER))
 
            const sealedRequest = await sealMessage(
                modifiedRequest.encode(),
                PrivateKey.from(this.data.encryptionKey),
                PublicKey.from(this.data.messageKey),
                nonce
            )
 
            const url = this.getChainUrl(context)
            const signUrl = `${url}/sign?sealed=${sealedRequest.toString(
                'hex'
            )}&nonce=${nonce.toString()}&chain=${context.chain?.id}&accountName=${
                context.accountName
            }&permissionName=${context.permissionName}&appName=${
                context.appName
            }&requestKey=${String(PrivateKey.from(this.data.encryptionKey).toPublic())}`
 
            const response = await this.openPopup(signUrl, callback, context.ui)
 
            const signatures = extractSignaturesFromCallback(response.payload)
            const wasSuccessful = isCallback(response.payload) && signatures.length > 0
 
            if (wasSuccessful) {
                // Return the signatures from the wallet
                return {
                    signatures: extractSignaturesFromCallback(response.payload),
                    resolved: resolved, // Return the original resolved request for testing
                }
            } else E{
                throw new Error('Signing failed: No signatures returned')
            }
        } catch (error: unknown) {
            Iif (error instanceof Error) {
                throw new Error(`Signing failed: ${error.message}`)
            }
            throw new Error('Signing failed: Unknown error')
        }
    }
}