Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

btc: add hex support to sign message. #527

Merged
merged 1 commit into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/js/core/methods/EthereumGetAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import AbstractMethod from './AbstractMethod';
import { validateParams, getFirmwareRange } from './helpers/paramsValidator';
import { validatePath, getSerializedPath } from '../../utils/pathUtils';
import { getNetworkLabel, stripHexPrefix } from '../../utils/ethereumUtils';
import { getNetworkLabel } from '../../utils/ethereumUtils';
import { getEthereumNetwork, getUniqueNetworks } from '../../data/CoinInfo';
import { stripHexPrefix } from '../../utils/formatUtils';

import * as UI from '../../constants/ui';
import { UiMessage } from '../../message/builder';
Expand Down
3 changes: 2 additions & 1 deletion src/js/core/methods/EthereumSignMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import AbstractMethod from './AbstractMethod';
import { validateParams, getFirmwareRange } from './helpers/paramsValidator';
import { validatePath } from '../../utils/pathUtils';
import { getEthereumNetwork } from '../../data/CoinInfo';
import { toChecksumAddress, getNetworkLabel, messageToHex } from '../../utils/ethereumUtils';
import { toChecksumAddress, getNetworkLabel } from '../../utils/ethereumUtils';
import { messageToHex } from '../../utils/formatUtils';

import type { MessageSignature } from '../../types/trezor';
import type { CoreMessage, EthereumNetworkInfo } from '../../types';
Expand Down
3 changes: 2 additions & 1 deletion src/js/core/methods/EthereumSignTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import AbstractMethod from './AbstractMethod';
import { validateParams, getFirmwareRange } from './helpers/paramsValidator';
import { validatePath } from '../../utils/pathUtils';
import { getEthereumNetwork } from '../../data/CoinInfo';
import { stripHexPrefix, getNetworkLabel } from '../../utils/ethereumUtils';
import { getNetworkLabel } from '../../utils/ethereumUtils';
import { stripHexPrefix } from '../../utils/formatUtils';
import * as helper from './helpers/ethereumSignTx';

import type { CoreMessage } from '../../types';
Expand Down
2 changes: 1 addition & 1 deletion src/js/core/methods/EthereumVerifyMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { validateParams, getFirmwareRange } from './helpers/paramsValidator';
import type { Success } from '../../types/trezor';
import type { CoreMessage } from '../../types';

import { stripHexPrefix, messageToHex } from '../../utils/ethereumUtils';
import { stripHexPrefix, messageToHex } from '../../utils/formatUtils';

type Params = {
address: string,
Expand Down
4 changes: 3 additions & 1 deletion src/js/core/methods/SignMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { validatePath, getLabel } from '../../utils/pathUtils';
import { getBitcoinNetwork } from '../../data/CoinInfo';
import type { MessageSignature } from '../../types/trezor';
import type { CoreMessage, BitcoinNetworkInfo } from '../../types';
import { messageToHex } from '../../utils/formatUtils';

type Params = {
path: Array<number>,
Expand All @@ -28,6 +29,7 @@ export default class SignMessage extends AbstractMethod {
{ name: 'path', obligatory: true },
{ name: 'coin', type: 'string' },
{ name: 'message', type: 'string', obligatory: true },
{ name: 'hex', type: 'boolean' },
]);

const path: Array<number> = validatePath(payload.path);
Expand All @@ -46,7 +48,7 @@ export default class SignMessage extends AbstractMethod {
this.firmwareRange = getFirmwareRange(this.name, coinInfo, this.firmwareRange);
}

const messageHex: string = Buffer.from(payload.message, 'utf8').toString('hex');
const messageHex: string = payload.hex ? messageToHex(payload.message) : Buffer.from(payload.message, 'utf8').toString('hex');
this.params = {
path,
message: messageHex,
Expand Down
5 changes: 3 additions & 2 deletions src/js/core/methods/VerifyMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { NO_COIN_INFO } from '../../constants/errors';

import type { Success } from '../../types/trezor';
import type { CoreMessage, BitcoinNetworkInfo } from '../../types';
import { messageToHex } from '../../utils/formatUtils';

type Params = {
address: string,
Expand All @@ -33,6 +34,7 @@ export default class VerifyMessage extends AbstractMethod {
{ name: 'signature', type: 'string', obligatory: true },
{ name: 'message', type: 'string', obligatory: true },
{ name: 'coin', type: 'string', obligatory: true },
{ name: 'hex', type: 'boolean' },
]);

const coinInfo: ?BitcoinNetworkInfo = getBitcoinNetwork(payload.coin);
Expand All @@ -43,8 +45,7 @@ export default class VerifyMessage extends AbstractMethod {
this.firmwareRange = getFirmwareRange(this.name, coinInfo, this.firmwareRange);
this.info = getLabel('Verify #NETWORK message', coinInfo);
}
// TODO: check if message is already a hex
const messageHex: string = Buffer.from(payload.message, 'utf8').toString('hex');
const messageHex: string = payload.hex ? messageToHex(payload.message) : Buffer.from(payload.message, 'utf8').toString('hex');
const signatureHex: string = Buffer.from(payload.signature, 'base64').toString('hex');

this.params = {
Expand Down
32 changes: 1 addition & 31 deletions src/js/utils/ethereumUtils.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
/* @flow */
import createKeccakHash from 'keccak';
import type { EthereumNetworkInfo, CoinInfo } from '../types';

const hasHexPrefix = (str: string): boolean => {
return str.slice(0, 2).toLowerCase() === '0x';
};

export const stripHexPrefix = (str: string): string => {
return hasHexPrefix(str) ? str.slice(2) : str;
};
import { hasHexPrefix, stripHexPrefix } from './formatUtils';

export const toChecksumAddress = (address: string, network: ?EthereumNetworkInfo): string => {
if (hasHexPrefix(address)) return address;
Expand All @@ -34,26 +27,3 @@ export const getNetworkLabel = (label: string, network: ?CoinInfo): string => {
}
return label.replace('#NETWORK', '');
};

// from (isHexString) https://github.com/ethjs/ethjs-util/blob/master/src/index.js
const isHexString = (value: string, length?: number) => {
if (typeof value !== 'string' || !value.match(/^(0x|0X)?[0-9A-Fa-f]*$/)) {
return false;
}
if (length && value.length !== 2 + 2 * length) { return false; }
return true;
};

// from (toBuffer) https://github.com/ethereumjs/ethereumjs-util/blob/master/index.js
export const messageToHex = (message: string): string => {
let buffer: Buffer;
if (isHexString(message)) {
let clean = stripHexPrefix(message);
// pad left even
if (clean.length % 2 !== 0) { clean = '0' + clean; }
buffer = Buffer.from(clean, 'hex');
} else {
buffer = Buffer.from(message);
}
return buffer.toString('hex');
};
31 changes: 31 additions & 0 deletions src/js/utils/formatUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,34 @@ export const formatTime = (n: number): string => {
export const btckb2satoshib = (n: string): string => {
return new BigNumber(n).times(1e5).toFixed(0, BigNumber.ROUND_HALF_UP);
};

export const hasHexPrefix = (str: string): boolean => {
return str.slice(0, 2).toLowerCase() === '0x';
};

export const stripHexPrefix = (str: string): string => {
return hasHexPrefix(str) ? str.slice(2) : str;
};

// from (isHexString) https://github.com/ethjs/ethjs-util/blob/master/src/index.js
const isHexString = (value: string, length?: number) => {
if (typeof value !== 'string' || !value.match(/^(0x|0X)?[0-9A-Fa-f]*$/)) {
return false;
}
if (length && value.length !== 2 + 2 * length) { return false; }
return true;
};

// from (toBuffer) https://github.com/ethereumjs/ethereumjs-util/blob/master/index.js
export const messageToHex = (message: string): string => {
let buffer: Buffer;
if (isHexString(message)) {
let clean = stripHexPrefix(message);
// pad left even
if (clean.length % 2 !== 0) { clean = '0' + clean; }
buffer = Buffer.from(clean, 'hex');
} else {
buffer = Buffer.from(message);
}
return buffer.toString('hex');
};