Skip to content

Commit

Permalink
Fixed dependencies
Browse files Browse the repository at this point in the history
Updated the code to support improved sameDevice authenthication flow
  • Loading branch information
andreyjamer committed Jan 23, 2024
1 parent 352829b commit 6c3d199
Show file tree
Hide file tree
Showing 7 changed files with 608 additions and 430 deletions.
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
"postcss": "^8.4.31",
"tailwindcss": "^3.3.5",
"typescript": "^5.0.2",
"vite": "^4.4.5"
"vite": "^4.5.2"
}
}
2 changes: 1 addition & 1 deletion examples/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const sessionKit = new SessionKit({
chains: [
{
id: "71ee83bcf52142d61019d95f9cc5427ba6a0d7ff8accd9e2088ae2abeaf3d3dd",
url: "https://proton-testnet.eoscafeblock.com",
url: "https://testnet-rpc.api.protondex.com",
},
],
ui: webRenderer,
Expand Down
634 changes: 389 additions & 245 deletions examples/yarn.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
},
"dependencies": {
"@greymass/buoy": "^1.0.3",
"@wharfkit/protocol-esr": "^1.0.0-rc1",
"@wharfkit/protocol-esr": "^1.2.1",
"isomorphic-ws": "^5.0.0",
"tslib": "^2.1.0",
"ws": "^8.14.2"
},
"peerDependencies": {
"@wharfkit/session": "^1.0.0"
"@wharfkit/session": "^1.2.5"
},
"devDependencies": {
"@rollup/plugin-alias": "^3.1.4",
Expand All @@ -38,8 +37,8 @@
"@types/node": "^18.7.18",
"@typescript-eslint/eslint-plugin": "^5.20.0",
"@typescript-eslint/parser": "^5.20.0",
"@wharfkit/mock-data": "^1.0.2",
"@wharfkit/session": "^1.0.0",
"@wharfkit/mock-data": "^1.2.0",
"@wharfkit/session": "^1.2.5",
"chai": "^4.3.4",
"eslint": "^8.13.0",
"eslint-config-prettier": "^8.1.0",
Expand All @@ -54,6 +53,7 @@
"rollup-plugin-terser": "^7.0.2",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.1.1",
"tslib": "^2.1.0",
"typedoc": "^0.23.14",
"typescript": "^4.1.2",
"yarn-deduplicate": "^6.0.1"
Expand Down
59 changes: 43 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {send} from '@greymass/buoy'
import { send } from '@greymass/buoy'
import {
AbstractWalletPlugin,
CallbackPayload,
Expand All @@ -15,7 +15,6 @@ import {
ResolvedSigningRequest,
Serializer,
TransactContext,
Transaction,
WalletPluginConfig,
WalletPluginLoginResponse,
WalletPluginMetadata,
Expand All @@ -29,12 +28,14 @@ import {
setTransactionCallback,
verifyLoginCallbackResponse,
waitForCallback,
generateReturnUrl,
isAppleHandheld,
} from '@wharfkit/protocol-esr'

import WebSocket from 'isomorphic-ws'
import {createIdentityRequest, Deferred, getChainId} from './utils'
import {BrowserTransport} from './browser'
import {inBrowserPayload, isInBrowserPayload} from './types'
import { createIdentityRequest, Deferred, getChainId } from './utils'
import { BrowserTransport } from './browser'
import { inBrowserPayload, isInBrowserPayload } from './types'

import defaultTranslations from './translations'

Expand Down Expand Up @@ -131,7 +132,7 @@ export class WalletPluginWebAuth extends AbstractWalletPlugin {
const browserLogin = new Deferred<inBrowserPayload>()

// Create the identity request to be presented to the user
const {callback, request, requestKey, privateKey} = await createIdentityRequest(
const {callback, request, sameDeviceRequest, requestKey, privateKey} = await createIdentityRequest(
context,
this.buoyUrl
)
Expand All @@ -152,7 +153,7 @@ export class WalletPluginWebAuth extends AbstractWalletPlugin {
type: 'link',
label: t('login.link', {default: 'Launch WebAuth'}),
data: {
href: request.encode(true, false, `${this.scheme}:`),
href: sameDeviceRequest.encode(true, false, `${this.scheme}:`),
label: t('login.link', {default: 'Launch WebAuth'}),
variant: 'primary',
},
Expand Down Expand Up @@ -225,6 +226,17 @@ export class WalletPluginWebAuth extends AbstractWalletPlugin {
this.data.channelUrl = callbackResponse.link_ch
this.data.channelName = callbackResponse.link_name

try {
if (callbackResponse.link_meta) {
const metadata = JSON.parse(callbackResponse.link_meta)
this.data.sameDevice = metadata.sameDevice
this.data.launchUrl = metadata.launchUrl
this.data.triggerUrl = metadata.triggerUrl
}
} catch (e) {
// console.log('Error processing link_meta', e)
}

return {
chain: Checksum256.from(callbackResponse.cid),
permissionLevel: PermissionLevel.from({
Expand Down Expand Up @@ -354,6 +366,25 @@ export class WalletPluginWebAuth extends AbstractWalletPlugin {

const request = modifiedRequest.encode(true, false)

// Mobile will return true or false, desktop will return undefined
const isSameDevice = this.data.sameDevice !== false

// Same device request
const sameDeviceRequest = modifiedRequest.clone()
const returnUrl = generateReturnUrl()
sameDeviceRequest.setInfoKey('same_device', true)
sameDeviceRequest.setInfoKey('return_path', returnUrl)

if (this.data.sameDevice) {
if (this.data.sameDevice) {
if (this.data.launchUrl) {
window.location.href = this.data.launchUrl
} else if (isAppleHandheld()) {
window.location.href = `${this.scheme}://link`
}
}
}

const signManually = () => {
context.ui?.prompt({
title: t('transact.sign_manually.title', {default: 'Sign manually'}),
Expand All @@ -372,7 +403,7 @@ export class WalletPluginWebAuth extends AbstractWalletPlugin {
default: 'Open WebAuth',
}),
data: {
href: String(request),
href: String(sameDeviceRequest),
label: t('transact.sign_manually.link.title', {
default: 'Open WebAuth',
}),
Expand Down Expand Up @@ -405,8 +436,9 @@ export class WalletPluginWebAuth extends AbstractWalletPlugin {
default: 'Sign manually or with another device',
}),
data: {
href: modifiedRequest.encode(true, false, `${this.scheme}:`),
onClick: signManually,
onClick: isSameDevice
? () => (window.location.href = sameDeviceRequest.encode())
: signManually,
label: t('transact.label', {
default: 'Sign manually or with another device',
}),
Expand Down Expand Up @@ -443,7 +475,7 @@ export class WalletPluginWebAuth extends AbstractWalletPlugin {
const service = new URL(this.data.channelUrl).origin
const channel = new URL(this.data.channelUrl).pathname.substring(1)
const sealedMessage = sealMessage(
modifiedRequest.encode(true, false, `${this.scheme}:`),
(this.data.sameDevice ? sameDeviceRequest : modifiedRequest).encode(true, false, `${this.scheme}:`),
PrivateKey.from(this.data.privateKey),
PublicKey.from(this.data.signerKey)
)
Expand Down Expand Up @@ -471,11 +503,6 @@ export class WalletPluginWebAuth extends AbstractWalletPlugin {
callbackResponse,
context.esrOptions
)

console.log('CBR', callbackResponse)
console.log('CBR Signed', extractSignaturesFromCallback(callbackResponse))
console.log('CBR resolved', resolvedRequest)

// Return the new request and the signatures from the wallet
return {
signatures: extractSignaturesFromCallback(callbackResponse),
Expand Down
8 changes: 7 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ReceiveOptions } from '@greymass/buoy';
import { BuoySession, IdentityRequestResponse, getUserAgent, prepareCallback, uuid } from '@wharfkit/protocol-esr';
import { BuoySession, IdentityRequestResponse, getUserAgent, prepareCallback, uuid, generateReturnUrl } from '@wharfkit/protocol-esr';
import { Checksum256, LoginContext, PrivateKey, SigningRequest } from '@wharfkit/session';

export function checkMultiChain(context: LoginContext,) {
Expand Down Expand Up @@ -61,12 +61,18 @@ export async function createIdentityRequest(

request.setInfoKey('req_account', String(context.appName))

const sameDeviceRequest = request.clone()
const returnUrl = generateReturnUrl()
sameDeviceRequest.setInfoKey('same_device', true)
sameDeviceRequest.setInfoKey('return_path', returnUrl)

// Return the request and the callback data
return {
callback: callbackChannel,
request,
requestKey,
privateKey,
sameDeviceRequest
}
}

Expand Down
Loading

0 comments on commit 6c3d199

Please sign in to comment.