• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Mintbase / mintbase-js / 7130552969

07 Dec 2023 03:46PM UTC coverage: 77.772%. First build
7130552969

Pull #442

github

sainthiago
fix array
Pull Request #442: Fix getAccessKeys types

631 of 956 branches covered (0.0%)

Branch coverage included in aggregate %.

975 of 1109 relevant lines covered (87.92%)

4.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

27.27
/packages/react/src/wallet/wallet.ts
1
import {
1✔
2
  setupWalletSelector,
3
  VerifiedOwner,
4
  VerifyOwnerParams,
5
  Wallet,
6
} from '@near-wallet-selector/core';
7
import { setupModal } from '@near-wallet-selector/modal-ui';
1✔
8
import { map, distinctUntilChanged, Subscription } from 'rxjs';
1✔
9

10
import {
1✔
11
  WALLET_CONNECTION_POLL_INTERVAL,
12
  WALLET_CONNECTION_TIMEOUT,
13
} from './constants';
14

15
import { setupNearWallet } from '@near-wallet-selector/near-wallet';
1✔
16
import { setupMeteorWallet } from '@near-wallet-selector/meteor-wallet';
1✔
17
import { setupHereWallet } from '@near-wallet-selector/here-wallet';
1✔
18
import { setupMyNearWallet } from '@near-wallet-selector/my-near-wallet';
1✔
19

20
import type {
21
  WalletSelector,
22
  AccountState,
23
  WalletModuleFactory,
24
} from '@near-wallet-selector/core';
25
import type { WalletSelectorModal } from '@near-wallet-selector/modal-ui';
26
import { setupMintbaseWallet } from '@mintbase-js/wallet';
1✔
27

28
// error messages
29
const SUPPORT = '- further help available on our telegram channel: https://t.me/mintdev';
1✔
30

31
export const ERROR_MESSAGES =  { 
1✔
32
  WALLET_SETUP_NOT_CALLED_ERROR : `Call and await setupWalletSelectorComponents() before registering a subscriber - ${SUPPORT}`,
33
  WALLET_CONNECTION_NOT_FOUND:  `Wallet connection not received after ${WALLET_CONNECTION_TIMEOUT}ms - ${SUPPORT}`,
34
};
35

36
export const SUPPORTED_NEAR_WALLETS: Array<WalletModuleFactory> =[
1✔
37
  setupNearWallet(),
38
  setupMeteorWallet(),
39
  setupMyNearWallet(),
40
  setupHereWallet(),
41
];
42

43
// mintbase SDK wallet functionality wraps
44
// Near Wallet Selector lib, provided by NEAR Protocol
45
// https://github.com/near/wallet-selector/
46

47
export type WalletSelectorComponents = {
48
  selector: WalletSelector;
49
  modal: WalletSelectorModal;
50
}
51

52
// wallet components are held and exposed as a singleton reference
53
// this way they can be more easily passed to other components vs composing calls.
54
export let walletSelectorComponents: WalletSelectorComponents = {
1✔
55
  selector: null,
56
  modal: null,
57
};
58

59
/**
60
 * Set up wallet selector components. Returns the modal
61
 * See also docs on {@link https://github.com/near/wallet-selector/ | near wallet selector}
62
 */
63

64
const walletUrls = {
1✔
65
  testnet: 'https://testnet.wallet.mintbase.xyz/',
66
  mainnet: 'https://wallet.mintbase.xyz',
67
};
68

69
// eslint-disable-next-line max-len
70
export const setupMintbaseWalletSelector = async (
1✔
71
  callbackUrl,
72
  onlyMbWallet = false,
×
73
  network?,
74
  contractAddress?,
75
  options?: { additionalWallets?: Array<WalletModuleFactory> },
76
): Promise<WalletSelectorComponents> => {
×
77

78

79
  if (onlyMbWallet === false) {
×
80

81
    walletSelectorComponents.selector = await setupWalletSelector({
×
82
      network: network,
83
      modules: [
84
        setupMintbaseWallet({
85
          walletUrl: walletUrls[network],
86
          callbackUrl: callbackUrl,
87
        }),
88
        ...(options?.additionalWallets || []),
×
89
        ...SUPPORTED_NEAR_WALLETS,
90
      ],
91
    });
92
  } else {
93
    walletSelectorComponents.selector = await setupWalletSelector({
×
94
      network: network,
95
      modules: [
96
        setupMintbaseWallet({
97
          walletUrl: walletUrls[network],
98
          callbackUrl: callbackUrl,
99
        }),
100
        ...(options?.additionalWallets || []),
×
101
      ],
102
    });
103
  }
104

105
  walletSelectorComponents.modal = setupModal( walletSelectorComponents.selector, {
×
106
    contractId: contractAddress,
107
  });
108

109
  return walletSelectorComponents;
×
110
};
111

112
export const setupWalletSelectorComponents = async (
1✔
113
  network?,
114
  contractAddress?,
115
  options?: { additionalWallets?: Array<WalletModuleFactory> },
116
): Promise<WalletSelectorComponents> => {
×
117
  const selector = await setupWalletSelector({
×
118
    network: network,
119
    modules: [
120
      ...SUPPORTED_NEAR_WALLETS,
121
      ...(options?.additionalWallets || []),
×
122
    ],
123
  });
124

125
  const modal = setupModal(selector, {
×
126
    contractId: contractAddress,
127
  });
128

129
  walletSelectorComponents = {
×
130
    selector,
131
    modal,
132
  };
133
  return walletSelectorComponents;
×
134
};
135

136
export class SetupNotCalledError extends Error {
1✔
137
  message: string
138
}
139

140
export class ConnectionTimeoutError extends Error {
1✔
141
  message: string
142
}
143

144
const validateWalletComponentsAreSetup = (): void => {
1✔
145
  if (!walletSelectorComponents.selector) {
×
146
    throw new SetupNotCalledError(ERROR_MESSAGES.WALLET_SETUP_NOT_CALLED_ERROR);
×
147
  }
148
};
149

150
export const registerWalletAccountsSubscriber = (
1✔
151
  callback: (accounts: AccountState[]) => void,
152
): Subscription => {
153
  validateWalletComponentsAreSetup();
×
154

155
  return walletSelectorComponents.selector.store.observable
×
156
    .pipe(
157
      map((state) => state.accounts),
×
158
      distinctUntilChanged(),
159
    )
160
    .subscribe(callback);
161
};
162

163
// scoped to module and cleared since pollForWalletConnection might
164
// get called repeatedly in react enviroments
165
let timerReference = null;
1✔
166

167
export const pollForWalletConnection = async (): Promise<AccountState[]> => {
1✔
168
  validateWalletComponentsAreSetup();
×
169
  // clear any existing timer
170
  clearTimeout(timerReference);
×
171

172
  const tryToResolveAccountsFromState = (
×
173
    resolve: (value: AccountState[]) => void,
174
    reject: (err: ConnectionTimeoutError) => void,
175
    elapsed = 0,
×
176
  ): void => {
177
    const { accounts } =
178
      walletSelectorComponents.selector.store.getState() || {};
×
179

180
    // accounts present in state
181
    if (accounts) {
×
182
      resolve(accounts);
×
183
    }
184

185
    // timed out
186
    if (elapsed > WALLET_CONNECTION_TIMEOUT) {
×
187
      reject(
×
188
        new ConnectionTimeoutError(ERROR_MESSAGES.WALLET_CONNECTION_NOT_FOUND),
189
      );
190
    }
191

192
    // try again
193
    clearTimeout(timerReference);
×
194
    timerReference = setTimeout(
×
195
      () =>
196
        tryToResolveAccountsFromState(
×
197
          resolve,
198
          reject,
199
          elapsed + WALLET_CONNECTION_POLL_INTERVAL,
200
        ),
201
      WALLET_CONNECTION_POLL_INTERVAL,
202
    );
203
  };
204

205
  return new Promise((resolve, reject) =>
×
206
    tryToResolveAccountsFromState(resolve, reject),
×
207
  );
208
};
209

210
export const getWallet = async (): Promise<Wallet> => {
1✔
211
  validateWalletComponentsAreSetup();
×
212

213
  return await walletSelectorComponents.selector.wallet();
×
214
};
215

216
export const connectWalletSelector = (): void => {
1✔
217
  validateWalletComponentsAreSetup();
×
218

219
  walletSelectorComponents.modal.show();
×
220
};
221

222
export const disconnectFromWalletSelector = async (): Promise<void> => {
1✔
223
  validateWalletComponentsAreSetup();
×
224

225
  const wallet = await walletSelectorComponents.selector.wallet();
×
226
  wallet.signOut();
×
227
};
228

229
export const getVerifiedOwner = async (
1✔
230
  params: VerifyOwnerParams,
231
): Promise<VerifiedOwner | undefined> => {
×
232
  validateWalletComponentsAreSetup();
×
233

234
  const { message, callbackUrl, meta } = params;
×
235

236
  const wallet = await walletSelectorComponents.selector.wallet();
×
237

238
  const owner = (await wallet.verifyOwner({
×
239
    message: message,
240
    callbackUrl: callbackUrl,
241
    meta: meta,
242
  })) as VerifiedOwner;
243

244
  return owner;
×
245
};
246

247
// returns a signature of message
248
export const signMessage = async (
1✔
249
  params: VerifyOwnerParams,
250
): Promise<VerifiedOwner> => {
×
251
  const owner = await getVerifiedOwner(params);
×
252

253
  return owner;
×
254
};
255

256

257
//  https://www.npmjs.com/package/bs58
258
// https://github.com/feross/buffer
259
// https://github.com/near/wallet-selector/issues/434
260
// export const verifyMessage = async (signature: string): Promise<boolean> => {
261

262
//   // const owner = await getVerifiedOwner(signature);
263

264
//   // const publicKeyString = `ed25519:${BinaryToBase58(Buffer.from(owner.publicKey, 'base64'))}`;
265

266
//   // const createdPublicKey = utils.PublicKey.from(publicKeyString);
267

268
//   // const stringified = JSON.stringify(owner);
269

270
//   // const verified = createdPublicKey.verify(new Uint8Array(sha256.array(stringified)), Buffer.from(signature, 'base64'));
271

272
//   return false;
273
// };
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc