-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into metamask-snaps-inte…
…gration
- Loading branch information
Showing
11 changed files
with
1,928 additions
and
558 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"compilerOptions": { | ||
"resolveJsonModule": true, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "@shapeshiftoss/hdwallet-walletconnectv2", | ||
"version": "1.50.6", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"main": "dist/index.js", | ||
"source": "src/index.ts", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "tsc --build", | ||
"clean": "rm -rf dist tsconfig.tsbuildinfo", | ||
"dev": "yarn tsc --build --watch", | ||
"prepublishOnly": "yarn clean && yarn build" | ||
}, | ||
"dependencies": { | ||
"@shapeshiftoss/hdwallet-core": "^1.50.6", | ||
"@walletconnect/ethereum-provider": "^2.10.1", | ||
"@walletconnect/modal": "^2.6.2", | ||
"ethers": "^5.6.5" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^4.3.2" | ||
}, | ||
"gitHead": "64bf68d0087821d82d2c714a82991cee8dbdc4b3" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Events, Keyring } from "@shapeshiftoss/hdwallet-core"; | ||
import { EthereumProvider } from "@walletconnect/ethereum-provider"; | ||
import { EthereumProviderOptions } from "@walletconnect/ethereum-provider/dist/types/EthereumProvider"; | ||
|
||
import { WalletConnectV2HDWallet } from "./walletconnectV2"; | ||
|
||
export class WalletConnectV2Adapter { | ||
keyring: Keyring; | ||
private readonly providerConfig: EthereumProviderOptions; | ||
|
||
private constructor(keyring: Keyring, config: EthereumProviderOptions) { | ||
this.keyring = keyring; | ||
this.providerConfig = config; | ||
} | ||
|
||
public static useKeyring(keyring: Keyring, config: EthereumProviderOptions) { | ||
return new WalletConnectV2Adapter(keyring, config); | ||
} | ||
|
||
public async initialize(): Promise<number> { | ||
return Object.keys(this.keyring.wallets).length; | ||
} | ||
|
||
public async pairDevice(): Promise<WalletConnectV2HDWallet> { | ||
try { | ||
if (!this.providerConfig) { | ||
throw new Error("WalletConnectV2 provider configuration not set."); | ||
} | ||
|
||
const provider = await EthereumProvider.init(this.providerConfig); | ||
const wallet = new WalletConnectV2HDWallet(provider); | ||
|
||
// Enable session (triggers QR Code modal) | ||
await wallet.initialize(); | ||
const deviceID = await wallet.getDeviceID(); | ||
this.keyring.add(wallet, deviceID); | ||
this.keyring.emit(["WalletConnectV2", deviceID, Events.CONNECT], deviceID); | ||
return wallet; | ||
} catch (error) { | ||
console.error("Could not pair WalletConnectV2"); | ||
throw error; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import type { | ||
BIP32Path, | ||
ETHSignedMessage, | ||
ETHSignedTx, | ||
ETHSignTx, | ||
ETHTxHash, | ||
ETHVerifyMessage, | ||
PathDescription, | ||
} from "@shapeshiftoss/hdwallet-core"; | ||
import { addressNListToBIP32, slip44ByCoin } from "@shapeshiftoss/hdwallet-core"; | ||
import EthereumProvider from "@walletconnect/ethereum-provider"; | ||
import type { Bytes } from "ethers"; | ||
import { arrayify, isBytes } from "ethers/lib/utils"; | ||
|
||
const getUnsignedTxFromMessage = (msg: ETHSignTx & { from: string }) => { | ||
const utxBase = { | ||
from: msg.from, | ||
to: msg.to, | ||
value: msg.value, | ||
data: msg.data, | ||
chainId: msg.chainId, | ||
nonce: msg.nonce, | ||
gasLimit: msg.gasLimit, | ||
}; | ||
|
||
return msg.maxFeePerGas | ||
? { | ||
...utxBase, | ||
maxFeePerGas: msg.maxFeePerGas, | ||
maxPriorityFeePerGas: msg.maxPriorityFeePerGas, | ||
} | ||
: { ...utxBase, gasPrice: msg.gasPrice }; | ||
}; | ||
|
||
export function describeETHPath(path: BIP32Path): PathDescription { | ||
const pathStr = addressNListToBIP32(path); | ||
const unknown: PathDescription = { | ||
verbose: pathStr, | ||
coin: "Ethereum", | ||
isKnown: false, | ||
}; | ||
|
||
if (path.length !== 5) return unknown; | ||
|
||
if (path[0] !== 0x80000000 + 44) return unknown; | ||
|
||
if (path[1] !== 0x80000000 + slip44ByCoin("Ethereum")) return unknown; | ||
|
||
if ((path[2] & 0x80000000) >>> 0 !== 0x80000000) return unknown; | ||
|
||
if (path[3] !== 0) return unknown; | ||
|
||
if (path[4] !== 0) return unknown; | ||
|
||
const index = path[2] & 0x7fffffff; | ||
return { | ||
verbose: `Ethereum Account #${index}`, | ||
accountIdx: index, | ||
wholeAccount: true, | ||
coin: "Ethereum", | ||
isKnown: true, | ||
}; | ||
} | ||
|
||
export async function ethSignTx( | ||
args: ETHSignTx & { from: string }, | ||
provider: EthereumProvider | ||
): Promise<ETHSignedTx | null> { | ||
const utx = getUnsignedTxFromMessage(args); | ||
return await provider.request({ method: "eth_signTransaction", params: [utx] }); | ||
} | ||
|
||
export async function ethSendTx( | ||
msg: ETHSignTx & { from: string }, | ||
provider: EthereumProvider | ||
): Promise<ETHTxHash | null> { | ||
const utx = getUnsignedTxFromMessage(msg); | ||
const txHash: string = await provider.request({ method: "eth_sendTransaction", params: [utx] }); | ||
return txHash | ||
? { | ||
hash: txHash, | ||
} | ||
: null; | ||
} | ||
|
||
export async function ethSignMessage( | ||
args: { data: string | Bytes; fromAddress: string }, | ||
provider: EthereumProvider | ||
): Promise<ETHSignedMessage | null> { | ||
const buffer = isBytes(args.data) ? Buffer.from(arrayify(args.data)) : Buffer.from(args.data); | ||
|
||
return await provider.request({ | ||
method: "eth_sign", | ||
params: [args.fromAddress, buffer], | ||
}); | ||
} | ||
|
||
export async function ethGetAddress(provider: EthereumProvider): Promise<string | null> { | ||
try { | ||
if (!(provider && provider.connected)) { | ||
throw new Error("No WalletConnectV2 provider available."); | ||
} | ||
const ethAccounts: string[] = await provider.request({ | ||
method: "eth_accounts", | ||
}); | ||
return ethAccounts[0]; | ||
} catch (error) { | ||
console.error(error); | ||
return null; | ||
} | ||
} | ||
|
||
export async function ethVerifyMessage(provider: EthereumProvider, args: ETHVerifyMessage): Promise<boolean> { | ||
return await provider.request({ method: "ethVerifyMessage", params: [args.message, args.signature] }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./adapter"; | ||
export * from "./walletconnectV2"; |
Oops, something went wrong.