diff --git a/.changeset/itchy-news-serve.md b/.changeset/itchy-news-serve.md new file mode 100644 index 00000000..5efff39f --- /dev/null +++ b/.changeset/itchy-news-serve.md @@ -0,0 +1,5 @@ +--- +"@fuel-connectors/bako-safe": patch +--- + +feat: bako sign message diff --git a/.github/workflows/pr-release.yaml b/.github/workflows/pr-release.yaml index f0fd52fc..e317d42f 100644 --- a/.github/workflows/pr-release.yaml +++ b/.github/workflows/pr-release.yaml @@ -7,7 +7,7 @@ jobs: release-pr: name: 'Release PR to npm' runs-on: buildjet-4vcpu-ubuntu-2204 - # Comment if:false to enable release PR to npm + # comment out if:false to enable release PR to npm # if: false permissions: write-all steps: @@ -47,4 +47,4 @@ jobs: message: | This PR is published in NPM with version **${{ steps.release.outputs.published_version }}** env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/packages/bako-safe/src/BakoSafeConnector.ts b/packages/bako-safe/src/BakoSafeConnector.ts index 28a189e0..fb633fde 100644 --- a/packages/bako-safe/src/BakoSafeConnector.ts +++ b/packages/bako-safe/src/BakoSafeConnector.ts @@ -23,6 +23,7 @@ import { APP_VERSION, HAS_WINDOW, HOST_URL, + IS_SAFARI, SESSION_ID, WINDOW, } from './constants'; @@ -47,7 +48,7 @@ export class BakoSafeConnector extends FuelConnector { description: APP_DESCRIPTION, }, }; - installed = true; + installed = !IS_SAFARI; connected = false; external = false; @@ -165,7 +166,7 @@ export class BakoSafeConnector extends FuelConnector { // @ts-ignore this.on(BakoSafeConnectorEvents.CLIENT_DISCONNECTED, () => { this.dAppWindow?.close(); - reject(false); + reject(new Error('User rejected the request')); }); this.on( @@ -209,7 +210,7 @@ export class BakoSafeConnector extends FuelConnector { BakoSafeConnectorEvents.CLIENT_DISCONNECTED, () => { this.dAppWindow?.close(); - reject(); + reject(new Error('User rejected the request')); }, ); @@ -231,7 +232,6 @@ export class BakoSafeConnector extends FuelConnector { // @ts-ignore BakoSafeConnectorEvents.TX_CONFIRMED, ({ data }: { data: IResponseTxCofirmed }) => { - this.dAppWindow?.close(); resolve(`0x${data.id}`); }, ); @@ -239,6 +239,9 @@ export class BakoSafeConnector extends FuelConnector { } async ping() { + if (IS_SAFARI) { + return false; + } await this.setup(); return this.setupReady ?? false; } @@ -307,7 +310,32 @@ export class BakoSafeConnector extends FuelConnector { } async signMessage(_address: string, _message: string): Promise { - throw new Error('Method not implemented.'); + return new Promise((resolve, reject) => { + // window control + this.dAppWindow?.open(`/dapp/sign/${_message}`, reject); + this.checkWindow(); + + // events control + this.on( + //@ts-ignore + BakoSafeConnectorEvents.CLIENT_DISCONNECTED, + () => { + this.dAppWindow?.close(); + reject(new Error('User rejected the request')); + }, + ); + + this.on( + //@ts-ignore + BakoSafeConnectorEvents.SIGN_CONFIRMED, + (data: { data: { signedMessage: string }; from: string }) => { + const signedMessage = data?.data?.signedMessage || ''; + + this.dAppWindow?.close(); + resolve(signedMessage); + }, + ); + }); } async addAssets(_assets: Asset[]): Promise { diff --git a/packages/bako-safe/src/constants.ts b/packages/bako-safe/src/constants.ts index 4f0348ab..de18da30 100644 --- a/packages/bako-safe/src/constants.ts +++ b/packages/bako-safe/src/constants.ts @@ -19,3 +19,6 @@ export const WINDOW: any = HAS_WINDOW ? window : {}; //storage export const SESSION_ID = 'sessionId'; +export const IS_SAFARI = /^((?!chrome|android).)*safari/i.test( + WINDOW.navigator?.userAgent, +); diff --git a/packages/bako-safe/src/types.ts b/packages/bako-safe/src/types.ts index 25c2a05e..b8232628 100644 --- a/packages/bako-safe/src/types.ts +++ b/packages/bako-safe/src/types.ts @@ -17,6 +17,9 @@ export enum BakoSafeConnectorEvents { //auth AUTH_CONFIRMED = '[AUTH_CONFIRMED]', + + //sign + SIGN_CONFIRMED = '[SIGN_CONFIRMED]', } export enum BakoSafeUsernames { diff --git a/packages/bako-safe/tests/BakoSafeConnector.test.ts b/packages/bako-safe/tests/BakoSafeConnector.test.ts index 03ac0078..14da36b1 100644 --- a/packages/bako-safe/tests/BakoSafeConnector.test.ts +++ b/packages/bako-safe/tests/BakoSafeConnector.test.ts @@ -118,7 +118,7 @@ describe('signMessage()', () => { expect( connector.signMessage(Address.fromRandom().toString(), 'message'), - ).rejects.toThrowError('Method not implemented.'); + ).rejects.toThrowError('User rejected the request'); }); }); diff --git a/packages/docs/src/guide/react-hooks/useIsSupportedNetwork.md b/packages/docs/src/guide/react-hooks/useIsSupportedNetwork.md index 82e2b17b..85c9312d 100644 --- a/packages/docs/src/guide/react-hooks/useIsSupportedNetwork.md +++ b/packages/docs/src/guide/react-hooks/useIsSupportedNetwork.md @@ -17,6 +17,6 @@ console.log(isSupportedNetwork); ``` #### Defined in -[packages/react/src/hooks/useIsSupportedNetwork.tsx:29](https://github.com/fuellabs/fuel-connectors/blob/main/packages/react/src/hooks/useIsSupportedNetwork.tsx#L29) +[packages/react/src/hooks/useIsSupportedNetwork.tsx:30](https://github.com/fuellabs/fuel-connectors/blob/main/packages/react/src/hooks/useIsSupportedNetwork.tsx#L30) ___