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

Commit

Permalink
refactor: extract hash functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfelton committed Mar 29, 2020
1 parent 196f651 commit dc9c736
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 30 deletions.
2 changes: 1 addition & 1 deletion electron/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import lndBinaryPath from '@zap/utils/lndBinaryPath'
import lndGrpcProtoDir from '@zap/utils/lndGrpcProtoDir'
import validateHost from '@zap/utils/validateHost'
import splitHostname from '@zap/utils/splitHostname'
import sha256digest from '@zap/utils/sha256'
import { sha256digest } from '@zap/utils/sha256'
import LndConfig from '@zap/utils/lndConfig'
import getPackageDetails from '@zap/utils/getPackageDetails'
import { normalizeBackupDir } from './walletBackup/local'
Expand Down
12 changes: 5 additions & 7 deletions renderer/reducers/pay/reducer.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { randomBytes, createHash } from 'crypto'
import get from 'lodash/get'
import { send } from 'redux-electron-ipc'
import { grpc } from 'workers'
import createReducer from '@zap/utils/createReducer'
import { estimateFeeRange } from '@zap/utils/fee'
import { decodePayReq, isPubkey, getTag } from '@zap/utils/crypto'
import { sha256digest } from '@zap/utils/sha256'
import { decodePayReq, isPubkey, getTag, generatePreimage } from '@zap/utils/crypto'
import { settingsSelectors } from 'reducers/settings'
import { infoSelectors } from 'reducers/info'
import { DEFAULT_CLTV_DELTA, PREIMAGE_BYTE_LENGTH, KEYSEND_PREIMAGE_TYPE } from 'reducers/payment'
import { DEFAULT_CLTV_DELTA, KEYSEND_PREIMAGE_TYPE } from 'reducers/payment'
import { createInvoice } from 'reducers/invoice'
import * as constants from './constants'

Expand Down Expand Up @@ -152,10 +152,8 @@ export const queryRoutes = (payReq, amt, finalCltvDelta = DEFAULT_CLTV_DELTA) =>
// Keysend payment.
if (isKeysend) {
pubkey = payReq
const preimage = randomBytes(PREIMAGE_BYTE_LENGTH)
paymentHash = createHash('sha256')
.update(preimage)
.digest()
const preimage = generatePreimage()
paymentHash = sha256digest(preimage)

payload = {
...payload,
Expand Down
1 change: 0 additions & 1 deletion renderer/reducers/payment/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ export const PAYMENT_STATUS_FAILED = 'failed'

export const DEFAULT_CLTV_DELTA = 43
export const KEYSEND_PREIMAGE_TYPE = '5482373484'
export const PREIMAGE_BYTE_LENGTH = 32
11 changes: 4 additions & 7 deletions renderer/reducers/payment/reducer.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import config from 'config'
import { randomBytes, createHash } from 'crypto'
import uniqBy from 'lodash/uniqBy'
import find from 'lodash/find'
import createReducer from '@zap/utils/createReducer'
import errorToUserFriendly from '@zap/utils/userFriendlyErrors'
import { getIntl } from '@zap/i18n'
import { decodePayReq, getTag, isPubkey } from '@zap/utils/crypto'
import { decodePayReq, getTag, isPubkey, generatePreimage } from '@zap/utils/crypto'
import delay from '@zap/utils/delay'
import genId from '@zap/utils/genId'
import { sha256digest } from '@zap/utils/sha256'
import { mainLog } from '@zap/utils/log'
import { CoinBig } from '@zap/utils/coin'
import { grpc } from 'workers'
Expand All @@ -31,7 +31,6 @@ const {
PAYMENT_STATUS_FAILED,
DEFAULT_CLTV_DELTA,
KEYSEND_PREIMAGE_TYPE,
PREIMAGE_BYTE_LENGTH,
} = constants

// ------------------------------------
Expand Down Expand Up @@ -252,10 +251,8 @@ export const payInvoice = ({
// Keysend payment.
if (isKeysend) {
pubkey = payReq
const preimage = randomBytes(PREIMAGE_BYTE_LENGTH)
paymentHash = createHash('sha256')
.update(preimage)
.digest()
const preimage = generatePreimage()
paymentHash = sha256digest(preimage)

payload = {
...payload,
Expand Down
4 changes: 2 additions & 2 deletions services/grpc/router.methods.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { randomBytes } from 'crypto'
import config from 'config'
import defaults from 'lodash/defaults'
import omitBy from 'lodash/omitBy'
import isNil from 'lodash/isNil'
import { grpcLog } from '@zap/utils/log'
import { generatePreimage } from '@zap/utils/crypto'
import { logGrpcCmd } from './helpers'

const PAYMENT_TIMEOUT = config.payments.timeout
Expand Down Expand Up @@ -33,7 +33,7 @@ async function probePayment(options) {

// Use a payload that has the payment hash set to some random bytes.
// This will cause the payment to fail at the final destination.
payload.paymentHash = new Uint8Array(randomBytes(32))
payload.paymentHash = new Uint8Array(generatePreimage())

logGrpcCmd('Router.probePayment', payload)

Expand Down
2 changes: 1 addition & 1 deletion stories/Provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import translations from '@zap/i18n/translation'
import { configureStore } from '@zap/renderer/store/configureStore'
import { getDefaultLocale } from '@zap/i18n'
import { getDb } from '@zap/renderer/store/db'
import sha256digest from '@zap/utils/sha256'
import { sha256digest } from '@zap/utils/sha256'
import getDbName from '@zap/utils/db'
import { setTheme } from 'reducers/theme'
import { setCryptoUnit } from 'reducers/ticker'
Expand Down
12 changes: 9 additions & 3 deletions test/unit/utils/sha256.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import sha256digest from '@zap/utils/sha256'
import { sha256digest } from '@zap/utils/sha256'

describe('sha256digest', () => {
it('should produce correct sha256 hashes of a string', () => {
expect(sha256digest('chancellor on the brink of second bailout for banks')).toEqual(
it('should produce correct sha256 hashes of a string (bytes)', () => {
expect(sha256digest('chancellor on the brink of second bailout for banks', 'hex')).toEqual(
'9fed463e7c26c0a742b137234e9bbea5fd7beacfa959ca5d54a57e7f41fa2255'
)
})

it('should produce correct sha256 hashes of a string (hex)', () => {
expect(sha256digest('chancellor on the brink of second bailout for banks')).toEqual(
Buffer.from('9fed463e7c26c0a742b137234e9bbea5fd7beacfa959ca5d54a57e7f41fa2255', 'hex')
)
})
})
10 changes: 10 additions & 0 deletions utils/crypto.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { randomBytes } from 'crypto'
import get from 'lodash/get'
import config from 'config'
import range from 'lodash/range'
Expand All @@ -7,6 +8,8 @@ import coininfo from 'coininfo'
import { CoinBig } from '@zap/utils/coin'
import { convert } from '@zap/utils/btc'

export const PREIMAGE_BYTE_LENGTH = 32

export const networks = {
bitcoin: {
mainnet: coininfo.bitcoin.main.toBitcoinJS(),
Expand Down Expand Up @@ -285,3 +288,10 @@ export const getFeeRange = (routes = []) => ({
min: getMinFee(routes),
max: getMaxFee(routes),
})

/**
* generateHash - Generates 32 random bytes suitible for use as a preimage.
*
* @returns {Uint8Array} hash bytes
*/
export const generatePreimage = () => randomBytes(PREIMAGE_BYTE_LENGTH)
16 changes: 8 additions & 8 deletions utils/sha256.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import crypto from 'crypto'
import { createHash } from 'crypto'

/**
* sha256digest - Generates a digest of the `message`
* sha256digest - Generates a digest of the `message`(hex)
*
* @param {string} message message to hash
* @returns {string} sha256 hex hash of a `message`
* @param {string} encoding Endoding to output
* @returns {string|Buffer} sha256 hash of a message`. If encoding is provided a string will be returned;
* otherwise a Buffer is returned.
*/
export default function sha256digest(message) {
return crypto
.createHash('sha256')
export const sha256digest = (message, encoding) =>
createHash('sha256')
.update(message)
.digest('hex')
}
.digest(encoding)

0 comments on commit dc9c736

Please sign in to comment.