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;
38export function signTransactionHash(msg: Hash, prvKey: Buffer) {
39 // msg is a keccak-256 hash
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 = {
56type WalletConnectAuthTokenArgs = {
58 signPersonalMessage: Function;
62type DefaultAuthTokenArgs = {
65 useLegacyTokenSigning?: boolean;
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],
86 return web3.eth.personal.sign(MESSAGE, address, '');
90const getWalletConnectAuthToken = async ({
93}: WalletConnectAuthTokenArgs) => {
94 return await signPersonalMessage({ message: MESSAGE, address: address });
97const getDefaultAuthToken = ({
99 useLegacyTokenSigning = false,
100}: DefaultAuthTokenArgs) => {
101 const signature = signMessage(wallet.privateKey!, useLegacyTokenSigning);
103 const token = `0x${Buffer.from(signature).toString('hex')}`;
108export const getAuthToken = async ({
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);
118 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