Skip to content

Commit

Permalink
support for Uint8Array
Browse files Browse the repository at this point in the history
  • Loading branch information
jamalavedra committed Apr 18, 2024
1 parent cc0ad0f commit b6ef7f2
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 20 deletions.
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.6.9",
"version": "0.6.10",
"description": "",
"author": "Openfort",
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions src/clients/iframe-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export class IframeClient {
throw new MissingRecoveryPasswordError();
}

async sign(message: string, requireArrayify?: boolean, requireHash?: boolean): Promise<string> {
async sign(message: string | Uint8Array, requireArrayify?: boolean, requireHash?: boolean): Promise<string> {
await this.waitForIframeLoad();
const uuid = this.generateShortUUID();
const openfortConfiguration: OpenfortConfiguration = {
Expand All @@ -162,7 +162,7 @@ export class IframeClient {
};
const request = new SignRequest(uuid, message, requireArrayify, requireHash, openfortConfiguration);
this._iframe.contentWindow?.postMessage(request, "*");

let response: SignResponse;
try {
response = await this.waitForResponse<SignResponse>(uuid);
Expand Down
12 changes: 9 additions & 3 deletions src/clients/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,18 @@ export class LogoutRequest implements IEventRequest {
export class SignRequest implements IEventRequest {
uuid: string;
action: Event = Event.SIGN;
message: string;
message: string | Uint8Array;
requireArrayify?: boolean;
requireHash?: boolean;
openfortConfiguration?: OpenfortConfiguration;

constructor(uuid: string, message: string, requireArrayify?:boolean, requireHash?:boolean, openfortConfiguration?: OpenfortConfiguration) {
constructor(
uuid: string,
message: string | Uint8Array,
requireArrayify?: boolean,
requireHash?: boolean,
openfortConfiguration?: OpenfortConfiguration,
) {
this.uuid = uuid;
this.message = message;
this.requireArrayify = requireArrayify;
Expand Down Expand Up @@ -226,4 +232,4 @@ export interface OpenfortConfiguration {
thirdPartyTokenType?: string;
publishableKey: string;
openfortURL?: string;
}
}
4 changes: 2 additions & 2 deletions src/crypto/key-pair.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {secp256k1} from "@noble/curves/secp256k1";
import {SigningKey} from "@ethersproject/signing-key";
import {arrayify, Bytes, BytesLike, joinSignature} from "@ethersproject/bytes";
import {arrayify, BytesLike, joinSignature} from "@ethersproject/bytes";
import {computeAddress} from "@ethersproject/transactions";
import {hashMessage} from "@ethersproject/hash";

Expand All @@ -17,7 +17,7 @@ export class KeyPair extends SigningKey {
* Sign the message with the private key
* @param message Message to sign
*/
public sign(message: Bytes | string): string {
public sign(message: Uint8Array | string): string {
return joinSignature(this.signDigest(hashMessage(arrayify(message))));
}

Expand Down
2 changes: 1 addition & 1 deletion src/openfort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export default class Openfort {
return result.data;
}

public async signMessage(message: string): Promise<string> {
public async signMessage(message: string | Uint8Array): Promise<string> {
await this.recoverSigner();
if (!this._signer) {
throw new NoSignerConfigured("No signer configured");
Expand Down
5 changes: 2 additions & 3 deletions src/signer/embedded.signer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {IframeClient, IFrameConfiguration} from "../clients/iframe-client";
import {Bytes} from "@ethersproject/bytes";
import {ISigner, SignerType} from "./signer";
import {InstanceManager} from "../instanceManager";
import {ConfigureRequest} from "../clients/types";
Expand Down Expand Up @@ -40,13 +39,13 @@ export class EmbeddedSigner implements ISigner {
return deviceID;
}

public async sign(message: Bytes | string, requireArrayify?: boolean, requireHash?: boolean): Promise<string> {
public async sign(message: Uint8Array | string, requireArrayify?: boolean, requireHash?: boolean): Promise<string> {
const loaded = await this.isLoaded();
if (!loaded) {
throw new Error("Signer is not loaded");
}

return await this._iframeClient.sign(message as string, requireArrayify, requireHash);
return await this._iframeClient.sign(message, requireArrayify, requireHash);
}

public getDeviceID(): string | null {
Expand Down
3 changes: 1 addition & 2 deletions src/signer/session.signer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {KeyPair} from "../crypto/key-pair";
import {Bytes} from "@ethersproject/bytes";
import {ISigner, SignerType} from "./signer";
import {InstanceManager} from "../instanceManager";

Expand All @@ -11,7 +10,7 @@ export class SessionSigner implements ISigner {
this._instanceManager = instanceManager;
}

public sign(message: Bytes | string, requireArrayify?: boolean, requireHash?: boolean): Promise<string> {
public sign(message: Uint8Array | string, requireArrayify?: boolean, requireHash?: boolean): Promise<string> {
return new Promise((resolve) => {
resolve(this._sessionKey.sign(message));
});
Expand Down
4 changes: 1 addition & 3 deletions src/signer/signer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {Bytes} from "@ethersproject/bytes";

export enum SignerType {
EMBEDDED = "embedded",
SESSION = "session",
}

export interface ISigner {
sign(message: Bytes | string, requireArrayify?: boolean, requireHash?: boolean): Promise<string>;
sign(message: Uint8Array | string, requireArrayify?: boolean, requireHash?: boolean): Promise<string>;
logout(): Promise<void>;
useCredentials(): boolean;
updateAuthentication(): Promise<void>;
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const VERSION = "0.6.9";
export const VERSION = "0.6.10";
export const PACKAGE = "@openfort/openfort-js";

0 comments on commit b6ef7f2

Please sign in to comment.