Skip to content

Commit

Permalink
chore: adding appName to SerializedSession
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Apr 29, 2024
1 parent 749881f commit 27f868c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface RestoreArgs {
actor?: NameType
permission?: NameType
walletPlugin?: Record<string, any>
loginContext?: Record<string, any>
}

export interface SessionKitArgs {
Expand Down Expand Up @@ -451,6 +452,7 @@ export class SessionKit {
chain: this.getChainDefinition(response.chain),
permissionLevel: response.permissionLevel,
walletPlugin,
loginContext: context,
},
this.getSessionOptions(options)
)
Expand Down Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]].
Expand All @@ -55,6 +56,7 @@ export interface SessionArgs {
permission?: NameType
permissionLevel?: PermissionLevelType | string
walletPlugin: WalletPlugin
loginContext?: LoginContext
}

/**
Expand All @@ -81,6 +83,7 @@ export interface SerializedSession {
default?: boolean
permission: NameType
walletPlugin: SerializedWalletPlugin
appName?: string
}

/**
Expand Down Expand Up @@ -602,13 +605,18 @@ export class Session {
return walletResponse.signatures
}

serialize = (): SerializedSession =>
Serializer.objectify({
chain: this.chain.id,
serialize = (): SerializedSession => {
const serializedData: Record<string, any> = {
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) {
Expand Down
7 changes: 7 additions & 0 deletions src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ export interface SerializedWalletPlugin {
data: WalletPluginData
}

/**
* Serialized form of a [[SerializedLoginContext]] instance.
*/

export type SerializedLoginContext = Record<string, any>


/**
* Interface which all 3rd party wallet plugins must implement.
*/
Expand Down
8 changes: 4 additions & 4 deletions test/tests/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -421,7 +421,7 @@ suite('session', function () {
privateKey: 'PVT_K1_25XP1Lt1Rt87hyymouSieBbgnUEAerS1yQHi9wqHC2Uek2mgzH',
},
},
})
}
)
assert.doesNotThrow(() => {
JSON.stringify(serialized)
Expand Down
2 changes: 2 additions & 0 deletions test/tests/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ suite('walletPlugin', function () {
assert.deepEqual(response.session.serialize(), {
chain: '73e4385a2708e6d7048834fbc1079f2fabb17b3c125b146af438971e90716c4d',
actor: 'mock',
appName: 'demo.app',
permission: 'interface',
walletPlugin: {
id: 'MockWalletPluginConfigs',
Expand Down Expand Up @@ -361,6 +362,7 @@ suite('walletPlugin', function () {
assert.deepEqual(response.session.serialize(), {
chain: '73e4385a2708e6d7048834fbc1079f2fabb17b3c125b146af438971e90716c4d',
actor: 'mock',
appName: 'demo.app',
permission: 'interface',
walletPlugin: {
id: 'MockWalletPluginConfigs',
Expand Down

0 comments on commit 27f868c

Please sign in to comment.