From fef546da706d5bd9b21e973f149ff3924624f88d Mon Sep 17 00:00:00 2001 From: tsviatkov Date: Mon, 29 May 2023 18:26:16 +0300 Subject: [PATCH] Add property to compute address for the session key --- example/index.ts | 3 ++- package-lock.json | 4 ++-- package.json | 2 +- src/key-pair.ts | 9 ++++++++- src/openfort.ts | 20 ++++++++++---------- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/example/index.ts b/example/index.ts index d38a667..bac8d8a 100644 --- a/example/index.ts +++ b/example/index.ts @@ -13,7 +13,8 @@ async function example(): Promise { const message = getRandomBytesSync(32); const signature = openfort.signMessage(message); const sessionId = "ses"; - console.log(signature); + console.log(`Signature: ${signature}`); + console.log(`Address: ${openfort.sessionKey.address}`); const response = await openfort.sendSignatureSessionRequest(sessionId, signature); console.dir(response); diff --git a/package-lock.json b/package-lock.json index e15577a..5456b15 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@openfort/openfort-js", - "version": "0.1.3", + "version": "0.1.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@openfort/openfort-js", - "version": "0.1.3", + "version": "0.1.4", "license": "MIT", "dependencies": { "@ethersproject/bytes": "^5.7.0", diff --git a/package.json b/package.json index 23533cc..05b4d84 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@openfort/openfort-js", - "version": "0.1.3", + "version": "0.1.4", "description": "", "author": "", "repository": { diff --git a/src/key-pair.ts b/src/key-pair.ts index 315adbb..8f21a05 100644 --- a/src/key-pair.ts +++ b/src/key-pair.ts @@ -1,10 +1,10 @@ import {secp256k1} from "ethereum-cryptography/secp256k1"; import {LocalStorage} from "./storage/local-storage"; import {FileStorage} from "./storage/file-storage"; -import {BaseStorage} from "./storage/base-storage"; import {StorageKeys} from "./storage/storage-keys"; import {SigningKey} from "@ethersproject/signing-key"; import {arrayify, Bytes, BytesLike, joinSignature} from "@ethersproject/bytes"; +import {computeAddress} from "@ethersproject/transactions" import {hashMessage} from "@ethersproject/hash"; export class KeyPair extends SigningKey { @@ -40,4 +40,11 @@ export class KeyPair extends SigningKey { const privateKey = await KeyPair.storage.get(StorageKeys.SESSION_KEY); return privateKey ? new KeyPair(arrayify(privateKey)) : null; } + + /** + * Return the address for the keypair + */ + public get address(): string { + return computeAddress(this.privateKey); + } } diff --git a/src/openfort.ts b/src/openfort.ts index df280bc..c9a4397 100644 --- a/src/openfort.ts +++ b/src/openfort.ts @@ -8,17 +8,17 @@ export default class Openfort { private readonly _configuration: Configuration; private _sessionsApi?: SessionsApi; private _transactionsApi?: TransactionIntentsApi; - private _keyPair?: KeyPair | null; + private _sessionKey?: KeyPair | null; constructor(accessToken: string, basePath?: string) { this._configuration = new Configuration({accessToken, basePath}); } - public get keyPair(): KeyPair { - if (!this._keyPair) { + public get sessionKey(): KeyPair { + if (!this._sessionKey) { throw new Error("Session key is not initialized"); } - return this._keyPair; + return this._sessionKey; } protected get sessionsApi(): SessionsApi { @@ -36,21 +36,21 @@ export default class Openfort { } public createSessionKey(): KeyPair { - this._keyPair = new KeyPair(); - return this._keyPair; + this._sessionKey = new KeyPair(); + return this._sessionKey; } public async loadSessionKey(): Promise { - this._keyPair = await KeyPair.load(); - return this._keyPair; + this._sessionKey = await KeyPair.load(); + return this._sessionKey; } public async saveSessionKey(): Promise { - return this.keyPair.save(); + return this.sessionKey.save(); } public signMessage(message: Bytes | string): string { - return this.keyPair.sign(message); + return this.sessionKey.sign(message); } @httpErrorHandler()