From 27f868c445da469525a2edcd5c3f187fc09073a6 Mon Sep 17 00:00:00 2001 From: dafuga Date: Mon, 29 Apr 2024 15:46:18 -0700 Subject: [PATCH] chore: adding appName to SerializedSession --- src/kit.ts | 3 +++ src/session.ts | 18 +++++++++++++----- src/wallet.ts | 7 +++++++ test/tests/session.ts | 8 ++++---- test/tests/wallet.ts | 2 ++ 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/kit.ts b/src/kit.ts index 7c993e6..a5fde04 100644 --- a/src/kit.ts +++ b/src/kit.ts @@ -58,6 +58,7 @@ export interface RestoreArgs { actor?: NameType permission?: NameType walletPlugin?: Record + loginContext?: Record } export interface SessionKitArgs { @@ -451,6 +452,7 @@ export class SessionKit { chain: this.getChainDefinition(response.chain), permissionLevel: response.permissionLevel, walletPlugin, + loginContext: context, }, this.getSessionOptions(options) ) @@ -584,6 +586,7 @@ export class SessionKit { id: args.walletPlugin.id, data: args.walletPlugin.data, }, + appName: this.appName, } } else { // Otherwise throw an error since we can't establish the session data diff --git a/src/session.ts b/src/session.ts index 42127ee..bed0a26 100644 --- a/src/session.ts +++ b/src/session.ts @@ -43,8 +43,9 @@ import { } from './transact' import {SessionStorage} from './storage' import {getFetch} from './utils' -import {SerializedWalletPlugin, WalletPlugin, WalletPluginSignResponse} from './wallet' +import {SerializedLoginContext, SerializedWalletPlugin, WalletPlugin, WalletPluginSignResponse} from './wallet' import {UserInterface} from './ui' +import { LoginContext } from './login' /** * Arguments required to create a new [[Session]]. @@ -55,6 +56,7 @@ export interface SessionArgs { permission?: NameType permissionLevel?: PermissionLevelType | string walletPlugin: WalletPlugin + loginContext?: LoginContext } /** @@ -81,6 +83,7 @@ export interface SerializedSession { default?: boolean permission: NameType walletPlugin: SerializedWalletPlugin + appName?: string } /** @@ -602,13 +605,18 @@ export class Session { return walletResponse.signatures } - serialize = (): SerializedSession => - Serializer.objectify({ - chain: this.chain.id, + serialize = (): SerializedSession => { + const serializedData: Record = { actor: this.permissionLevel.actor, + chain: this.chain.id, permission: this.permissionLevel.permission, walletPlugin: this.walletPlugin.serialize(), - }) + } + if (this.appName) { + serializedData.appName = this.appName + } + return Serializer.objectify(serializedData) + } getPluginTranslations(transactPlugin: TransactPlugin | WalletPlugin): LocaleDefinitions { if (!transactPlugin.translations) { diff --git a/src/wallet.ts b/src/wallet.ts index cd482f6..ebfb8b0 100644 --- a/src/wallet.ts +++ b/src/wallet.ts @@ -97,6 +97,13 @@ export interface SerializedWalletPlugin { data: WalletPluginData } +/** + * Serialized form of a [[SerializedLoginContext]] instance. + */ + +export type SerializedLoginContext = Record + + /** * Interface which all 3rd party wallet plugins must implement. */ diff --git a/test/tests/session.ts b/test/tests/session.ts index e2eae98..9632756 100644 --- a/test/tests/session.ts +++ b/test/tests/session.ts @@ -409,9 +409,9 @@ suite('session', function () { test('returns valid json string', function () { const original = new Session(mockSessionArgs, mockSessionOptions) const serialized = original.serialize() - assert.equal( - JSON.stringify(serialized), - JSON.stringify({ + assert.deepEqual( + serialized, + { chain: '73e4385a2708e6d7048834fbc1079f2fabb17b3c125b146af438971e90716c4d', actor: 'wharfkit1111', permission: 'test', @@ -421,7 +421,7 @@ suite('session', function () { privateKey: 'PVT_K1_25XP1Lt1Rt87hyymouSieBbgnUEAerS1yQHi9wqHC2Uek2mgzH', }, }, - }) + } ) assert.doesNotThrow(() => { JSON.stringify(serialized) diff --git a/test/tests/wallet.ts b/test/tests/wallet.ts index 1299a0d..a3f980d 100644 --- a/test/tests/wallet.ts +++ b/test/tests/wallet.ts @@ -333,6 +333,7 @@ suite('walletPlugin', function () { assert.deepEqual(response.session.serialize(), { chain: '73e4385a2708e6d7048834fbc1079f2fabb17b3c125b146af438971e90716c4d', actor: 'mock', + appName: 'demo.app', permission: 'interface', walletPlugin: { id: 'MockWalletPluginConfigs', @@ -361,6 +362,7 @@ suite('walletPlugin', function () { assert.deepEqual(response.session.serialize(), { chain: '73e4385a2708e6d7048834fbc1079f2fabb17b3c125b146af438971e90716c4d', actor: 'mock', + appName: 'demo.app', permission: 'interface', walletPlugin: { id: 'MockWalletPluginConfigs',