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

Metamask integration #359

Merged
merged 9 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
50 changes: 18 additions & 32 deletions examples/sandbox/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ <h4>Select</h4>
<button id="ledger">Pair Ledger</button>
<button id="portis">Pair Portis</button>
<button id="native">Pair Native</button>
<button id="metaMask">Pair MetaMask</button>

<select id="keyring" style="height: 100px" size="4"></select>
</div>
Expand Down Expand Up @@ -227,7 +228,7 @@ <h5>Get Address</h5>
<button class="button button-outline" id="osmosisAddress">Address</button>
<input type="text" id="osmosisAddressResults" />
<hr />
<h5>Sign Tx</h5>
<h5>Send</h5>
<label for="osmosisSignTxAddress">Address</label>
<input type="text" id="osmosisSignTxAddress" />
<label for="osmosisSignTxAmount">Amount (uOSMO)</label>
Expand All @@ -236,42 +237,26 @@ <h5>Sign Tx</h5>
<input type="text" id="osmosisSignTxResults" />
<hr />
<h5>Delegate</h5>
<div class="row">
<div class="column">
<label for="osmosisDelegateDelegatorAddress">Delegator Address</label>
<input type="text" id="osmosisDelegateDelegatorAddress" />
</div>
<div class="column">
<label for="osmosisDelegateValidatorAddress">Validator Address</label>
<input
type="text"
disabled
placeholder="osmovaloper1cyw4vw20el8e7ez8080md0r8psg25n0cq98a9n"
id="osmosisDelegateValidatorAddress"
/>
</div>
</div>
<label for="osmosisDelegateValidatorAddress">Validator Address</label>
<input
type="text"
disabled
placeholder="osmovaloper1cyw4vw20el8e7ez8080md0r8psg25n0cq98a9n"
id="osmosisDelegateValidatorAddress"
/>
<label for="osmosisDelegateAmount">Amount (uOSMO)</label>
<input type="text" placeholder="Ex: 10000000 = 1 full asset" id="osmosisDelegateAmount" />
<button class="button button-outline" id="osmosisDelegate">Delegate</button>
<input type="text" id="osmosisDelegateResults" />
<hr />
<h5>Undelegate</h5>
<div class="row">
<div class="column">
<label for="osmosisUndelegateDelegatorAddress">Delegator Address</label>
<input type="text" id="osmosisUndelegateDelegatorAddress" />
</div>
<div class="column">
<label for="osmosisUndelegateValidatorAddress">Validator Address</label>
<input
type="text"
disabled
placeholder="osmovaloper1cyw4vw20el8e7ez8080md0r8psg25n0cq98a9n"
id="osmosisUndelegateValidatorAddress"
/>
</div>
</div>
<label for="osmosisUndelegateValidatorAddress">Validator Address</label>
<input
type="text"
disabled
placeholder="osmovaloper1cyw4vw20el8e7ez8080md0r8psg25n0cq98a9n"
id="osmosisUndelegateValidatorAddress"
/>
<label for="osmosisUndelegateAmount">Amount (uOSMO)</label>
<input type="text" placeholder="Ex: 10000000 = 1 full asset" id="osmosisUndelegateAmount" />
<button class="button button-outline" id="osmosisUndelegate">Undelegate</button>
Expand All @@ -281,7 +266,7 @@ <h5>IBC Deposit</h5>
<hr />
<h5>IBC Withdrawal</h5>
<hr />
<h5>LP Add</h5>
<h5>Add Liquidity</h5>
<hr />
<h5>LP Stake</h5>
</div>
Expand All @@ -291,6 +276,7 @@ <h4>Ethereum</h4>
<button class="button button-outline" id="ethAddr">Address</button>
<button class="button button-outline" id="ethTx">Tx</button>
<button class="button button-outline" id="ethSign">Sign</button>
<button class="button button-outline" id="ethSend">Send</button>
<button class="button button-outline" id="ethVerify">Verify</button>
<align ="right"><button class="button button-outline" id="ethEIP1559">EIP-1559?</button></align>
<input type="text" id="ethResults" />
Expand Down
46 changes: 45 additions & 1 deletion examples/sandbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as keepkeyWebUSB from "@shapeshiftoss/hdwallet-keepkey-webusb";
import * as ledgerWebUSB from "@shapeshiftoss/hdwallet-ledger-webusb";
import * as native from "@shapeshiftoss/hdwallet-native";
import * as portis from "@shapeshiftoss/hdwallet-portis";
import * as metaMask from "@shapeshiftoss/hdwallet-metamask";
import * as trezorConnect from "@shapeshiftoss/hdwallet-trezor-connect";

import * as debug from "debug";
Expand Down Expand Up @@ -38,6 +39,7 @@ const keepkeyAdapter = keepkeyWebUSB.WebUSBKeepKeyAdapter.useKeyring(keyring);
const kkbridgeAdapter = keepkeyTcp.TCPKeepKeyAdapter.useKeyring(keyring);
const kkemuAdapter = keepkeyTcp.TCPKeepKeyAdapter.useKeyring(keyring);
const portisAdapter = portis.PortisAdapter.useKeyring(keyring, { portisAppId });
const metaMaskAdapter = metaMask.MetaMaskAdapter.useKeyring(keyring);
const nativeAdapter = native.NativeAdapter.useKeyring(keyring, {
mnemonic,
deviceId: "native-wallet-test",
Expand Down Expand Up @@ -65,6 +67,7 @@ const $trezor = $("#trezor");
const $ledger = $("#ledger");
const $portis = $("#portis");
const $native = $("#native");
const $metaMask = $("#metaMask");
const $keyring = $("#keyring");

$keepkey.on("click", async (e) => {
Expand All @@ -81,7 +84,6 @@ $keepkeybridge.on("click", async (e) => {
$("#keyring select").val(await wallet.transport.getDeviceID());
});


$kkemu.on("click", async (e) => {
e.preventDefault();
wallet = await kkemuAdapter.pairDevice("http://localhost:5000");
Expand Down Expand Up @@ -124,6 +126,19 @@ $native.on("click", async (e) => {
$("#keyring select").val(await wallet.getDeviceID());
});

$metaMask.on("click", async (e) => {
e.preventDefault();
wallet = await metaMaskAdapter.pairDevice("testid");
window["wallet"] = wallet;
let deviceID = "nothing";
try {
deviceID = await wallet.getDeviceID();
$("#keyring select").val(deviceID);
} catch (e) {
console.error(e);
}
});

async function deviceConnected(deviceId) {
wallet = keyring.get(deviceId);
if (!$keyring.find(`option[value="${deviceId}"]`).length) {
Expand Down Expand Up @@ -193,6 +208,12 @@ async function deviceConnected(deviceId) {
console.error("Could not initialize NativeAdapter", e);
}

try {
await metaMaskAdapter.initialize();
} catch (e) {
console.error("Could not initialize MetaMaskAdapter", e);
}

for (const [deviceID, wallet] of Object.entries(keyring.wallets)) {
await deviceConnected(deviceID);
}
Expand Down Expand Up @@ -1434,6 +1455,7 @@ $osmosisUndelegate.on("click", async (e) => {
const $ethAddr = $("#ethAddr");
const $ethTx = $("#ethTx");
const $ethSign = $("#ethSign");
const $ethSend = $("#ethSend");
const $ethVerify = $("#ethVerify");
const $ethResults = $("#ethResults");
const $ethEIP1559 = $("#ethEIP1559");
Expand Down Expand Up @@ -1528,6 +1550,28 @@ $ethSign.on("click", async (e) => {
}
});

$ethSend.on("click", async (e) => {
e.preventDefault();
if (!wallet) {
$ethResults.val("No wallet?");
return;
}
if (core.supportsETH(wallet)) {
let { hardenedPath: hard, relPath: rel } = wallet.ethGetAccountPaths({
coin: "Ethereum",
accountIdx: 0,
})[0];
let result = ethEIP1559Selected
? await wallet.ethSendTx(ethTx1559 as core.ETHSignTx)
: await wallet.ethSendTx(ethTx as core.ETHSignTx);
console.log("Result: ", result)
$ethResults.val(result.hash);
} else {
let label = await wallet.getLabel();
$ethResults.val(label + " does not support ETH");
}
});

$ethVerify.on("click", async (e) => {
e.preventDefault();
if (!wallet) {
Expand Down
20 changes: 10 additions & 10 deletions examples/sandbox/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hdwallet-sandbox",
"version": "1.17.0",
"version": "1.17.1-alpha.0",
"main": "index.js",
"license": "MIT",
"private": true,
Expand All @@ -10,15 +10,15 @@
"clean": "rm -rf dist .cache"
},
"dependencies": {
"@shapeshiftoss/hdwallet-core": "1.17.0",
"@shapeshiftoss/hdwallet-keepkey-tcp": "1.17.0",
"@shapeshiftoss/hdwallet-keepkey-webusb": "1.17.0",
"@shapeshiftoss/hdwallet-ledger": "1.17.0",
"@shapeshiftoss/hdwallet-ledger-webusb": "1.17.0",
"@shapeshiftoss/hdwallet-native": "1.17.0",
"@shapeshiftoss/hdwallet-portis": "1.17.0",
"@shapeshiftoss/hdwallet-trezor": "1.17.0",
"@shapeshiftoss/hdwallet-trezor-connect": "1.17.0",
"@shapeshiftoss/hdwallet-core": "1.17.1-alpha.0",
"@shapeshiftoss/hdwallet-keepkey-tcp": "1.17.1-alpha.0",
"@shapeshiftoss/hdwallet-keepkey-webusb": "1.17.1-alpha.0",
"@shapeshiftoss/hdwallet-ledger": "1.17.1-alpha.0",
"@shapeshiftoss/hdwallet-ledger-webusb": "1.17.1-alpha.0",
"@shapeshiftoss/hdwallet-native": "1.17.1-alpha.0",
"@shapeshiftoss/hdwallet-portis": "1.17.1-alpha.0",
"@shapeshiftoss/hdwallet-trezor": "1.17.1-alpha.0",
"@shapeshiftoss/hdwallet-trezor-connect": "1.17.1-alpha.0",
"bip32": "^2.0.4",
"debug": "^4.1.1",
"jquery": "^3.4.1",
Expand Down
12 changes: 6 additions & 6 deletions integration/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shapeshiftoss/integration",
"version": "1.17.0",
"version": "1.17.1-alpha.0",
"main": "index.js",
"license": "MIT",
"private": true,
Expand All @@ -10,11 +10,11 @@
"dev": "lerna run test --scope integration --parallel --include-filtered-dependencies"
},
"dependencies": {
"@shapeshiftoss/hdwallet-core": "1.17.0",
"@shapeshiftoss/hdwallet-keepkey": "1.17.0",
"@shapeshiftoss/hdwallet-keepkey-nodewebusb": "1.17.0",
"@shapeshiftoss/hdwallet-keepkey-tcp": "1.17.0",
"@shapeshiftoss/hdwallet-trezor": "1.17.0",
"@shapeshiftoss/hdwallet-core": "1.17.1-alpha.0",
"@shapeshiftoss/hdwallet-keepkey": "1.17.1-alpha.0",
"@shapeshiftoss/hdwallet-keepkey-nodewebusb": "1.17.1-alpha.0",
"@shapeshiftoss/hdwallet-keepkey-tcp": "1.17.1-alpha.0",
"@shapeshiftoss/hdwallet-trezor": "1.17.1-alpha.0",
"@types/debug": "^4.1.5",
"@types/jest": "^26.0.23",
"debug": "^4.2.0",
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"lerna": "3.22.1",
"version": "1.17.0",
"version": "1.17.1-alpha.0",
"npmClient": "yarn",
"useWorkspaces": true,
"command": {
Expand Down
2 changes: 1 addition & 1 deletion packages/hdwallet-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shapeshiftoss/hdwallet-core",
"version": "1.17.0",
"version": "1.17.1-alpha.0",
"license": "MIT",
"publishConfig": {
"access": "public"
Expand Down
5 changes: 5 additions & 0 deletions packages/hdwallet-core/src/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export interface ETHSignTx {
exchangeType?: ExchangeType;
}

export interface ETHTxHash {
hash: string;
}

export interface ETHSignedTx {
/** uint32 */
v: number;
Expand Down Expand Up @@ -134,6 +138,7 @@ export interface ETHWallet extends ETHWalletInfo, HDWallet {

ethGetAddress(msg: ETHGetAddress): Promise<string | null>;
ethSignTx(msg: ETHSignTx): Promise<ETHSignedTx | null>;
ethSendTx?(msg: ETHSignTx): Promise<ETHTxHash | null>;
ethSignMessage(msg: ETHSignMessage): Promise<ETHSignedMessage | null>;
ethVerifyMessage(msg: ETHVerifyMessage): Promise<boolean | null>;
}
Expand Down
11 changes: 11 additions & 0 deletions packages/hdwallet-core/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,17 @@ export interface HDWalletInfo {
*/
hasNativeShapeShift(srcCoin: Coin, dstCoin: Coin): boolean;

/**
* Will the device allow for transactions to be signed offline to be
* broadcasted separately?
*/
supportsOfflineSigning(): boolean

/**
* Can the device broadcast signed transactions internally?
*/
supportsBroadcast(): boolean

/**
* Describes a BIP32 path in plain English.
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/hdwallet-keepkey-chromeusb/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shapeshiftoss/hdwallet-keepkey-chromeusb",
"version": "1.17.0",
"version": "1.17.1-alpha.0",
"license": "MIT",
"publishConfig": {
"access": "public"
Expand All @@ -14,8 +14,8 @@
"prepublishOnly": "yarn clean && yarn build"
},
"dependencies": {
"@shapeshiftoss/hdwallet-core": "1.17.0",
"@shapeshiftoss/hdwallet-keepkey": "1.17.0"
"@shapeshiftoss/hdwallet-core": "1.17.1-alpha.0",
"@shapeshiftoss/hdwallet-keepkey": "1.17.1-alpha.0"
},
"devDependencies": {
"typescript": "^4.3.2"
Expand Down
4 changes: 2 additions & 2 deletions packages/hdwallet-keepkey-electron/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shapeshiftoss/hdwallet-keepkey-electron",
"version": "1.17.0",
"version": "1.17.1-alpha.0",
"license": "MIT",
"publishConfig": {
"access": "public"
Expand All @@ -14,7 +14,7 @@
"prepublishOnly": "yarn clean && yarn build"
},
"dependencies": {
"@shapeshiftoss/hdwallet-keepkey": "1.17.0",
"@shapeshiftoss/hdwallet-keepkey": "1.17.1-alpha.0",
"uuid": "^8.3.2"
},
"peerDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/hdwallet-keepkey-nodehid/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shapeshiftoss/hdwallet-keepkey-nodehid",
"version": "1.17.0",
"version": "1.17.1-alpha.0",
"license": "MIT",
"private": true,
"main": "dist/index.js",
Expand All @@ -12,7 +12,7 @@
"prepublishOnly": "yarn clean && yarn build"
},
"dependencies": {
"@shapeshiftoss/hdwallet-keepkey": "1.17.0"
"@shapeshiftoss/hdwallet-keepkey": "1.17.1-alpha.0"
},
"peerDependencies": {
"node-hid": "^0.7.7"
Expand Down
6 changes: 3 additions & 3 deletions packages/hdwallet-keepkey-nodewebusb/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shapeshiftoss/hdwallet-keepkey-nodewebusb",
"version": "1.17.0",
"version": "1.17.1-alpha.0",
"license": "MIT",
"publishConfig": {
"access": "public"
Expand All @@ -14,8 +14,8 @@
"prepublishOnly": "yarn clean && yarn build"
},
"dependencies": {
"@shapeshiftoss/hdwallet-core": "1.17.0",
"@shapeshiftoss/hdwallet-keepkey": "1.17.0"
"@shapeshiftoss/hdwallet-core": "1.17.1-alpha.0",
"@shapeshiftoss/hdwallet-keepkey": "1.17.1-alpha.0"
},
"peerDependencies": {
"webusb": "^2.2.0"
Expand Down
6 changes: 3 additions & 3 deletions packages/hdwallet-keepkey-tcp/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shapeshiftoss/hdwallet-keepkey-tcp",
"version": "1.17.0",
"version": "1.17.1-alpha.0",
"license": "MIT",
"publishConfig": {
"access": "public"
Expand All @@ -14,8 +14,8 @@
"prepublishOnly": "yarn clean && yarn build"
},
"dependencies": {
"@shapeshiftoss/hdwallet-core": "1.17.0",
"@shapeshiftoss/hdwallet-keepkey": "1.17.0",
"@shapeshiftoss/hdwallet-core": "1.17.1-alpha.0",
"@shapeshiftoss/hdwallet-keepkey": "1.17.1-alpha.0",
"axios": "^0.21.1"
},
"devDependencies": {
Expand Down
Loading