1import { crypto } from 'bitcoinjs-lib';
2import { ecdsaSign } from 'secp256k1';
3import Web3 from 'web3';
4import { hexToBytes } from 'web3-utils';
5import WalletConnect from '@walletconnect/client';
7import { WALLET_TYPES } from './constants';
8import { ledgerSignMessage } from './ledger';
9import { trezorSignMessage } from './trezor';
10import BridgeWallet from './types/BridgeWallet';
11import { Hash } from '@urbit/roller-api';
12import { keccak256 } from 'ethereumjs-util';
14const MESSAGE = 'Bridge Authentication Token';
16function signMessage(privateKey: Buffer, useLegacyTokenSigning = false) {
17 const msg = '\x19Ethereum Signed Message:\n' + MESSAGE.length + MESSAGE;
18 const hashed = useLegacyTokenSigning
19 ? crypto.sha256(Buffer.from(msg))
20 : keccak256(Buffer.from(msg));
21 const { signature } = ecdsaSign(Buffer.from(hashed), privateKey);
23 // add key recovery parameter
24 const ethSignature = new Uint8Array(65);
25 ethSignature.set(signature);
26 const v = (ethSignature[32] & 1) + 27;
32export function signTransactionHash(msg: Hash, prvKey: Buffer) {
33 // msg is a keccak-256 hash
35 const hashed = Buffer.from(hexToBytes(msg));
36 const { signature, recid } = ecdsaSign(hashed, prvKey);
37 // add key recovery parameter
38 const ethSignature = new Uint8Array(65);
39 ethSignature.set(signature);
40 ethSignature[64] = recid;
41 return `0x${Buffer.from(ethSignature).toString('hex')}`;
44type MetamaskAuthTokenArgs = {
50type LedgerAuthTokenArgs = {
55type TrezorAuthTokenArgs = LedgerAuthTokenArgs;
57type WalletConnectAuthTokenArgs = {
59 connector: WalletConnect;
63type DefaultAuthTokenArgs = {
66 useLegacyTokenSigning?: boolean;
69type GetAuthTokenArgs =
70 | MetamaskAuthTokenArgs
73 | WalletConnectAuthTokenArgs
74 | DefaultAuthTokenArgs;
76const getMetamaskAuthToken = ({ address, web3 }: MetamaskAuthTokenArgs) => {
77 if (window.ethereum) {
78 //NOTE this doesn't _seem_ to be affected by #596,
79 // but web3.eth.personal.sign hits it semi-reliably?
80 // no idea what's going on, we should figure it out,
81 // but we apply this bandaid to hopefully stop the bleeding.
82 return window.ethereum.request({
83 method: 'personal_sign',
84 params: [MESSAGE, address],
88 return web3.eth.personal.sign(MESSAGE, address, '');
92const getLedgerAuthToken = ({ walletHdPath }: LedgerAuthTokenArgs) => {
93 return ledgerSignMessage(MESSAGE, walletHdPath);
96const getTrezorAuthToken = ({ walletHdPath }: TrezorAuthTokenArgs) => {
97 return trezorSignMessage(MESSAGE, walletHdPath);
100const getWalletConnectAuthToken = ({
103}: WalletConnectAuthTokenArgs) => {
104 return connector.signPersonalMessage([MESSAGE, address]);
107const getDefaultAuthToken = ({
109 useLegacyTokenSigning = false,
110}: DefaultAuthTokenArgs) => {
111 const signature = signMessage(wallet.privateKey!, useLegacyTokenSigning);
113 const token = `0x${Buffer.from(signature).toString('hex')}`;
118export const getAuthToken = async ({
121}: GetAuthTokenArgs) => {
122 switch (walletType) {
123 case WALLET_TYPES.METAMASK:
124 return getMetamaskAuthToken(args as MetamaskAuthTokenArgs);
125 case WALLET_TYPES.LEDGER:
126 return getLedgerAuthToken(args as LedgerAuthTokenArgs);
127 case WALLET_TYPES.TREZOR:
128 return getTrezorAuthToken(args as TrezorAuthTokenArgs);
129 case WALLET_TYPES.WALLET_CONNECT:
130 return getWalletConnectAuthToken(args as WalletConnectAuthTokenArgs);
132 return getDefaultAuthToken(args as DefaultAuthTokenArgs);
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