on b578685 help sign in
bridge/src/lib/authToken.js
31 lines 840 B history raw
1import * as secp256k1 from 'secp256k1';
3import { keccak256, WALLET_TYPES } from './wallet';
5const MESSAGE = 'Bridge Authentication Token';
7function signMessage(privateKey) {
8 const { signature } = secp256k1.sign(
9 keccak256('\x19Ethereum Signed Message:\n' + MESSAGE.length + MESSAGE),
10 privateKey
11 );
12 // add key recovery parameter
13 const ethSignature = new Uint8Array(65);
14 ethSignature.set(signature);
15 const v = (ethSignature[32] & 1) + 27;
16 ethSignature[64] = v;
18 return ethSignature;
21export const getAuthToken = ({ wallet, walletType, walletHdPath, web3 }) => {
22 if (walletType === WALLET_TYPES.METAMASK) {
23 return web3.eth.personal.sign(MESSAGE, wallet.address, '');
24 }
26 const signature = signMessage(wallet.privateKey);
28 const token = `0x${Buffer.from(signature).toString('hex')}`;
30 return token;
31};