Skip to content

Commit

Permalink
[wallet-ext][wallet-adapter] Update to new wallet standard (MystenLab…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan-Mysten authored Oct 27, 2022
1 parent 73074c9 commit 56de844
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 59 deletions.
6 changes: 6 additions & 0 deletions .changeset/poor-pandas-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@mysten/wallet-adapter-wallet-standard": minor
"@mysten/wallet-standard": minor
---

Update wallet standard adapters to use new wallet registration logic.
5 changes: 1 addition & 4 deletions apps/wallet/src/dapp-interface/WalletStandardInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,7 @@ export class SuiWallet implements Wallet {
// TODO: Expose public key instead of address:
publicKey: new Uint8Array(),
chains: SUI_CHAINS,
features: [
'sui:signAndExecuteTransaction',
'standard:signMessage',
],
features: ['sui:signAndExecuteTransaction'],
});
this.#events.emit('change', { accounts: this.accounts });
}
Expand Down
11 changes: 3 additions & 8 deletions apps/wallet/src/dapp-interface/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { registerWallet } from '@mysten/wallet-standard';

import { DAppInterface } from './DAppInterface';
import { SuiWallet } from './WalletStandardInterface';

import type { WalletsWindow } from '@mysten/wallet-standard';

declare const window: WalletsWindow;

window.navigator.wallets = window.navigator.wallets || [];
window.navigator.wallets.push(({ register }) => {
register(new SuiWallet());
});
registerWallet(new SuiWallet());

Object.defineProperty(window, 'suiWallet', {
enumerable: false,
Expand Down
46 changes: 27 additions & 19 deletions pnpm-lock.yaml

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

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"@mysten/sui.js": "workspace:*",
"@mysten/wallet-adapter-base": "workspace:*",
"@mysten/wallet-standard": "workspace:*",
"@wallet-standard/app": "0.1.0-alpha.6",
"mitt": "^3.0.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { WalletAdapterProvider } from "@mysten/wallet-adapter-base";
import {
isStandardWalletAdapterCompatibleWallet,
StandardWalletAdapterWallet,
DEPRECATED_getWallets,
Wallets,
} from "@mysten/wallet-standard";
import { initialize, InitializedWallets } from "@wallet-standard/app";
import { StandardWalletAdapter } from "./StandardWalletAdapter";
import mitt, { Emitter } from "mitt";

Expand All @@ -15,13 +16,13 @@ type Events = {
};

export class WalletStandardAdapterProvider implements WalletAdapterProvider {
#wallets: InitializedWallets;
#wallets: Wallets;
#adapters: Map<StandardWalletAdapterWallet, StandardWalletAdapter>;
#events: Emitter<Events>;

constructor() {
this.#adapters = new Map();
this.#wallets = initialize();
this.#wallets = DEPRECATED_getWallets();
this.#events = mitt();

this.#wallets.on("register", () => {
Expand Down
25 changes: 11 additions & 14 deletions sdk/wallet-adapter/packages/wallet-standard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ class YourWallet implements Wallet {
// Your wallet's events on implementation.
};

#connect: ConnectMethod = () => {
// Your wallet's connect implementation
};
#connect: ConnectMethod = () => {
// Your wallet's connect implementation
};

#signAndExecuteTransaction: SuiSignAndExecuteTransactionMethod = () => {
// Your wallet's signAndExecuteTransaction implementation
};
#signAndExecuteTransaction: SuiSignAndExecuteTransactionMethod = () => {
// Your wallet's signAndExecuteTransaction implementation
};
}
```

Expand All @@ -104,7 +104,7 @@ class YourWallet implements Wallet {
chains: [SUI_DEVNET_CHAIN],
// The features that this account supports. This can be a subset of the wallet's supported features.
// These features must exist on the wallet as well.
features: ["sui:signAndExecuteTransaction", "standard:signMessage"],
features: ["sui:signAndExecuteTransaction"],
})
);
}
Expand All @@ -113,15 +113,12 @@ class YourWallet implements Wallet {

### Registering in the window

Once you have a compatible interface for your wallet, you can register it in the window under the `window.navigator.wallets` interface. Wallets self-register by pushing their standard wallet interface to this array-like interface.
Once you have a compatible interface for your wallet, you can register it using the `registerWallet` function.

```typescript
// This makes TypeScript aware of the `window.navigator.wallets` interface.
declare const window: import("@mysten/wallet-standard").WalletsWindow;
import { registerWallet } from '@mysten/wallet-standard';

(window.navigator.wallets || []).push(({ register }) => {
register(new YourWallet());
});
registerWallet(new YourWallet());
```

> Note that while this interface is array-like, it is not always an array, and the only method that should be called on it is `push`.
> If you're interested in the internal implementation of the `registerWallet` method, you can [see how it works here](https://github.com/wallet-standard/wallet-standard/blob/b4794e761de688906827829d5380b24cb8ed5fd5/packages/core/wallet/src/register.ts#L9).
4 changes: 1 addition & 3 deletions sdk/wallet-adapter/packages/wallet-standard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
},
"dependencies": {
"@mysten/sui.js": "workspace:*",
"@wallet-standard/features": "0.1.0-alpha.4",
"@wallet-standard/standard": "0.1.0-alpha.5",
"@wallet-standard/util": "0.1.0-alpha.6"
"@wallet-standard/core": "1.0.0-rc.5"
},
"devDependencies": {
"tsup": "^6.2.2",
Expand Down
5 changes: 3 additions & 2 deletions sdk/wallet-adapter/packages/wallet-standard/src/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {
ConnectFeature,
DisconnectFeature,
EventsFeature,
} from "@wallet-standard/features";
import { Wallet, WalletWithFeatures } from "@wallet-standard/standard";
Wallet,
WalletWithFeatures,
} from "@wallet-standard/core";
import { SuiSignAndExecuteTransactionFeature } from "./features";

export type StandardWalletAdapterWallet = WalletWithFeatures<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import type { WalletWithFeatures } from "@wallet-standard/standard";
import type { WalletWithFeatures } from "@wallet-standard/core";
import type { SuiSignAndExecuteTransactionFeature } from "./suiSignAndExecuteTransaction";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
SignableTransaction,
SuiTransactionResponse,
} from "@mysten/sui.js";
import type { SignAndSendTransactionInput } from "@wallet-standard/features";
import type { SignAndSendTransactionInput } from "@wallet-standard/core";

/** The latest API version of the signAndExecuteTransaction API. */
export type SuiSignAndExecuteTransactionVersion = "1.0.0";
Expand Down
4 changes: 1 addition & 3 deletions sdk/wallet-adapter/packages/wallet-standard/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

export * from '@wallet-standard/standard';
export * from '@wallet-standard/features';
export * from '@wallet-standard/util';
export * from '@wallet-standard/core';

export * from "./features";
export * from "./detect";
Expand Down

0 comments on commit 56de844

Please sign in to comment.