1import { crypto } from 'bitcoinjs-lib';
2import { ecdsaSign } from 'secp256k1';
3import Web3 from 'web3';
4import WalletConnect from '@walletconnect/client';
6import { WALLET_TYPES } from './constants';
7import { ledgerSignMessage } from './ledger';
8import { trezorSignMessage } from './trezor';
9import BridgeWallet from './types/BridgeWallet';
11const MESSAGE = 'Bridge Authentication Token';
13function signMessage(privateKey: Buffer) {
14 const msg = '\x19Ethereum Signed Message:\n' + MESSAGE.length + MESSAGE;
15 // #ecdsaSign requires a 32-byte buffer, hence sha256
16 const hashed = crypto.sha256(Buffer.from(msg));
17 const { signature } = ecdsaSign(Buffer.from(hashed), privateKey);
19 // add key recovery parameter
20 const ethSignature = new Uint8Array(65);
21 ethSignature.set(signature);
22 const v = (ethSignature[32] & 1) + 27;
28function signTransactionHash(privateKey: Buffer, hash: string) {
29 const msg = '\x19Ethereum Signed Message:\n' + hash.length + hash;
30 // #ecdsaSign requires a 32-byte buffer, hence sha256
31 const hashed = crypto.sha256(Buffer.from(msg));
32 const { signature } = ecdsaSign(Buffer.from(hashed), privateKey);
34 // add key recovery parameter
35 const ethSignature = new Uint8Array(65);
36 ethSignature.set(signature);
37 const v = (ethSignature[32] & 1) + 27;
43type MetamaskAuthTokenArgs = {
49type LedgerAuthTokenArgs = {
54type TrezorAuthTokenArgs = LedgerAuthTokenArgs;
56type WalletConnectAuthTokenArgs = {
58 connector: WalletConnect;
62type DefaultAuthTokenArgs = {
67type GetAuthTokenArgs =
68 | MetamaskAuthTokenArgs
71 | WalletConnectAuthTokenArgs
72 | DefaultAuthTokenArgs;
74const getMetamaskAuthToken = ({ address, web3 }: MetamaskAuthTokenArgs) => {
75 if (window.ethereum) {
76 //NOTE this doesn't _seem_ to be affected by #596,
77 // but web3.eth.personal.sign hits it semi-reliably?
78 // no idea what's going on, we should figure it out,
79 // but we apply this bandaid to hopefully stop the bleeding.
80 return window.ethereum.request({
81 method: 'personal_sign',
82 params: [MESSAGE, address],
86 return web3.eth.personal.sign(MESSAGE, address, '');
90const getLedgerAuthToken = ({ walletHdPath }: LedgerAuthTokenArgs) => {
91 return ledgerSignMessage(MESSAGE, walletHdPath);
94const getTrezorAuthToken = ({ walletHdPath }: TrezorAuthTokenArgs) => {
95 return trezorSignMessage(MESSAGE, walletHdPath);
98const getWalletConnectAuthToken = ({
101}: WalletConnectAuthTokenArgs) => {
102 return connector.signPersonalMessage([MESSAGE, address]);
105const getDefaultAuthToken = ({ wallet }: DefaultAuthTokenArgs) => {
106 const signature = signMessage(wallet.privateKey);
108 const token = `0x${Buffer.from(signature).toString('hex')}`;
113export const getAuthToken = async ({
116}: GetAuthTokenArgs) => {
117 switch (walletType) {
118 case WALLET_TYPES.METAMASK:
119 return getMetamaskAuthToken(args);
120 case WALLET_TYPES.LEDGER:
121 return getLedgerAuthToken(args);
122 case WALLET_TYPES.TREZOR:
123 return getTrezorAuthToken(args);
124 case WALLET_TYPES.WALLET_CONNECT:
125 return getWalletConnectAuthToken(args);
127 return getDefaultAuthToken(args);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list