-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathcoin98-wallet.ts
214 lines (177 loc) · 4.92 KB
/
coin98-wallet.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { isMobile } from "is-mobile";
import type {
WalletModuleFactory,
WalletBehaviourFactory,
InjectedWallet,
Account,
Optional,
Transaction,
SignMessageParams,
SignedMessage,
} from "@near-wallet-selector/core";
import { getActiveAccount } from "@near-wallet-selector/core";
import type { InjectedCoin98 } from "./injected-coin98-wallet";
import { signTransactions } from "@near-wallet-selector/wallet-utils";
import type { FinalExecutionOutcome } from "near-api-js/lib/providers";
import icon from "./icon";
declare global {
interface Window {
coin98: InjectedCoin98;
}
}
export interface Coin98WalletParams {
iconUrl?: string;
deprecated?: boolean;
}
interface Coin98WalletState {
wallet: InjectedCoin98;
}
const isInstalled = () => {
return !!window.coin98;
};
const setupCoin98WalletState = (): Coin98WalletState => {
const wallet = window.coin98!;
return {
wallet,
};
};
const Coin98Wallet: WalletBehaviourFactory<InjectedWallet> = async ({
metadata,
options,
store,
provider,
logger,
}) => {
const _state = setupCoin98WalletState();
const getAccounts = async (): Promise<Array<Account>> => {
const accountId = _state.wallet.near.account;
if (!accountId) {
return [];
}
const publicKey = await _state.wallet.near.signer.getPublicKey(
accountId,
options.network.networkId
);
return [
{
accountId,
publicKey: publicKey ? publicKey.toString() : undefined,
},
];
};
const transformTransactions = (
transactions: Array<Optional<Transaction, "signerId" | "receiverId">>
): Array<Transaction> => {
const { contract } = store.getState();
if (!contract) {
throw new Error("Wallet not signed in");
}
const account = getActiveAccount(store.getState());
if (!account) {
throw new Error("No active account");
}
return transactions.map((transaction) => {
return {
signerId: transaction.signerId || account.accountId,
receiverId: transaction.receiverId || contract.contractId,
actions: transaction.actions,
};
});
};
return {
async signIn({ contractId }) {
const existingAccounts = await getAccounts();
if (existingAccounts.length) {
return existingAccounts;
}
await _state.wallet.near.connect({ prefix: "near_selector", contractId });
return getAccounts();
},
async signMessage({
message,
nonce,
recipient,
state,
}: SignMessageParams): Promise<SignedMessage> {
if (!_state.wallet) {
throw new Error("Wallet is not installed");
}
logger.log("Coin98:signMessage", {
message,
nonce,
recipient,
state,
});
const signature = await _state.wallet.near.signMessage({
message,
nonce,
recipient,
state,
});
return signature;
},
async signOut() {
// Ignore if unsuccessful (returns false).
await _state.wallet.near.disconnect();
},
async getAccounts() {
return getAccounts();
},
async verifyOwner() {
throw new Error(`Method not supported by ${metadata.name}`);
},
async signAndSendTransaction({ signerId, receiverId, actions }) {
logger.log("signAndSendTransaction", { signerId, receiverId, actions });
const signedTransactions = await signTransactions(
transformTransactions([{ signerId, receiverId, actions }]),
_state.wallet.near.signer,
options.network
);
return provider.sendTransaction(signedTransactions[0]);
},
async signAndSendTransactions({ transactions }) {
logger.log("signAndSendTransactions", { transactions });
const signedTransactions = await signTransactions(
transformTransactions(transactions),
_state.wallet.near.signer,
options.network
);
logger.log(
"signAndSendTransactions:signedTransactions",
signedTransactions
);
const results: Array<FinalExecutionOutcome> = [];
for (let i = 0; i < signedTransactions.length; i++) {
results.push(await provider.sendTransaction(signedTransactions[i]));
}
return results;
},
};
};
export const setupCoin98Wallet = ({
iconUrl = icon,
deprecated = false,
}: Coin98WalletParams = {}): WalletModuleFactory<InjectedWallet> => {
return async () => {
const mobile = isMobile();
if (mobile) {
return null;
}
const installed = isInstalled();
return {
id: "coin98-wallet",
type: "injected",
metadata: {
name: "Coin98 Wallet",
description:
"Using a Decentralized Wallet With Experiences of a Centralized One",
iconUrl,
downloadUrl:
"https://chrome.google.com/webstore/detail/coin98-wallet/aeachknmefphepccionboohckonoeemg",
deprecated,
available: installed,
},
init: Coin98Wallet,
};
};
};