Skip to content

Commit

Permalink
Add property to compute address for the session key
Browse files Browse the repository at this point in the history
  • Loading branch information
tsviatkov committed May 29, 2023
1 parent e5eab2d commit fef546d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
3 changes: 2 additions & 1 deletion example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ async function example(): Promise<void> {
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);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfort/openfort-js",
"version": "0.1.3",
"version": "0.1.4",
"description": "",
"author": "",
"repository": {
Expand Down
9 changes: 8 additions & 1 deletion src/key-pair.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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);
}
}
20 changes: 10 additions & 10 deletions src/openfort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<KeyPair | null> {
this._keyPair = await KeyPair.load();
return this._keyPair;
this._sessionKey = await KeyPair.load();
return this._sessionKey;
}

public async saveSessionKey(): Promise<void> {
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()
Expand Down

0 comments on commit fef546d

Please sign in to comment.