Skip to content

Commit

Permalink
Add substrate wallet into wallet module list, and resolve transaction…
Browse files Browse the repository at this point in the history
… off WalletConnect for substrate account
  • Loading branch information
Thiendekaco committed Feb 18, 2024
1 parent 95de42e commit ca0186c
Show file tree
Hide file tree
Showing 28 changed files with 839 additions and 24 deletions.
16 changes: 16 additions & 0 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ export async function getBalanceSubstrate({ url, data } : clientAxiosProps){


const TypeWalletEvmPlatformByLabel : Record<WalletState['label'], WalletStateDeviceInterface> = {
'SubWallet': {
platform: ['Extension'],
namespace: 'SubWallet'
},
'Arcana Auth' : {
platform: ['Extension', 'WebApp']
},
Expand Down Expand Up @@ -528,6 +532,18 @@ const TypeWalletSubstratePlatformByLabel : Record<WalletState['label'], WalletSt
},
'Ledger': {
platform: ['Cold Wallet']
},
'SubWallet': {
platform: ['Extension'],
namespace: 'subwallet-js'
},
'Polkadot{.js}': {
platform: ['Extension'],
namespace: 'polkadot-js'
},
'Talisman': {
platform: ['Extension'],
namespace: 'talisman'
}
}

Expand Down
23 changes: 18 additions & 5 deletions packages/core/src/views/connect/WalletButton.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,24 @@
const { platform, namespace } = WalletPlatformByLabel[typeWallet][label as WalletState['label']]
if(namespace && window !== undefined) {
if(typeWallet === 'evm' && 'ethereum' in window){
if(!(window.ethereum[namespace as keyof typeof window.ethereum]
|| window[namespace as keyof typeof window])){
statusIcon = downloadIcon;
}
if(typeWallet === 'evm'){
if('ethereum' in window){
if(!(window.ethereum[namespace as keyof typeof window.ethereum]
|| window[namespace as keyof typeof window])){
statusIcon = downloadIcon;
}
}else{
statusIcon = downloadIcon;
}
}else {
if ('injectedWeb3' in window) {
if (!(window.injectedWeb3[namespace as keyof typeof window.injectedWeb3]
|| window[namespace as keyof typeof window])) {
statusIcon = downloadIcon;
}
}else{
statusIcon = downloadIcon;
}
}
}
if(statusIcon === undefined){
Expand Down
4 changes: 4 additions & 0 deletions packages/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
"@subwallet_connect/web3auth": "^1.0.1",
"@subwallet_connect/xdefi": "^1.0.0",
"@subwallet_connect/zeal": "^1.0.0",
"@subwallet_connect/subwallet": "^1.0.0",
"@subwallet_connect/subwallet-polkadot": "^1.0.0",
"@subwallet_connect/talisman": "^1.0.0",
"@subwallet_connect/polkadot_js": "^1.0.0",
"@subwallet/chain-list": "^0.2.39",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^12.0.0",
Expand Down
18 changes: 8 additions & 10 deletions packages/demo/src/utils/api/substrateApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class substrateApi {
try{
const sendTransaction = async () => {
let txHash_ = '';
await transferExtrinsic.signAndSend(senderAddress, { signer }, ({ status, txHash }) => {
await transferExtrinsic.signAndSend(senderAddress, { signer }, ({ status, txHash }) => {
if (status.isInBlock) {
txHash_ = txHash.toString();
console.log(`Completed at block hash #${status.asInBlock.toString()}`);
Expand All @@ -39,14 +39,14 @@ export class substrateApi {
})
return txHash_
}
const txDetails = {
to: recipientAddress,
value: amount
}
const txDetails = {
to: recipientAddress,
value: amount
}

const hash = await sendTransaction();
const hash = await sendTransaction();
console.log(hash, 'hash')
} catch (e) {
} catch (e) {
console.log(':( transaction failed', e);
}

Expand All @@ -65,8 +65,7 @@ export class substrateApi {
transactionPayload: payload
};

const signature = (await provider.request(args)) as HexString;
console.log(signature);
const { signature } = (await provider.request(args)) as Pick<SignerResult, 'signature'>;
return { id: 0, signature };
}
}
Expand Down Expand Up @@ -108,4 +107,3 @@ export class substrateApi {
}



24 changes: 17 additions & 7 deletions packages/demo/src/web3-onboard.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@

import injectedModule from '@subwallet_connect/injected-wallets'
import { init } from '@subwallet_connect/react'
import walletConnectPolkadotModule from '@subwallet_connect/walletconnect-polkadot'
import injectedModule from '@subwallet_connect/injected-wallets';
import { init } from '@subwallet_connect/react';
import walletConnectPolkadotModule from '@subwallet_connect/walletconnect-polkadot';
import ledgerPolkadot from "@subwallet_connect/ledgerpolkadot";
import metamaskSDK from '@subwallet_connect/metamask'
import metamaskSDK from '@subwallet_connect/metamask';
import subwalletModule from '@subwallet_connect/subwallet';
import talismanModule from '@subwallet_connect/talisman';
import polkadot_jsModule from '@subwallet_connect/polkadot_js';
import subwalletPolkadotModule from '@subwallet_connect/subwallet-polkadot'
import {TransactionHandlerReturn} from "@subwallet_connect/core/dist/types";
import { SubWallet } from "../assets";

Expand Down Expand Up @@ -40,8 +44,10 @@ const metamaskSDKWallet = metamaskSDK({
}
}
})


const subwalletWallet = subwalletModule();
const polkadotWallet = polkadot_jsModule();
const subwalletPolkadotWalet = subwalletPolkadotModule();
const talismanWallet = talismanModule();



Expand Down Expand Up @@ -107,9 +113,13 @@ export default init({

// An array of wallet modules that you would like to be presented to the user to select from when connecting a wallet.
wallets: [
metamaskSDKWallet,
subwalletWallet,
subwalletPolkadotWalet,
walletConnectPolkadot,
metamaskSDKWallet,
ledgerPolkadot_,
talismanWallet,
polkadotWallet,
injected
],
// An array of Chains that your app supports
Expand Down
2 changes: 1 addition & 1 deletion packages/injected/src/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ declare const window: CustomWindow

const UNSUPPORTED_METHOD = null

const DAPP_NAME = 'SubWallet Connect_v2';
const DAPP_NAME = 'SubConnect';

function getInjectedInterface(
identity: string,
Expand Down
73 changes: 73 additions & 0 deletions packages/polkadotJs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"name": "@subwallet_connect/polkadot_js",
"version": "1.0.0",
"description": "Polkadot{.js} 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"
],
"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": {
"@polkadot/dev": "^0.78.7",
"typescript": "^4.5.5",
"window": "^4.2.7",
"@types/node": "17.0.19"
},
"dependencies": {
"@polkadot/extension-inject": "^0.46.5",
"@subwallet_connect/common": "^1.0.2",
"events": "^3.3.0"
},
"author": ""
}
6 changes: 6 additions & 0 deletions packages/polkadotJs/src/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const DAPP_NAME = 'SubConnect';

export const EXTENSION_NAME = 'polkadot-js';


export const URL_INSTALL = 'https://polkadot.js.org/extension'
12 changes: 12 additions & 0 deletions packages/polkadotJs/src/icon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default `
<svg width="100%" height="100%" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg">
<mask id="mask0_16261_2383" style="mask-type:alpha" maskUnits="userSpaceOnUse" x="0" y="0" width="40" height="40">
<path d="M20 0C35.1658 0 40 4.83421 40 20C40 35.1658 35.1658 40 20 40C4.83421 40 0 35.1658 0 20C0 4.83421 4.83421 0 20 0Z" fill="#004BFF"/>
</mask>
<g mask="url(#mask0_16261_2383)">
<rect width="40" height="40" fill="#FF8C00"/>
</g>
<path d="M20.1266 8.71466C15.4176 8.71466 11.5689 12.5407 11.5689 17.2723C11.5689 18.2232 11.7274 19.1514 12.0217 20.0569C12.2254 20.6682 12.9046 21.0078 13.5385 20.804C14.1498 20.6003 14.4894 19.9211 14.2856 19.2872C14.0366 18.5854 13.9234 17.8383 13.946 17.0912C14.0366 13.8991 16.6175 11.2729 19.8096 11.1144C23.364 10.9333 26.3071 13.7632 26.3071 17.2723C26.3071 20.555 23.7262 23.2491 20.4888 23.4302C20.4888 23.4302 19.2889 23.4981 18.7003 23.5887C18.406 23.634 18.1796 23.6792 18.0211 23.7019C17.9532 23.7245 17.8853 23.6566 17.9079 23.5887L18.1117 22.5925L19.221 17.4761C19.3568 16.8422 18.9493 16.2083 18.3154 16.0724C17.6815 15.9366 17.0476 16.3441 16.9118 16.978C16.9118 16.978 14.2403 29.4296 14.2177 29.5654C14.0819 30.1993 14.4894 30.8332 15.1233 30.9691C15.7572 31.1049 16.3911 30.6974 16.5269 30.0635C16.5496 29.9277 16.9118 28.275 16.9118 28.275C17.1835 27.0072 18.2249 26.079 19.4474 25.9205C19.7191 25.8752 20.7831 25.8073 20.7831 25.8073C25.1978 25.4677 28.6842 21.7775 28.6842 17.2723C28.6842 12.5407 24.8355 8.71466 20.1266 8.71466ZM20.7378 28.4108C19.9681 28.2524 19.1983 28.7278 19.0399 29.5202C18.8814 30.2899 19.3568 31.0596 20.1492 31.2181C20.9189 31.3766 21.6887 30.9012 21.8471 30.1088C22.0056 29.3164 21.5302 28.5693 20.7378 28.4108Z" fill="white"/>
</svg>
`
92 changes: 92 additions & 0 deletions packages/polkadotJs/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import type { WalletInit, EIP1193Provider } from '@subwallet_connect/common'
import { SubstrateProvider, WalletInterfaceSubstrate} from "@subwallet_connect/common";
import { InjectedMetadata, InjectedWindow } from "@polkadot/extension-inject/types";
import EventEmitter from "events";
import { Signer } from "@polkadot/types/types";
import { EXTENSION_NAME, DAPP_NAME, URL_INSTALL} from "./constant.js";

function SubWallet (): WalletInit {
if (typeof window === 'undefined') return () => null

return () => {

return {
label: 'Polkadot{.js}',
type: 'substrate',
getIcon: async () => (await import('./icon.js')).default,
platforms: ['desktop'],
getInterface: async (): Promise<WalletInterfaceSubstrate> => {
const isInstalled = (extensionName: string) => {
const injectedWindow = window as unknown as Window & InjectedWindow;
const injectedExtension =
injectedWindow?.injectedWeb3[extensionName]
return !!injectedExtension;
}

if(!isInstalled(EXTENSION_NAME)){
window.open(URL_INSTALL, '_blank');
throw new Error('Please Install Polkadot{.js} wallet to use this wallet');
}
const getRawExtension = (extensionName: string) => {
const injectedWindow = window as unknown as Window & InjectedWindow;
return injectedWindow?.injectedWeb3[extensionName];
}
const emitter = new EventEmitter()
const provider: SubstrateProvider = {
async enable() {

try {
const injectedExtension = getRawExtension(EXTENSION_NAME);

if (!injectedExtension || !injectedExtension.enable) {
return;
}

const rawExtension = await injectedExtension.enable(DAPP_NAME);
if (!rawExtension) {
return;
}
const accounts = await rawExtension.accounts.get();

return {
signer: rawExtension.signer as Signer,
metadata: rawExtension.metadata as InjectedMetadata,
address: accounts.map(
(account: { address: string }) => account.address
)
}
} catch (e) {
console.log('error', (e as Error).message);
}
},
async signDummy(address: string, data: string,
signer: Signer) {
if (signer && signer.signRaw) {
return (await signer.signRaw({
address: address,
data: 'This is dummy message',
type: 'bytes'
})).signature as string;
}
return '0x0'
},

async request() {},

async disconnect() {},

on: emitter.on.bind(emitter),
removeListener: emitter.removeListener.bind(emitter)

}

return {
provider
}
},
}
}

}

export default SubWallet
16 changes: 16 additions & 0 deletions packages/polkadotJs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}
}
Loading

0 comments on commit ca0186c

Please sign in to comment.