@manahippo/aptos-wallet-adapter 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintignore ADDED
@@ -0,0 +1,6 @@
1
+ node_modules/
2
+ build/
3
+ public/
4
+ craco.config.js
5
+ tailwind.config.js
6
+ postcss.config.js
package/.eslintrc.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "root": true,
3
+ "env": {
4
+ "browser": true,
5
+ "es2021": true
6
+ },
7
+ "settings": {
8
+ "import/resolver": {
9
+ "node": {
10
+ "extensions": [".js", ".ts", ".tsx"]
11
+ }
12
+ },
13
+ "import/extensions": [".js", ".ts", ".tsx"]
14
+ },
15
+ "extends": [
16
+ "plugin:react/recommended",
17
+ "plugin:react-hooks/recommended",
18
+ "plugin:prettier/recommended",
19
+ "airbnb-typescript"
20
+ ],
21
+ "parser": "@typescript-eslint/parser",
22
+ "parserOptions": {
23
+ "ecmaFeatures": {
24
+ "jsx": true
25
+ },
26
+ "ecmaVersion": 12,
27
+ "sourceType": "module",
28
+ "project": "./tsconfig.json"
29
+ },
30
+ "plugins": ["import", "react", "@typescript-eslint", "prettier"],
31
+ "ignorePatterns": ["src/**/*.svg", "**/node_modules/**"],
32
+ "rules": {
33
+ "consistent-return": 0,
34
+ "function-paren-newline": 0,
35
+ "object-curly-newline": 0,
36
+ "react/prop-types": 0,
37
+ "react/jsx-filename-extension": 0,
38
+ "react/react-in-jsx-scope": 0,
39
+ "react/jsx-one-expression-per-line": 0,
40
+ "react/jsx-props-no-spreading": 0,
41
+ "react/jsx-closing-bracket-location": 0,
42
+ "react/no-unescaped-entities": 0,
43
+ "import/prefer-default-export": 0,
44
+ "implicit-arrow-linebreak": 0,
45
+ "jest/no-mocks-import": 0,
46
+ "prettier/prettier": "error",
47
+ "no-unused-vars": ["error", { "vars": "all", "args": "none", "ignoreRestSiblings": true }],
48
+ "@typescript-eslint/comma-dangle": 0,
49
+ "@typescript-eslint/no-use-before-define": 0,
50
+ "@typescript-eslint/indent": 0
51
+ }
52
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "trailingComma": "none",
3
+ "semi": true,
4
+ "tabWidth": 2,
5
+ "singleQuote": true,
6
+ "bracketSpacing": true,
7
+ "bracketSameLine": true,
8
+ "printWidth": 100
9
+ }
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # aptos-wallet-adapter
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@manahippo/aptos-wallet-adapter",
3
+ "version": "0.1.1",
4
+ "scripts": {
5
+ "build": "rm -rf dist; tsc -p tsconfig.json"
6
+ },
7
+ "peerDependencies": {
8
+ "react": "^18.1.0",
9
+ "react-dom": "^18.1.0"
10
+ },
11
+ "devDependencies": {
12
+ "@types/jest": "^27.0.1",
13
+ "@types/node": "^16.7.13",
14
+ "@types/react": "^18.0.0",
15
+ "aptos": "^0.0.20",
16
+ "eslint-config-airbnb-typescript": "^17.0.0",
17
+ "eslint-config-prettier": "^8.5.0",
18
+ "eslint-plugin-prettier": "^4.2.1",
19
+ "eslint-plugin-react": "^7.30.1",
20
+ "eslint-plugin-react-hooks": "^4.6.0",
21
+ "eventemitter3": "^4.0.7",
22
+ "prettier": "^2.7.1",
23
+ "typescript": "^4.7.4"
24
+ }
25
+ }
@@ -0,0 +1,194 @@
1
+ import {
2
+ PendingTransaction,
3
+ SubmitTransactionRequest,
4
+ TransactionPayload
5
+ } from 'aptos/dist/api/data-contracts';
6
+ import {
7
+ WalletDisconnectionError,
8
+ WalletNotConnectedError,
9
+ WalletNotReadyError,
10
+ WalletSignTransactionError
11
+ } from '../WalletProviders/errors';
12
+ import {
13
+ BaseWalletAdapter,
14
+ PublicKey,
15
+ scopePollingDetectionStrategy,
16
+ WalletName,
17
+ WalletReadyState
18
+ } from './BaseAdapter';
19
+
20
+ interface IAptosWallet {
21
+ requestId: number;
22
+ connect: () => Promise<{ address: string }>;
23
+ account: () => Promise<string>;
24
+ isConnected: () => Promise<boolean>;
25
+ signAndSubmitTransaction(transaction: any): Promise<void>;
26
+ signTransaction(transaction: any): Promise<void>;
27
+ disconnect(): Promise<void>;
28
+ }
29
+
30
+ interface AptosWindow extends Window {
31
+ aptos?: IAptosWallet;
32
+ }
33
+
34
+ declare const window: AptosWindow;
35
+
36
+ export const AptosWalletName = 'Aptos Wallet' as WalletName<'Aptos Wallet'>;
37
+
38
+ export interface AptosWalletAdapterConfig {
39
+ provider?: IAptosWallet;
40
+ // network?: WalletAdapterNetwork;
41
+ timeout?: number;
42
+ }
43
+
44
+ export class AptosWalletAdapter extends BaseWalletAdapter {
45
+ name = AptosWalletName;
46
+
47
+ url = 'https://aptos.dev/guides/building-wallet-extension';
48
+
49
+ icon = 'https://miro.medium.com/fit/c/176/176/1*Gf747eyRywU8Img0tK5wvw.png';
50
+
51
+ protected _provider: IAptosWallet | undefined;
52
+
53
+ // protected _network: WalletAdapterNetwork;
54
+ protected _timeout: number;
55
+
56
+ protected _readyState: WalletReadyState =
57
+ typeof window === 'undefined' || typeof document === 'undefined'
58
+ ? WalletReadyState.Unsupported
59
+ : WalletReadyState.NotDetected;
60
+
61
+ protected _connecting: boolean;
62
+
63
+ protected _wallet: any | null;
64
+
65
+ constructor({
66
+ // provider,
67
+ // network = WalletAdapterNetwork.Mainnet,
68
+ timeout = 10000
69
+ }: AptosWalletAdapterConfig = {}) {
70
+ super();
71
+
72
+ this._provider = window.aptos;
73
+ // this._network = network;
74
+ this._timeout = timeout;
75
+ this._connecting = false;
76
+ this._wallet = null;
77
+
78
+ if (this._readyState !== WalletReadyState.Unsupported) {
79
+ scopePollingDetectionStrategy(() => {
80
+ if (window.aptos) {
81
+ this._readyState = WalletReadyState.Installed;
82
+ this.emit('readyStateChange', this._readyState);
83
+ return true;
84
+ }
85
+ return false;
86
+ });
87
+ }
88
+ }
89
+
90
+ get publicKey(): PublicKey | null {
91
+ return this._wallet?.publicKey || null;
92
+ }
93
+
94
+ get connecting(): boolean {
95
+ return this._connecting;
96
+ }
97
+
98
+ get connected(): boolean {
99
+ return !!this._wallet?.isConnected;
100
+ }
101
+
102
+ get readyState(): WalletReadyState {
103
+ return this._readyState;
104
+ }
105
+
106
+ async connect(): Promise<void> {
107
+ try {
108
+ if (this.connected || this.connecting) return;
109
+ if (
110
+ !(
111
+ this._readyState === WalletReadyState.Loadable ||
112
+ this._readyState === WalletReadyState.Installed
113
+ )
114
+ )
115
+ throw new WalletNotReadyError();
116
+ this._connecting = true;
117
+
118
+ const provider = this._provider || window.aptos;
119
+ const response = await provider?.connect();
120
+
121
+ this._wallet = {
122
+ publicKey: response?.address,
123
+ isConnected: true
124
+ };
125
+
126
+ this.emit('connect', this._wallet.publicKey);
127
+ } catch (error: any) {
128
+ this.emit('error', error);
129
+ throw error;
130
+ } finally {
131
+ this._connecting = false;
132
+ }
133
+ }
134
+
135
+ async disconnect(): Promise<void> {
136
+ const wallet = this._wallet;
137
+ if (wallet) {
138
+ this._wallet = null;
139
+
140
+ try {
141
+ const provider = this._provider || window.aptos;
142
+ await provider?.disconnect();
143
+ } catch (error: any) {
144
+ this.emit('error', new WalletDisconnectionError(error?.message, error));
145
+ }
146
+ }
147
+
148
+ this.emit('disconnect');
149
+ }
150
+
151
+ async signTransaction(transaction: TransactionPayload): Promise<SubmitTransactionRequest> {
152
+ try {
153
+ const wallet = this._wallet;
154
+ if (!wallet) throw new WalletNotConnectedError();
155
+
156
+ try {
157
+ const provider = this._provider || window.aptos;
158
+ const response = await provider?.signTransaction(transaction);
159
+ if (response) {
160
+ return response;
161
+ } else {
162
+ throw new Error('Transaction failed');
163
+ }
164
+ } catch (error: any) {
165
+ throw new WalletSignTransactionError(error?.message, error);
166
+ }
167
+ } catch (error: any) {
168
+ this.emit('error', error);
169
+ throw error;
170
+ }
171
+ }
172
+
173
+ async signAndSubmitTransaction(transaction: TransactionPayload): Promise<PendingTransaction> {
174
+ try {
175
+ const wallet = this._wallet;
176
+ if (!wallet) throw new WalletNotConnectedError();
177
+
178
+ try {
179
+ const provider = this._provider || window.aptos;
180
+ const response = await provider?.signAndSubmitTransaction(transaction);
181
+ if (response) {
182
+ return response;
183
+ } else {
184
+ throw new Error('Transaction failed');
185
+ }
186
+ } catch (error: any) {
187
+ throw new WalletSignTransactionError(error.message);
188
+ }
189
+ } catch (error: any) {
190
+ this.emit('error', error);
191
+ throw error;
192
+ }
193
+ }
194
+ }
@@ -0,0 +1,148 @@
1
+ import { MaybeHexString } from 'aptos';
2
+ import {
3
+ PendingTransaction,
4
+ SubmitTransactionRequest,
5
+ TransactionPayload
6
+ } from 'aptos/dist/api/data-contracts';
7
+ import EventEmitter from 'eventemitter3';
8
+
9
+ declare global {
10
+ interface Window {
11
+ hippo: any;
12
+ }
13
+ }
14
+
15
+ export { EventEmitter };
16
+
17
+ export type PublicKey = MaybeHexString;
18
+
19
+ export interface WalletAdapterEvents {
20
+ connect(publicKey: PublicKey): void;
21
+ disconnect(): void;
22
+ error(error: any): void;
23
+ success(value: any): void;
24
+ readyStateChange(readyState: WalletReadyState): void;
25
+ }
26
+
27
+ export enum WalletReadyState {
28
+ /**
29
+ * User-installable wallets can typically be detected by scanning for an API
30
+ * that they've injected into the global context. If such an API is present,
31
+ * we consider the wallet to have been installed.
32
+ */
33
+ Installed = 'Installed',
34
+ NotDetected = 'NotDetected',
35
+ /**
36
+ * Loadable wallets are always available to you. Since you can load them at
37
+ * any time, it's meaningless to say that they have been detected.
38
+ */
39
+ Loadable = 'Loadable',
40
+ /**
41
+ * If a wallet is not supported on a given platform (eg. server-rendering, or
42
+ * mobile) then it will stay in the `Unsupported` state.
43
+ */
44
+ Unsupported = 'Unsupported'
45
+ }
46
+
47
+ export type WalletName<T extends string = string> = T & { __brand__: 'WalletName' };
48
+
49
+ export interface WalletAdapterProps<Name extends string = string> {
50
+ name: WalletName<Name>;
51
+ url: string;
52
+ icon: string;
53
+ readyState: WalletReadyState;
54
+ publicKey: PublicKey | null;
55
+ connecting: boolean;
56
+ connected: boolean;
57
+
58
+ connect(): Promise<void>;
59
+ disconnect(): Promise<void>;
60
+ signAndSubmitTransaction(
61
+ transaction: TransactionPayload
62
+ // connection: Connection,
63
+ // options?: SendTransactionOptions
64
+ ): Promise<PendingTransaction>;
65
+ signTransaction(
66
+ transaction: TransactionPayload
67
+ // connection: Connection,
68
+ // options?: SendTransactionOptions
69
+ ): Promise<SubmitTransactionRequest>;
70
+ }
71
+
72
+ export type WalletAdapter<Name extends string = string> = WalletAdapterProps<Name> &
73
+ EventEmitter<WalletAdapterEvents>;
74
+
75
+ export interface SignerWalletAdapterProps {
76
+ // signTransaction(transaction: Transaction): Promise<Transaction>;
77
+ // signAllTransactions(transaction: Transaction[]): Promise<Transaction[]>;
78
+ }
79
+
80
+ export abstract class BaseWalletAdapter
81
+ extends EventEmitter<WalletAdapterEvents>
82
+ implements WalletAdapter
83
+ {
84
+ abstract name: WalletName;
85
+
86
+ abstract url: string;
87
+
88
+ abstract icon: string;
89
+
90
+ abstract get readyState(): WalletReadyState;
91
+
92
+ abstract get publicKey(): PublicKey | null;
93
+
94
+ abstract get connecting(): boolean;
95
+
96
+ get connected(): boolean {
97
+ return !!this.publicKey;
98
+ }
99
+
100
+ abstract connect(): Promise<void>;
101
+ abstract disconnect(): Promise<void>;
102
+ abstract signAndSubmitTransaction(transaction: TransactionPayload): Promise<PendingTransaction>;
103
+
104
+ abstract signTransaction(transaction: TransactionPayload): Promise<SubmitTransactionRequest>;
105
+ }
106
+
107
+ export function scopePollingDetectionStrategy(detect: () => boolean): void {
108
+ // Early return when server-side rendering
109
+ if (typeof window === 'undefined' || typeof document === 'undefined') return;
110
+
111
+ const disposers: (() => void)[] = [];
112
+
113
+ function detectAndDispose() {
114
+ const detected = detect();
115
+ if (detected) {
116
+ for (const dispose of disposers) {
117
+ dispose();
118
+ }
119
+ }
120
+ }
121
+
122
+ // Strategy #1: Try detecting every second.
123
+ const interval =
124
+ // TODO: #334 Replace with idle callback strategy.
125
+ setInterval(detectAndDispose, 1000);
126
+ disposers.push(() => clearInterval(interval));
127
+
128
+ // Strategy #2: Detect as soon as the DOM becomes 'ready'/'interactive'.
129
+ if (
130
+ // Implies that `DOMContentLoaded` has not yet fired.
131
+ document.readyState === 'loading'
132
+ ) {
133
+ document.addEventListener('DOMContentLoaded', detectAndDispose, { once: true });
134
+ disposers.push(() => document.removeEventListener('DOMContentLoaded', detectAndDispose));
135
+ }
136
+
137
+ // Strategy #3: Detect after the `window` has fully loaded.
138
+ if (
139
+ // If the `complete` state has been reached, we're too late.
140
+ document.readyState !== 'complete'
141
+ ) {
142
+ window.addEventListener('load', detectAndDispose, { once: true });
143
+ disposers.push(() => window.removeEventListener('load', detectAndDispose));
144
+ }
145
+
146
+ // Strategy #4: Detect synchronously, now.
147
+ detectAndDispose();
148
+ }
@@ -0,0 +1,194 @@
1
+ import {
2
+ PendingTransaction,
3
+ SubmitTransactionRequest,
4
+ TransactionPayload
5
+ } from 'aptos/dist/api/data-contracts';
6
+ import {
7
+ WalletDisconnectionError,
8
+ WalletNotConnectedError,
9
+ WalletNotReadyError,
10
+ WalletSignTransactionError
11
+ } from '../WalletProviders/errors';
12
+ import {
13
+ BaseWalletAdapter,
14
+ PublicKey,
15
+ scopePollingDetectionStrategy,
16
+ WalletName,
17
+ WalletReadyState
18
+ } from './BaseAdapter';
19
+
20
+ interface IHippoWallet {
21
+ connect: () => Promise<{ address: string }>;
22
+ account: () => Promise<string>;
23
+ isConnected: () => Promise<boolean>;
24
+ signAndSubmitTransaction(transaction: any): Promise<void>;
25
+ signTransaction(transaction: any): Promise<void>;
26
+ disconnect(): Promise<void>;
27
+ }
28
+
29
+ interface HippoWindow extends Window {
30
+ hippoWallet?: IHippoWallet;
31
+ }
32
+
33
+ declare const window: HippoWindow;
34
+
35
+ export const HippoExtensionWalletName =
36
+ 'Hippo Extension Wallet' as WalletName<'Hippo Extension Wallet'>;
37
+
38
+ export interface HippoExtensionWalletAdapterConfig {
39
+ provider?: IHippoWallet;
40
+ // network?: WalletAdapterNetwork;
41
+ timeout?: number;
42
+ }
43
+
44
+ export class HippoExtensionWalletAdapter extends BaseWalletAdapter {
45
+ name = HippoExtensionWalletName;
46
+
47
+ url = 'https://hippo-wallet-test.web.app';
48
+
49
+ icon = 'https://ui-test1-22e7c.web.app/static/media/hippo_logo.ecded6bf411652de9b7f.png';
50
+
51
+ protected _provider: IHippoWallet | undefined;
52
+
53
+ // protected _network: WalletAdapterNetwork;
54
+ protected _timeout: number;
55
+
56
+ protected _readyState: WalletReadyState =
57
+ typeof window === 'undefined' || typeof document === 'undefined'
58
+ ? WalletReadyState.Unsupported
59
+ : WalletReadyState.NotDetected;
60
+
61
+ protected _connecting: boolean;
62
+
63
+ protected _wallet: any | null;
64
+
65
+ constructor({
66
+ // provider,
67
+ // network = WalletAdapterNetwork.Mainnet,
68
+ timeout = 10000
69
+ }: HippoExtensionWalletAdapterConfig = {}) {
70
+ super();
71
+
72
+ this._provider = window.hippoWallet;
73
+ // this._network = network;
74
+ this._timeout = timeout;
75
+ this._connecting = false;
76
+ this._wallet = null;
77
+
78
+ if (this._readyState !== WalletReadyState.Unsupported) {
79
+ scopePollingDetectionStrategy(() => {
80
+ if (window.hippoWallet) {
81
+ this._readyState = WalletReadyState.Installed;
82
+ this.emit('readyStateChange', this._readyState);
83
+ return true;
84
+ }
85
+ return false;
86
+ });
87
+ }
88
+ }
89
+
90
+ get publicKey(): PublicKey | null {
91
+ return this._wallet?.publicKey || null;
92
+ }
93
+
94
+ get connecting(): boolean {
95
+ return this._connecting;
96
+ }
97
+
98
+ get connected(): boolean {
99
+ return !!this._wallet?.isConnected;
100
+ }
101
+
102
+ get readyState(): WalletReadyState {
103
+ return this._readyState;
104
+ }
105
+
106
+ async connect(): Promise<void> {
107
+ try {
108
+ if (this.connected || this.connecting) return;
109
+ if (
110
+ !(
111
+ this._readyState === WalletReadyState.Loadable ||
112
+ this._readyState === WalletReadyState.Installed
113
+ )
114
+ )
115
+ throw new WalletNotReadyError();
116
+ this._connecting = true;
117
+
118
+ const provider = this._provider || window.hippoWallet;
119
+ const response = await provider?.connect();
120
+
121
+ this._wallet = {
122
+ publicKey: response?.address,
123
+ isConnected: true
124
+ };
125
+
126
+ this.emit('connect', this._wallet.publicKey);
127
+ } catch (error: any) {
128
+ this.emit('error', error);
129
+ throw error;
130
+ } finally {
131
+ this._connecting = false;
132
+ }
133
+ }
134
+
135
+ async disconnect(): Promise<void> {
136
+ const wallet = this._wallet;
137
+ if (wallet) {
138
+ this._wallet = null;
139
+
140
+ try {
141
+ const provider = this._provider || window.hippoWallet;
142
+ await provider?.disconnect();
143
+ } catch (error: any) {
144
+ this.emit('error', new WalletDisconnectionError(error?.message, error));
145
+ }
146
+ }
147
+
148
+ this.emit('disconnect');
149
+ }
150
+
151
+ async signTransaction(transaction: TransactionPayload): Promise<SubmitTransactionRequest> {
152
+ try {
153
+ const wallet = this._wallet;
154
+ if (!wallet) throw new WalletNotConnectedError();
155
+
156
+ try {
157
+ const provider = this._provider || window.hippoWallet;
158
+ const response = await provider?.signTransaction(transaction);
159
+ if (response) {
160
+ return response;
161
+ } else {
162
+ throw new Error('Transaction failed');
163
+ }
164
+ } catch (error: any) {
165
+ throw new WalletSignTransactionError(error?.message, error);
166
+ }
167
+ } catch (error: any) {
168
+ this.emit('error', error);
169
+ throw error;
170
+ }
171
+ }
172
+
173
+ async signAndSubmitTransaction(transaction: TransactionPayload): Promise<PendingTransaction> {
174
+ try {
175
+ const wallet = this._wallet;
176
+ if (!wallet) throw new WalletNotConnectedError();
177
+
178
+ try {
179
+ const provider = this._provider || window.hippoWallet;
180
+ const response = await provider?.signAndSubmitTransaction(transaction);
181
+ if (response) {
182
+ return response;
183
+ } else {
184
+ throw new Error('Transaction failed');
185
+ }
186
+ } catch (error: any) {
187
+ throw new WalletSignTransactionError(error.message);
188
+ }
189
+ } catch (error: any) {
190
+ this.emit('error', error);
191
+ throw error;
192
+ }
193
+ }
194
+ }