From b4f601c078152d8397e554ebdb1763ad5287c8dd Mon Sep 17 00:00:00 2001 From: Thiendekaco Date: Thu, 30 May 2024 11:37:13 +0700 Subject: [PATCH] [Issue 25] [feature] SubConnect - Support OpenBit Wallet --- packages/core/src/utils.ts | 4 ++ packages/demo/package.json | 1 + packages/demo/src/web3-onboard.ts | 3 ++ packages/metamask/package.json | 2 +- packages/metamask/src/index.ts | 22 ++++----- packages/openbit/package.json | 74 +++++++++++++++++++++++++++++++ packages/openbit/src/constant.ts | 4 ++ packages/openbit/src/icon.ts | 13 ++++++ packages/openbit/src/index.ts | 54 ++++++++++++++++++++++ packages/openbit/src/types.ts | 6 +++ packages/openbit/tsconfig.json | 16 +++++++ yarn.lock | 28 ++++++------ 12 files changed, 199 insertions(+), 28 deletions(-) create mode 100644 packages/openbit/package.json create mode 100644 packages/openbit/src/constant.ts create mode 100644 packages/openbit/src/icon.ts create mode 100644 packages/openbit/src/index.ts create mode 100644 packages/openbit/src/types.ts create mode 100644 packages/openbit/tsconfig.json diff --git a/packages/core/src/utils.ts b/packages/core/src/utils.ts index 7bd14f940..c1c1cce6d 100644 --- a/packages/core/src/utils.ts +++ b/packages/core/src/utils.ts @@ -583,6 +583,10 @@ export async function getBalanceSubstrate({ url, data } : clientAxiosProps){ 'Zeal': { platform: ['Extension'], namespace: 'zeal' + }, + 'OpenBit': { + platform: ['Extension'], + namespace: 'OpenBit' } } diff --git a/packages/demo/package.json b/packages/demo/package.json index 5ff9dbe20..5183a77d2 100644 --- a/packages/demo/package.json +++ b/packages/demo/package.json @@ -56,6 +56,7 @@ "@subwallet-connect/talisman": "^1.0.6", "@subwallet-connect/polkadot-js": "^1.0.6", "@subwallet-connect/polkadot-vault": "^1.0.6", + "@subwallet-connect/openbit": "^0.0.1", "@subwallet/chain-list": "^0.2.39", "@testing-library/jest-dom": "^5.14.1", "@testing-library/react": "^12.0.0", diff --git a/packages/demo/src/web3-onboard.ts b/packages/demo/src/web3-onboard.ts index 9d7fa5055..4be0fbc6b 100644 --- a/packages/demo/src/web3-onboard.ts +++ b/packages/demo/src/web3-onboard.ts @@ -11,6 +11,7 @@ import subwalletPolkadotModule from '@subwallet-connect/subwallet-polkadot'; import polkadotVaultModule from '@subwallet-connect/polkadot-vault'; import ledgerModule from '@subwallet-connect/ledger'; import walletConnectModule from '@subwallet-connect/walletconnect'; +import OpenBitModule from '@subwallet-connect/openbit'; import {TransactionHandlerReturn} from "@subwallet-connect/core/dist/types"; import { SubWallet, LogoSubWallet } from "../assets"; @@ -53,6 +54,7 @@ const subwalletPolkadotWalet = subwalletPolkadotModule(); const talismanWallet = talismanModule(); const polkadotVaultWallet = polkadotVaultModule(); const ledger = ledgerModule({ projectId : '59b5826141a56b204e9e0a3f7e46641d', walletConnectVersion: 2 }) +const openBitWallet = OpenBitModule(); const walletConnect = walletConnectModule({ projectId: '59b5826141a56b204e9e0a3f7e46641d', dappUrl: 'https://w3o-demo.subwallet.app/' @@ -91,6 +93,7 @@ export default init({ ledger, talismanWallet, polkadotWallet, + openBitWallet, polkadotVaultWallet, injected ], diff --git a/packages/metamask/package.json b/packages/metamask/package.json index c117d2827..876d43de4 100644 --- a/packages/metamask/package.json +++ b/packages/metamask/package.json @@ -62,7 +62,7 @@ "typescript": "^5.2.2" }, "dependencies": { - "@metamask/sdk": "^0.18.6", + "@metamask/sdk": "^0.20.3", "@subwallet-connect/common": "^1.0.6" }, "author": "" diff --git a/packages/metamask/src/index.ts b/packages/metamask/src/index.ts index 371c6351c..60c74a525 100644 --- a/packages/metamask/src/index.ts +++ b/packages/metamask/src/index.ts @@ -43,7 +43,7 @@ function metamask({ type: 'evm', getIcon: async () => (await import('./icon.js')).default, getInterface: async ({ appMetadata }) => { - sdk = (window as any).mmsdk || sdk; // Prevent conflict with existing mmsdk instances + sdk = (window as any).mmsdk || sdk // Prevent conflict with existing mmsdk instances if (sdk) { // Prevent re-initializing instance as it causes issues with MetaMask sdk mobile provider. @@ -58,14 +58,11 @@ function metamask({ const appLogoUrl = `data:image/svg+xml;base64,${base64}` const imports = await importPromise - if ( - !imports?.MetaMaskSDKConstructor || - !imports?.createEIP1193Provider - ) { + if (!imports?.MetaMaskSDKConstructor) { throw new Error('Error importing and initializing MetaMask SDK') } - const { createEIP1193Provider, MetaMaskSDKConstructor } = imports + const { MetaMaskSDKConstructor } = imports sdk = new MetaMaskSDKConstructor({ ...options, @@ -74,16 +71,15 @@ function metamask({ url: options.dappMetadata?.url || window.location.origin, base64Icon: appLogoUrl }, - _source: 'web3-onboard' + _source: 'SubConnect-v2' }) - await sdk.init(); - const provider = sdk.getProvider(); + await sdk.init() + const provider = sdk.getProvider() - const _disconnect = sdk.disconnect - if(provider) { - (provider as any).disconnect = () => { - sdk?.terminate(); + if (provider) { + ;(provider as any).disconnect = () => { + sdk?.terminate() } } diff --git a/packages/openbit/package.json b/packages/openbit/package.json new file mode 100644 index 000000000..9af766fc5 --- /dev/null +++ b/packages/openbit/package.json @@ -0,0 +1,74 @@ +{ + "name": "@subwallet-connect/openbit", + "version": "0.0.1", + "description": "OpenBit Wallet SDK wallet module for connecting to Web3-Onboard. Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardized spec compliant web3 providers for all supported wallets, framework agnostic modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.", + "keywords": [ + "Ethereum", + "Web3", + "EVM", + "dapp", + "Multichain", + "Wallet", + "Transaction", + "Provider", + "Hardware Wallet", + "Notifications", + "React", + "Svelte", + "Vue", + "Next", + "Nuxt", + "MetaMask", + "Coinbase", + "WalletConnect", + "Ledger", + "Trezor", + "Connect Wallet", + "Ethereum Hooks", + "Blocknative", + "Mempool", + "pending", + "confirmed", + "Injected Wallet", + "Crypto", + "Crypto Wallet", + "Enkrypt", + "SubWallet", + "OpenBit" + ], + "repository": { + "type": "git", + "url": "git+https://github.com/blocknative/web3-onboard.git", + "directory": "packages/subwallet" + }, + "homepage": "https://www.blocknative.com/onboard", + "bugs": { + "url": "https://github.com/blocknative/web3-onboard/issues" + }, + "module": "dist/index.js", + "browser": "dist/index.js", + "main": "dist/index.js", + "type": "module", + "typings": "dist/index.d.ts", + "files": [ + "dist" + ], + "scripts": { + "build": "tsc", + "dev": "tsc -w", + "type-check": "tsc --noEmit" + }, + "license": "MIT", + "devDependencies": { + "@ethersproject/providers": "^5.5.0", + "@types/node": "^17.0.21", + "ts-node": "^10.2.1", + "typescript": "^4.5.5", + "window": "^4.2.7" + }, + "dependencies": { + "@polkadot/extension-inject": "^0.46.5", + "@subwallet-connect/common": "^1.0.4" + }, + "author": "" +} diff --git a/packages/openbit/src/constant.ts b/packages/openbit/src/constant.ts new file mode 100644 index 000000000..e01e3a662 --- /dev/null +++ b/packages/openbit/src/constant.ts @@ -0,0 +1,4 @@ + + + +export const URL_INSTALL = 'https://www.subwallet.app/download.html' diff --git a/packages/openbit/src/icon.ts b/packages/openbit/src/icon.ts new file mode 100644 index 000000000..d78eea83d --- /dev/null +++ b/packages/openbit/src/icon.ts @@ -0,0 +1,13 @@ +export default ` + + + + + + + + + +` + + diff --git a/packages/openbit/src/index.ts b/packages/openbit/src/index.ts new file mode 100644 index 000000000..02b5b64ad --- /dev/null +++ b/packages/openbit/src/index.ts @@ -0,0 +1,54 @@ +import type { WalletInit, EIP1193Provider } from '@subwallet-connect/common' +import { + createEIP1193Provider, + SimpleEventEmitter, + WalletInterface +} from "@subwallet-connect/common"; +import { CustomWindow } from "./types.js"; +import { URL_INSTALL } from "./constant.js"; +declare const window: CustomWindow + +function OpenBit (): WalletInit { + if (typeof window === 'undefined') return () => null + + return () => { + + return { + label: 'OpenBit', + type: 'evm', + getIcon: async () => (await import('./icon.js')).default, + platforms: ['desktop'], + getInterface: async ():Promise => { + const openBitExist = window.hasOwnProperty('OpenBit'); + + + if(openBitExist){ + + const openBitProvider : EIP1193Provider = window.OpenBit as EIP1193Provider + + const addListener: SimpleEventEmitter['on'] = + openBitProvider.on.bind(openBitProvider) + + openBitProvider.on = (event, func) => { + addListener(event, func) + } + const provider = createEIP1193Provider(openBitProvider) + + provider.removeListener = (event, func) => {} + + return { + provider + } + }else{ + window.open(URL_INSTALL, '_blank') + throw new Error('Please Install SubWallet wallet to use this wallet') + } + + }, + + } + } + +} + +export default OpenBit diff --git a/packages/openbit/src/types.ts b/packages/openbit/src/types.ts new file mode 100644 index 000000000..7acb1f8e0 --- /dev/null +++ b/packages/openbit/src/types.ts @@ -0,0 +1,6 @@ +import { ExternalProvider } from "@ethersproject/providers"; + +export interface CustomWindow extends Window { + OpenBit: ExternalProvider + +} diff --git a/packages/openbit/tsconfig.json b/packages/openbit/tsconfig.json new file mode 100644 index 000000000..42d08d48d --- /dev/null +++ b/packages/openbit/tsconfig.json @@ -0,0 +1,16 @@ +{ + "extends": "../../tsconfig.json", + "include": ["src/**/*"], + + "compilerOptions": { + "outDir": "dist", + "rootDir": "src", + "allowSyntheticDefaultImports": true, + "declaration": true, + "declarationDir": "dist", + "paths": { + "*": ["./src/*", "./node_modules/*"] + }, + "typeRoots": ["node_modules/@types"] + } +} diff --git a/yarn.lock b/yarn.lock index 1c91df0e4..d18eaecaa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4141,10 +4141,10 @@ resolved "https://registry.npmjs.org/@metamask/safe-event-emitter/-/safe-event-emitter-3.0.0.tgz" integrity sha512-j6Z47VOmVyGMlnKXZmL0fyvWfEYtKWCA9yGZkU3FCsGZUT5lHGmvaV9JA5F2Y+010y7+ROtR3WMXIkvl/nVzqQ== -"@metamask/sdk-communication-layer@0.18.5": - version "0.18.5" - resolved "https://registry.yarnpkg.com/@metamask/sdk-communication-layer/-/sdk-communication-layer-0.18.5.tgz#4d153512172e6d0ff940f8ff6c50c2796ba49ceb" - integrity sha512-WMf9oJa3rAimjCXMAaaRVFPD0Q6dfZR0r9037KCpD5g9SlVh+TeMISfdQCEKAyhXP3d4CGxmp/Kib8C3mn2YLw== +"@metamask/sdk-communication-layer@0.20.2": + version "0.20.2" + resolved "https://registry.yarnpkg.com/@metamask/sdk-communication-layer/-/sdk-communication-layer-0.20.2.tgz#7f7fd334b2d26abd1a5a1ec1ffadf823a9589344" + integrity sha512-TN+whYbCClFSkx52Ild1RcjoRyz8YZgwNvZeooIcZIvCfBM6U9W5273KGiY7WLc/oO4KKmFk17d7vMO4gNvhhw== dependencies: bufferutil "^4.0.8" date-fns "^2.29.3" @@ -4152,22 +4152,22 @@ utf-8-validate "^6.0.3" uuid "^8.3.2" -"@metamask/sdk-install-modal-web@0.18.5": - version "0.18.5" - resolved "https://registry.yarnpkg.com/@metamask/sdk-install-modal-web/-/sdk-install-modal-web-0.18.5.tgz#f074f0c9a9c4d7272004ebf9792ce816418f13e0" - integrity sha512-Wygc0dgr1PwIA/Sg9WW9QWAsQr4G2GV6iveXt2xw8VKW/9cRORWqYukH1NZLr71hBKzi9AKYBU54Tk5Dfg41zg== +"@metamask/sdk-install-modal-web@0.20.2": + version "0.20.2" + resolved "https://registry.yarnpkg.com/@metamask/sdk-install-modal-web/-/sdk-install-modal-web-0.20.2.tgz#1cf0eb3c26291de7598190878fa9a893c4eb2d66" + integrity sha512-0QiaZhV15AGdN1zU2jfTI32eC3YkwEpzDfR9+oiZ9bd2G72c6lYBhTsmDGUd01aP6A+bqJR5PjI8Wh2AWtoLeA== dependencies: qr-code-styling "^1.6.0-rc.1" -"@metamask/sdk@^0.18.6": - version "0.18.6" - resolved "https://registry.yarnpkg.com/@metamask/sdk/-/sdk-0.18.6.tgz#ac6cfb80d35b179ee6efd9b2b2f6b67e27422e0a" - integrity sha512-ZT8e4BrcWrm44apLb412WR0fDsgeaS8UlI1c0wKRUPu1w/UntpXuUVO+EaY8WDlnOPAiAsjyqWKey64/DfvbXQ== +"@metamask/sdk@^0.20.3": + version "0.20.3" + resolved "https://registry.yarnpkg.com/@metamask/sdk/-/sdk-0.20.3.tgz#73851d68ffe5d45c1872c024182922530b187b7a" + integrity sha512-HZ9NwA+LxiXzuy0YWbWsuD4xejQtp85bhcCAf8UgpA/0dOyF3RS4dKDdBBXSyRgk3RWPjeJgHxioaH4CmBmiRA== dependencies: "@metamask/onboarding" "^1.0.1" "@metamask/providers" "^15.0.0" - "@metamask/sdk-communication-layer" "0.18.5" - "@metamask/sdk-install-modal-web" "0.18.5" + "@metamask/sdk-communication-layer" "0.20.2" + "@metamask/sdk-install-modal-web" "0.20.2" "@types/dom-screen-wake-lock" "^1.0.0" bowser "^2.9.0" cross-fetch "^4.0.0"