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

Mintbase / mintbase-js / 7929488522

16 Feb 2024 10:48AM UTC coverage: 77.204%. First build
7929488522

Pull #476

github

rubenmarcus
fix attributes by contract
Pull Request #476: fix attributes by contract

839 of 1269 branches covered (66.12%)

Branch coverage included in aggregate %.

1149 of 1306 relevant lines covered (87.98%)

12.91 hits per line

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

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

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

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

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

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

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

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

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

45
export type WalletSelectorComponents = {
46
  selector: WalletSelector;
47
  modal: WalletSelectorModal;
48
}
49

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

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

62
const walletUrls = {
3✔
63
  testnet: 'https://testnet.wallet.mintbase.xyz/',
64
  mainnet: 'https://wallet.mintbase.xyz',
65
};
66

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

76

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

102
  walletSelectorComponents.modal = setupModal(walletSelectorComponents.selector, {
×
103
    contractId: contractAddress,
104
  });
105

106
  return walletSelectorComponents;
×
107
};
108

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

122
  const modal = setupModal(selector, {
×
123
    contractId: contractAddress,
124
  });
125

126
  walletSelectorComponents = {
×
127
    selector,
128
    modal,
129
  };
130
  return walletSelectorComponents;
×
131
};
132

133
export class SetupNotCalledError extends Error {
3✔
134
  message: string
135
}
136

137
export class ConnectionTimeoutError extends Error {
3✔
138
  message: string
139
}
140

141
const validateWalletComponentsAreSetup = (): void => {
3✔
142
  if (!walletSelectorComponents.selector) {
×
143
    throw new SetupNotCalledError(ERROR_MESSAGES.WALLET_SETUP_NOT_CALLED_ERROR);
×
144
  }
145
};
146

147
export const registerWalletAccountsSubscriber = (
3✔
148
  callback: (accounts: AccountState[]) => void,
149
): Subscription => {
150
  validateWalletComponentsAreSetup();
×
151

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

160
// scoped to module and cleared since pollForWalletConnection might
161
// get called repeatedly in react enviroments
162
let timerReference = null;
3✔
163

164
export const pollForWalletConnection = async (): Promise<AccountState[]> => {
3✔
165
  validateWalletComponentsAreSetup();
×
166
  // clear any existing timer
167
  clearTimeout(timerReference);
×
168

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

177
    // accounts present in state
178
    if (accounts) {
×
179
      resolve(accounts);
×
180
    }
181

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

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

202
  return new Promise((resolve, reject) =>
×
203
    tryToResolveAccountsFromState(resolve, reject),
×
204
  );
205
};
206

207
export const getWallet = async (): Promise<Wallet> => {
3✔
208
  validateWalletComponentsAreSetup();
×
209

210
  return await walletSelectorComponents.selector.wallet();
×
211
};
212

213
export const connectWalletSelector = (): void => {
3✔
214
  validateWalletComponentsAreSetup();
×
215

216
  walletSelectorComponents.modal.show();
×
217
};
218

219
export const disconnectFromWalletSelector = async (): Promise<void> => {
3✔
220
  validateWalletComponentsAreSetup();
×
221

222
  const wallet = await walletSelectorComponents.selector.wallet();
×
223
  wallet.signOut();
×
224
};
225

226
export const getVerifiedOwner = async (
3✔
227
  params: VerifyOwnerParams,
228
): Promise<VerifiedOwner | undefined> => {
×
229
  validateWalletComponentsAreSetup();
×
230

231
  const { message, callbackUrl, meta } = params;
×
232

233
  const wallet = await walletSelectorComponents.selector.wallet();
×
234

235
  const owner = (await wallet.verifyOwner({
×
236
    message: message,
237
    callbackUrl: callbackUrl,
238
    meta: meta,
239
  })) as VerifiedOwner;
240

241
  return owner;
×
242
};
243

244
// returns a signature of message
245
export const signMessage = async (
3✔
246
  params: VerifyOwnerParams,
247
): Promise<VerifiedOwner> => {
×
248
  const owner = await getVerifiedOwner(params);
×
249

250
  return owner;
×
251
};
252

253

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

259
//   // const owner = await getVerifiedOwner(signature);
260

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

263
//   // const createdPublicKey = utils.PublicKey.from(publicKeyString);
264

265
//   // const stringified = JSON.stringify(owner);
266

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

269
//   return false;
270
// };
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