on d55748d help sign in
bridge/src/lib/authToken.js
44 lines 1.1 kB history raw
1import * as secp256k1 from 'secp256k1';
3import { keccak256, WALLET_TYPES } from './wallet';
4import { ledgerSignMessage } from './ledger';
5import { trezorSignMessage } from './trezor';
7const MESSAGE = 'Bridge Authentication Token';
9function signMessage(privateKey) {
10 const { signature } = secp256k1.sign(
11 keccak256('\x19Ethereum Signed Message:\n' + MESSAGE.length + MESSAGE),
12 privateKey
13 );
14 // add key recovery parameter
15 const ethSignature = new Uint8Array(65);
16 ethSignature.set(signature);
17 const v = (ethSignature[32] & 1) + 27;
18 ethSignature[64] = v;
20 return ethSignature;
23export const getAuthToken = async ({
24 wallet,
25 walletType,
26 walletHdPath,
27 web3,
28}) => {
29 if (walletType === WALLET_TYPES.METAMASK) {
30 return web3.eth.personal.sign(MESSAGE, wallet.address, '');
31 }
32 if (walletType === WALLET_TYPES.LEDGER) {
33 return ledgerSignMessage(MESSAGE, walletHdPath);
34 }
35 if (walletType === WALLET_TYPES.TREZOR) {
36 return trezorSignMessage(MESSAGE, walletHdPath);
37 }
39 const signature = signMessage(wallet.privateKey);
41 const token = `0x${Buffer.from(signature).toString('hex')}`;
43 return token;
44};