Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Default Wallets Module #379

Merged
merged 20 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ yarn add \
@near-wallet-selector/meteor-wallet \
@near-wallet-selector/ledger \
@near-wallet-selector/wallet-connect \
@near-wallet-selector/nightly-connect
@near-wallet-selector/nightly-connect \
@near-wallet-selector/default-wallets

# Using NPM.
npm install \
Expand All @@ -63,7 +64,8 @@ npm install \
@near-wallet-selector/meteor-wallet \
@near-wallet-selector/ledger \
@near-wallet-selector/wallet-connect \
@near-wallet-selector/nightly-connect
@near-wallet-selector/nightly-connect \
@near-wallet-selector/default-wallets
```

Optionally, you can install our [`modal-ui`](https://www.npmjs.com/package/@near-wallet-selector/modal-ui) package for a pre-built interface that wraps the `core` API and presents the supported wallets:
Expand All @@ -90,10 +92,12 @@ import { setupMeteorWallet } from "@near-wallet-selector/meteor-wallet";
import { setupLedger } from "@near-wallet-selector/ledger";
import { setupWalletConnect } from "@near-wallet-selector/wallet-connect";
import { setupNightlyConnect } from "@near-wallet-selector/nightly-connect";
import { setupDefaultWallets } from "@near-wallet-selector/default-wallets";

const selector = await setupWalletSelector({
network: "testnet",
modules: [
...(await setupDefaultWallets()),
setupNearWallet(),
setupMyNearWallet(),
setupSender(),
Expand Down
16 changes: 2 additions & 14 deletions examples/angular/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { Component, OnInit } from "@angular/core";
import { setupWalletSelector } from "@near-wallet-selector/core";
import type { WalletSelector, AccountState } from "@near-wallet-selector/core";
import { setupDefaultWallets } from "@near-wallet-selector/default-wallets";
import { setupNearWallet } from "@near-wallet-selector/near-wallet";
import { setupMyNearWallet } from "@near-wallet-selector/my-near-wallet";
import { setupSender } from "@near-wallet-selector/sender";
import { setupMathWallet } from "@near-wallet-selector/math-wallet";
import { setupNightly } from "@near-wallet-selector/nightly";
import { setupMeteorWallet } from "@near-wallet-selector/meteor-wallet";
import { setupLedger } from "@near-wallet-selector/ledger";
import { setupWalletConnect } from "@near-wallet-selector/wallet-connect";
import { setupNightlyConnect } from "@near-wallet-selector/nightly-connect";
import { setupModal } from "@near-wallet-selector/modal-ui";
import type { WalletSelectorModal } from "@near-wallet-selector/modal-ui";
Expand Down Expand Up @@ -44,22 +42,12 @@ export class AppComponent implements OnInit {
network: "testnet",
debug: true,
modules: [
...(await setupDefaultWallets()),
setupNearWallet(),
setupMyNearWallet(),
setupSender(),
setupMathWallet(),
setupNightly(),
setupMeteorWallet(),
setupLedger(),
setupWalletConnect({
projectId: "c4f79cc...",
metadata: {
name: "NEAR Wallet Selector",
description: "Example dApp used by NEAR Wallet Selector",
url: "https://github.com/near/wallet-selector",
icons: ["https://avatars.githubusercontent.com/u/37784886"],
},
}),
setupNightlyConnect({
url: "wss://ncproxy.nightly.app/app",
appMetadata: {
Expand Down
16 changes: 2 additions & 14 deletions examples/react/contexts/WalletSelectorContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import { setupWalletSelector } from "@near-wallet-selector/core";
import type { WalletSelector, AccountState } from "@near-wallet-selector/core";
import { setupModal } from "@near-wallet-selector/modal-ui";
import type { WalletSelectorModal } from "@near-wallet-selector/modal-ui";
import { setupDefaultWallets } from "@near-wallet-selector/default-wallets";
import { setupNearWallet } from "@near-wallet-selector/near-wallet";
import { setupMyNearWallet } from "@near-wallet-selector/my-near-wallet";
import { setupSender } from "@near-wallet-selector/sender";
import { setupMathWallet } from "@near-wallet-selector/math-wallet";
import { setupNightly } from "@near-wallet-selector/nightly";
import { setupMeteorWallet } from "@near-wallet-selector/meteor-wallet";
import { setupLedger } from "@near-wallet-selector/ledger";
import { setupWalletConnect } from "@near-wallet-selector/wallet-connect";
import { setupNightlyConnect } from "@near-wallet-selector/nightly-connect";
import { CONTRACT_ID } from "../constants";

Expand Down Expand Up @@ -42,22 +40,12 @@ export const WalletSelectorContextProvider: React.FC = ({ children }) => {
network: "testnet",
debug: true,
modules: [
...(await setupDefaultWallets()),
setupNearWallet(),
setupMyNearWallet(),
setupSender(),
setupMathWallet(),
setupNightly(),
setupMeteorWallet(),
setupLedger(),
setupWalletConnect({
projectId: "c4f79cc...",
metadata: {
name: "NEAR Wallet Selector",
description: "Example dApp used by NEAR Wallet Selector",
url: "https://github.com/near/wallet-selector",
icons: ["https://avatars.githubusercontent.com/u/37784886"],
},
}),
setupNightlyConnect({
url: "wss://ncproxy.nightly.app/app",
appMetadata: {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"build:wallet-connect": "nx run-many --target=build --projects=wallet-connect --configuration=production",
"build:nightly-connect": "nx run-many --target=build --projects=nightly-connect --configuration=production",
"build:wallet-utils": "nx run-many --target=build --projects=wallet-utils --configuration=production",
"build:default-wallets": "nx run-many --target=build --projects=default-wallets --configuration=production",
"lint": "nx workspace-lint && nx run-many --target=lint --all --parallel",
"lint:fix": "nx run-many --target=lint --all --fix",
"serve:react": "nx serve react",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,9 @@ export class WalletModules {
continue;
}

// Skip duplicated module.
if (modules.some((x) => x.id === module.id)) {
throw new Error("Duplicate module id detected: " + module.id);
continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious as to why this was removed - should we not still be alerting someone if they configure the selector with multiple instances of the same module -- unique id values are important because they are used as keys on an object

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we add 2 wallets with the same id the last one this way will be skipped.
If we don't add continue here the initialization of the wallet selector in setupWalletSelector will fail with the above error.
This was done to prevent failing of setupWalletSelector when we want to override one of the default wallets.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Developers may want to override one of the default wallets in the list to add custom options in the setup function of that specific wallet.

}

modules.push({
Expand Down
10 changes: 10 additions & 0 deletions packages/default-wallets/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": [
[
"@nrwl/web/babel",
{
"useBuiltIns": "usage"
}
]
]
}
18 changes: 18 additions & 0 deletions packages/default-wallets/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
78 changes: 78 additions & 0 deletions packages/default-wallets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# @near-wallet-selector/default-wallets

This is the Default Wallets package for NEAR Wallet Selector.

This is the list of default wallets:

- [My NEAR Wallet](https://www.npmjs.com/package/@near-wallet-selector/my-near-wallet) - Browser wallet.
- [Ledger](https://www.npmjs.com/package/@near-wallet-selector/ledger) - Hardware wallet.
- [WalletConnect](https://www.npmjs.com/package/@near-wallet-selector/wallet-connect) - Bridge wallet.


## Installation and Usage

The easiest way to use this package is to install it from the NPM registry, this package requires `near-api-js` v0.44.2 or above:

```bash
# Using Yarn
yarn add near-api-js@^0.44.2

# Using NPM.
npm install near-api-js@^0.44.2
```
```bash
# Using Yarn
yarn add @near-wallet-selector/default-wallets

# Using NPM.
npm install @near-wallet-selector/default-wallets
```

Then use it in your dApp:

```ts
import { setupWalletSelector } from "@near-wallet-selector/core";
import { setupDefaultWallets } from "@near-wallet-selector/default-wallets";

const selector = await setupWalletSelector({
network: "testnet",
modules: await setupDefaultWallets(),
});
```


## Options

The `setupDefaultWallets` does not take any options. </br>
To customize any of the default wallets listed above, follow these steps for example customize setup for WalletConnect:

>**Note**: In this case the customized wallet setup function must be placed before the `setupDefaultWallets` call.

**Example**

```ts
// The same can be done for MyNearWallet and Ledger too.
import { setupWalletSelector } from "@near-wallet-selector/core";
import { setupWalletConnect } from "@near-wallet-selector/wallet-connect";
import { setupDefaultWallets } from "@near-wallet-selector/default-wallets";

const selector = await setupWalletSelector({
network: "testnet",
modules: [
setupWalletConnect({
projectId: "your-custom-project-id",
metadata: {
name: "NEAR Wallet Selector",
description: "Example dApp used by NEAR Wallet Selector",
url: "https://github.com/near/wallet-selector",
icons: ["https://avatars.githubusercontent.com/u/37784886"],
},
}),
...(await setupDefaultWallets())
],
});
```

## License

This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0).
15 changes: 15 additions & 0 deletions packages/default-wallets/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable */
export default {
displayName: "default-wallets",
preset: "../../jest.preset.js",
globals: {
"ts-jest": {
tsconfig: "<rootDir>/tsconfig.spec.json",
},
},
transform: {
"^.+\\.[tj]s$": "ts-jest",
},
moduleFileExtensions: ["ts", "tsx", "js", "jsx"],
coverageDirectory: "../../coverage/packages/default-wallets",
};
4 changes: 4 additions & 0 deletions packages/default-wallets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@near-wallet-selector/default-wallets",
"version": "5.1.0"
}
69 changes: 69 additions & 0 deletions packages/default-wallets/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/default-wallets/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nrwl/web:rollup",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/default-wallets",
"tsConfig": "packages/default-wallets/tsconfig.lib.json",
"project": "packages/default-wallets/package.json",
"entryFile": "packages/default-wallets/src/index.ts",
"buildableProjectDepsInPackageJsonType": "dependencies",
"compiler": "babel",
"format": ["esm", "cjs"],
"assets": [
{
"glob": "packages/default-wallets/README.md",
"input": ".",
"output": "."
},
{
"glob": "packages/default-wallets/assets/*",
"input": ".",
"output": "assets"
},
{
"glob": "**/*",
"input": "packages/my-near-wallet/assets/",
"output": "assets/"
},
{
"glob": "**/*",
"input": "packages/ledger/assets/",
"output": "assets/"
},
{
"glob": "**/*",
"input": "packages/wallet-connect/assets/",
"output": "assets/"
}
]
}
},
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["packages/default-wallets/**/*.ts"]
}
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["coverage/packages/default-wallets"],
"options": {
"jestConfig": "packages/default-wallets/jest.config.ts",
"passWithNoTests": true
}
},
"deploy": {
"executor": "ngx-deploy-npm:deploy",
"options": {
"access": "public"
}
}
},
"tags": ["hardware-wallet"]
}
1 change: 1 addition & 0 deletions packages/default-wallets/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { setupDefaultWallets } from "./lib/default-wallets";
22 changes: 22 additions & 0 deletions packages/default-wallets/src/lib/default-wallets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { setupMyNearWallet } from "@near-wallet-selector/my-near-wallet";
import { setupLedger } from "@near-wallet-selector/ledger";
import { setupWalletConnect } from "@near-wallet-selector/wallet-connect";
import type { WalletModuleFactory } from "@near-wallet-selector/core";

export const setupDefaultWallets = async (): Promise<
Array<WalletModuleFactory>
> => {
return [
setupMyNearWallet(),
setupLedger(),
setupWalletConnect({
projectId: "c8cb6204543639c31aef44ea4837a554",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm -- where did this projectId come from?

Copy link
Contributor Author

@kujtimprenkuSQA kujtimprenkuSQA Aug 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is coming from here: https://cloud.walletconnect.com/sign-in .
I have mentioned it a few times on Slack that if we add WalletConnect to the default wallets it will require some params ( This is a hardcoded - default projectId otherwise the pairing does not work without it).

Update:

WalletConnect is one of those wallet options that would require a projectId which is different for each dApp.
These options should be more dApp specific than Wallet Selector specific:

{
   projectId: "c8cb6204543639c31aef44ea4837a554",
   metadata: {
    name: "NEAR Wallet Selector",
    description: "Example dApp used by NEAR Wallet Selector",
    url: "https://github.com/near/wallet-selector",
    icons: ["https://avatars.githubusercontent.com/u/37784886"],
   },
}

metadata: {
name: "NEAR Wallet Selector",
description: "Example dApp used by NEAR Wallet Selector",
url: "https://github.com/near/wallet-selector",
icons: ["https://avatars.githubusercontent.com/u/37784886"],
},
}),
];
};
19 changes: 19 additions & 0 deletions packages/default-wallets/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
10 changes: 10 additions & 0 deletions packages/default-wallets/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": []
},
"include": ["**/*.ts"],
"exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"]
}
Loading