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: verify receive address on BitBox02 hardware #2377

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion _raw/locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2124,7 +2124,8 @@
"Done": "Done",
"Nonce": "Nonce",
"tryAgain": "Try Again",
"notSupportTesntnet": "Not supported for custom network"
"notSupportTesntnet": "Not supported for custom network",
"verifyAddress": "Verify address on device"
},
"background": {
"error": {
Expand Down
5 changes: 5 additions & 0 deletions src/background/controller/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2934,6 +2934,11 @@ export class WalletController extends BaseController {
return this._setCurrentAccountFromKeyring(keyringInstance, -1);
};

verifyAddress = async (type: string, from: string) => {
const keyring = await keyringService.getKeyringForAccount(from, type);
return await keyringService.verifyAddress(keyring, from);
};

getSignTextHistory = (address: string) => {
return signTextHistoryService.getHistory(address);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ export interface BitBox02BridgeInterface {
keypath: string,
msg: string
) => ReturnType<PairedBitBox['ethSignTypedMessage']>;
ethAddress: (
chainId: number,
keypath: string,
display: boolean
) => ReturnType<PairedBitBox['ethAddress']>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,15 @@ export default class BitBox02Bridge implements BitBox02BridgeInterface {
}
return this.app.ethSignTypedMessage(BigInt(chainId), keypath, message);
};

ethAddress: BitBox02BridgeInterface['ethAddress'] = async (
chainId,
keypath,
display = true
) => {
if (!this.app) {
throw new Error('Device not initialized');
}
return this.app.ethAddress(BigInt(chainId), keypath, display);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,22 @@ export default class BitBox02OffscreenBridge
});
});
};

ethAddress: BitBox02BridgeInterface['ethAddress'] = async (...params) => {
return new Promise((resolve, reject) => {
browser.runtime
.sendMessage({
target: OffscreenCommunicationTarget.bitbox02Offscreen,
action: BitBox02Action.ethAddress,
params,
})
.then((res) => {
if (res?.error) {
reject(res.error);
} else {
resolve(res);
}
});
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,15 @@ class BitBox02Keyring extends EventEmitter {
});
}

async verifyAddress(withAccount) {
await this.init();
return await this.bridge.ethAddress(
1,
this._pathFromAddress(withAccount),
true
);
}

exportAccount(): Promise<any> {
return Promise.reject(new Error('Not supported on this device'));
}
Expand Down
12 changes: 12 additions & 0 deletions src/background/service/keyring/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,18 @@ export class KeyringService extends EventEmitter {
return keyring.exportAccount(address, { withAppKeyOrigin: origin });
}

/**
* Show address on device
* Display receive address on hardware device for verification
*
* @param {Object} msgParams - The account parameters.
* @returns {Promise<string>} The account address.
*/
verifyAddress(keyring, from) {
const address = normalizeAddress(from);
return keyring.verifyAddress(address);
}

/**
* Persist All Keyrings
*
Expand Down
1 change: 1 addition & 0 deletions src/constant/offscreen-communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export enum BitBox02Action {
ethSignMessage = 'bitbox02-sign-message',
ethSignTypedMessage = 'bitbox02-sign-typed-message',
ethXpub = 'bitbox02-xpub',
ethAddress = 'bitbox02-eth-address',
}

export enum LatticeAction {
Expand Down
9 changes: 9 additions & 0 deletions src/offscreen/scripts/bitbox02.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ export function initBitBox02() {
});
break;

case BitBox02Action.ethAddress:
bridge
.ethAddress(...(msg.params as [any, any, any]))
.then(sendResponse)
.catch((err) => {
sendResponse({ error: err });
});
break;

default:
sendResponse({
success: false,
Expand Down
5 changes: 5 additions & 0 deletions src/ui/assets/icon-eye-black.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 41 additions & 10 deletions src/ui/views/Receive/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ReactComponent as IconBack } from 'ui/assets/back.svg';
import { ReactComponent as RcIconCopy } from 'ui/assets/icon-copy-1-cc.svg';
import IconEyeHide from 'ui/assets/icon-eye-hide.svg';
import IconEye from 'ui/assets/icon-eye.svg';
import IconEyeBlack from 'ui/assets/icon-eye-black.svg';
import IconSuccess from 'ui/assets/icon-success-1.svg';
import { ReactComponent as RcIconWarning } from 'ui/assets/icon-warning-large.svg';
import { splitNumberByStep, useWallet } from 'ui/utils';
Expand Down Expand Up @@ -104,6 +105,14 @@ const Receive = () => {
copyAddress(account.address!);
};

const handleVerifyAddress = () => {
if (!account) throw new Error(t('background.error.noCurrentAccount'));
return wallet.verifyAddress(
account.type as string,
account.address as string
);
};

const init = async () => {
const account = await wallet.syncGetCurrentAccount();

Expand Down Expand Up @@ -233,17 +242,39 @@ const Receive = () => {
{account?.address && <QRCode value={account.address} size={175} />}
</div>
<div className="qr-card-address">{account?.address}</div>
<button
type="button"
className="qr-card-btn"
onClick={handleCopyAddress}
<div
className={
account?.type === KEYRING_CLASS.HARDWARE.BITBOX02
? 'qr-card-btn-container'
: ''
}
>
<ThemeIcon
src={RcIconCopy}
className="icon-copy text-r-neutral-title-1"
/>
{t('global.copyAddress')}
</button>
<button
type="button"
className="qr-card-btn"
onClick={handleCopyAddress}
>
<ThemeIcon
src={RcIconCopy}
className="icon-copy text-r-neutral-title-1"
/>
{t('global.copyAddress')}
</button>

{account?.type === KEYRING_CLASS.HARDWARE.BITBOX02 && (
<button
type="button"
className="qr-card-btn"
onClick={handleVerifyAddress}
>
<ThemeIcon
src={IconEyeBlack}
className="icon-copy text-r-neutral-title-1"
/>
{t('global.verifyAddress')}
</button>
)}
</div>
</div>
<div className="page-receive-footer">
<img
Expand Down
12 changes: 12 additions & 0 deletions src/ui/views/Receive/style.less
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,17 @@
margin-right: 6px;
}
}
&-btn-container {
display: flex;
gap: 10px;
justify-content: center;
margin-left: 10px;
margin-right: 10px;

.qr-card-btn {
margin: 0;
padding: 12px 16px;
}
}
}
}