on master help sign in
bridge/src/lib/authToken.ts
120 lines 3.8 kB history raw
1import { crypto } from 'bitcoinjs-lib';
2import { ecdsaSign } from 'secp256k1';
3import Web3 from 'web3';
4import { hexToBytes, utf8ToHex } from 'web3-utils';
5import { hashPersonalMessage } from '@ethereumjs/util';
6import { WALLET_TYPES } from './constants';
7import BridgeWallet from './types/BridgeWallet';
8import { Hash } from '@urbit/roller-api';
10const MESSAGE = 'Bridge Authentication Token';
12function signMessage(privateKey: Buffer, useLegacyTokenSigning = false) {
13 const msg = '\x19Ethereum Signed Message:\n' + MESSAGE.length + MESSAGE;
14 const hashed = useLegacyTokenSigning
15 ? crypto.sha256(Buffer.from(msg))
16 : hashPersonalMessage(Buffer.from(MESSAGE)); // msg prefix is handled by lib
18 const hashAry = new Uint8Array(hashed.buffer);
19 const { signature } = ecdsaSign(hashAry, privateKey);
21 // add key recovery parameter
22 const ethSignature = new Uint8Array(65);
23 ethSignature.set(signature);
25 // https://eips.ethereum.org/EIPS/eip-155
26 // This method for setting the recovery parameter is the "legacy" way of doing
27 // so; and in fact, does not match up with output of Metamask / Brave Wallet.
28 // MM uses 28 instead of 27.
29 // TODO: Should we bring this up to latest standard? The problem is, users
30 // who have set network keys or generated invites with an older token will
31 // not be able to rederive them
32 const v = (ethSignature[32] & 1) + 27;
33 ethSignature[64] = v;
35 return ethSignature;
38export function signTransactionHash(msg: Hash, prvKey: Buffer) {
39 // msg is a keccak-256 hash
40 //
41 const hashed = Buffer.from(hexToBytes(msg));
42 const { signature, recid } = ecdsaSign(hashed, prvKey);
43 // add key recovery parameter
44 const ethSignature = new Uint8Array(65);
45 ethSignature.set(signature);
46 ethSignature[64] = recid;
47 return `0x${Buffer.from(ethSignature).toString('hex')}`;
50type MetamaskAuthTokenArgs = {
51 address: string;
52 web3: Web3;
53 walletType: symbol;
54};
56type WalletConnectAuthTokenArgs = {
57 address: string;
58 signPersonalMessage: Function;
59 walletType: symbol;
60};
62type DefaultAuthTokenArgs = {
63 wallet: BridgeWallet;
64 walletType?: symbol;
65 useLegacyTokenSigning?: boolean;
66};
68type GetAuthTokenArgs =
69 | MetamaskAuthTokenArgs
70 | WalletConnectAuthTokenArgs
71 | DefaultAuthTokenArgs;
73const getMetamaskAuthToken = ({ address, web3 }: MetamaskAuthTokenArgs) => {
74 if (window.ethereum) {
75 //NOTE this doesn't _seem_ to be affected by #596,
76 // but web3.eth.personal.sign hits it semi-reliably?
77 // no idea what's going on, we should figure it out,
78 // but we apply this bandaid to hopefully stop the bleeding.
79 //NOTE personal_sign expects hex-encoded data; signing the hex of
80 // MESSAGE yields the same signature as the other wallet types
81 return window.ethereum.request({
82 method: 'personal_sign',
83 params: [utf8ToHex(MESSAGE), address],
84 });
85 } else {
86 return web3.eth.personal.sign(MESSAGE, address, '');
87 }
88};
90const getWalletConnectAuthToken = async ({
91 address,
92 signPersonalMessage,
93}: WalletConnectAuthTokenArgs) => {
94 return await signPersonalMessage({ message: MESSAGE, address: address });
95};
97const getDefaultAuthToken = ({
98 wallet,
99 useLegacyTokenSigning = false,
100}: DefaultAuthTokenArgs) => {
101 const signature = signMessage(wallet.privateKey!, useLegacyTokenSigning);
103 const token = `0x${Buffer.from(signature).toString('hex')}`;
105 return token;
106};
108export const getAuthToken = async ({
109 walletType,
110 ...args
111}: GetAuthTokenArgs) => {
112 switch (walletType) {
113 case WALLET_TYPES.METAMASK:
114 return getMetamaskAuthToken(args as MetamaskAuthTokenArgs);
115 case WALLET_TYPES.WALLET_CONNECT:
116 return getWalletConnectAuthToken(args as WalletConnectAuthTokenArgs);
117 default:
118 return getDefaultAuthToken(args as DefaultAuthTokenArgs);
119 }
120};