Skip to content

Commit

Permalink
packages/signatures: add listSessions, getSession with address to Abs…
Browse files Browse the repository at this point in the history
…tractSessionSigner
  • Loading branch information
raykyri committed Jul 16, 2024
1 parent f8c389d commit 45e9cdc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/core/src/Canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ export class Canvas<T extends Contract = Contract> extends TypedEventEmitter<Can
* Get an existing session
*/
public async getSession(query: { address: string; publicKey: string; timestamp?: number }): Promise<string | null> {
// TODO: change address to did (note that network explorer uses this)
const sessions = await this.db.query<{ message_id: string }>("$sessions", {
select: { message_id: true },
orderBy: { message_id: "desc" },
Expand Down
2 changes: 1 addition & 1 deletion packages/interfaces/src/SessionSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface SessionSigner<AuthorizationData = any> {
hasSession: (topic: string, did: DidIdentifier) => boolean
getSession: (
topic: string,
options?: { did?: DidIdentifier },
options?: { did?: DidIdentifier } | { address: string },
) => Awaitable<{ payload: Session<AuthorizationData>; signer: Signer<Action | Session<AuthorizationData>> } | null>
newSession: (
topic: string,
Expand Down
25 changes: 23 additions & 2 deletions packages/signatures/src/AbstractSessionSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,16 @@ export abstract class AbstractSessionSigner<AuthorizationData, WalletAddress ext
*/
public async getSession(
topic: string,
options: { did?: string } = {},
options: { did?: string; address?: string } = {},
): Promise<{ payload: Session<AuthorizationData>; signer: Signer<Action | Session<AuthorizationData>> } | null> {
const did = await Promise.resolve(options.did ?? this.getDid())
let did
if (options.address) {
const dids = this.listSessions(topic).filter((did) => did.endsWith(":" + options.address))
if (dids.length === 0) return null
did = dids[0]
} else {
did = await Promise.resolve(options.did ?? this.getDid())
}
const key = `canvas/${topic}/${did}`

this.log("getting session for topic %s and DID %s", topic, did)
Expand All @@ -107,12 +114,26 @@ export abstract class AbstractSessionSigner<AuthorizationData, WalletAddress ext
return null
}

public listSessions(topic: string): string[] {
// TODO: look at target
const prefix = `canvas/${topic}/`
const result = []

for (const key of this.#cache.keys()) {
if (key.startsWith(prefix)) {
result.push(key)
}
}
return result
}

public hasSession(topic: string, did: DidIdentifier): boolean {
const key = `canvas/${topic}/${did}`
return this.#cache.has(key) || target.get(key) !== null
}

public async clear(topic: string) {
// TODO: delete from target
const prefix = `canvas/${topic}/`

for (const key of this.#cache.keys()) {
Expand Down

0 comments on commit 45e9cdc

Please sign in to comment.