Moved some cryptographic operations in the shared codebase

This commit is contained in:
Kirill Fedoseev 2019-10-08 20:45:28 +03:00
parent 1f61aa65ba
commit cded4b2696
14 changed files with 55 additions and 116 deletions

View File

@ -9,6 +9,6 @@ COPY ./bncWatcher/package.json /watcher/
RUN npm install
COPY ./bncWatcher/bncWatcher.js ./shared/db.js ./shared/logger.js /watcher/
COPY ./bncWatcher/bncWatcher.js ./shared/db.js ./shared/logger.js ./shared/crypto.js /watcher/
ENTRYPOINT ["node", "bncWatcher.js"]

View File

@ -1,12 +1,11 @@
const redis = require('./db')
const axios = require('axios')
const bech32 = require('bech32')
const BN = require('bignumber.js')
const fs = require('fs')
const crypto = require('crypto')
const { computeAddress } = require('ethers').utils
const logger = require('./logger')
const redis = require('./db')
const { publicKeyToAddress } = require('./crypto')
const { FOREIGN_URL, PROXY_URL, FOREIGN_ASSET } = process.env
@ -89,20 +88,6 @@ function getLastForeignAddress () {
return publicKeyToAddress(publicKey)
}
function publicKeyToAddress ({ x, y }) {
const compact = (parseInt(y[y.length - 1], 16) % 2 ? '03' : '02') + padZeros(x, 64)
const sha256Hash = crypto.createHash('sha256').update(Buffer.from(compact, 'hex')).digest('hex')
const hash = crypto.createHash('ripemd160').update(Buffer.from(sha256Hash, 'hex')).digest('hex')
const words = bech32.toWords(Buffer.from(hash, 'hex'))
return bech32.encode('tbnb', words)
}
function padZeros (s, len) {
while (s.length < len)
s = '0' + s
return s
}
initialize().then(async () => {
while (true) {
await main()

View File

@ -9,6 +9,6 @@ COPY ./ethWatcher/package.json /watcher/
RUN npm install
COPY ./ethWatcher/ethWatcher.js ./shared/db.js ./shared/logger.js ./shared/amqp.js /watcher/
COPY ./ethWatcher/ethWatcher.js ./shared/db.js ./shared/logger.js ./shared/amqp.js ./shared/crypto.js /watcher/
ENTRYPOINT ["node", "ethWatcher.js"]

View File

@ -1,12 +1,11 @@
const Web3 = require('web3')
const crypto = require('crypto')
const utils = require('ethers').utils
const BN = require('bignumber.js')
const bech32 = require('bech32')
const logger = require('./logger')
const redis = require('./db')
const { connectRabbit, assertQueue } = require('./amqp')
const { publicKeyToAddress } = require('./crypto')
const abiToken = require('./contracts_data/IERC20.json').abi
const abiBridge = require('./contracts_data/Bridge.json').abi
@ -76,7 +75,7 @@ async function main () {
await sendKeygen(event)
break
case 'NewEpochCancelled':
sendKeygenCancelation(event)
sendKeygenCancellation(event)
break
case 'NewFundsTransfer':
await sendSignFundsTransfer(event)
@ -122,7 +121,7 @@ async function sendKeygen (event) {
logger.debug('Sent keygen start event')
}
function sendKeygenCancelation (event) {
function sendKeygenCancellation (event) {
const epoch = event.returnValues.epoch.toNumber()
cancelKeygenQueue.send({ epoch })
logger.debug('Sent keygen cancellation event')
@ -174,17 +173,3 @@ async function sendSign (event) {
redisTx.incr(`foreignNonce${epoch}`)
foreignNonce[epoch]++
}
function publicKeyToAddress ({ x, y }) {
const compact = (parseInt(y[y.length - 1], 16) % 2 ? '03' : '02') + padZeros(x, 64)
const sha256Hash = crypto.createHash('sha256').update(Buffer.from(compact, 'hex')).digest('hex')
const hash = crypto.createHash('ripemd160').update(Buffer.from(sha256Hash, 'hex')).digest('hex')
const words = bech32.toWords(Buffer.from(hash, 'hex'))
return bech32.encode('tbnb', words)
}
function padZeros (s, len) {
while (s.length < len)
s = '0' + s
return s
}

View File

@ -6,6 +6,6 @@ COPY ./proxy/package.json /proxy/
RUN npm install
COPY ./proxy/index.js ./proxy/encode.js ./proxy/decode.js ./shared/logger.js /proxy/
COPY ./proxy/index.js ./proxy/encode.js ./proxy/decode.js ./shared/logger.js ./shared/crypto.js /proxy/
ENTRYPOINT ["node", "index.js"]

View File

@ -224,6 +224,5 @@ module.exports = function (isKeygen, round, value) {
const tokenizer = Tokenizer(value)
const roundNumber = parseInt(round[round.length - 1])
const decoder = (isKeygen ? keygenDecoders : signDecoders)[roundNumber]
const decoded = JSON.stringify(decoder(tokenizer))
return decoded
return JSON.stringify(decoder(tokenizer))
}

View File

@ -1,10 +1,6 @@
const BN = require('bignumber.js')
function padZeros (s, len) {
while (s.length < len)
s = '0' + s
return s
}
const { padZeros } = require('./crypto')
function makeBuffer (value, length = 32, base = 16) {
return Buffer.from(padZeros(new BN(value, base).toString(16), length * 2), 'hex')

View File

@ -1,8 +1,6 @@
const express = require('express')
const Web3 = require('web3')
const AsyncLock = require('async-lock')
const crypto = require('crypto')
const bech32 = require('bech32')
const axios = require('axios')
const BN = require('bignumber.js')
const { utils } = require('ethers')
@ -10,6 +8,7 @@ const { utils } = require('ethers')
const encode = require('./encode')
const decode = require('./decode')
const logger = require('./logger')
const { publicKeyToAddress } = require('./crypto')
const {
HOME_RPC_URL, HOME_BRIDGE_ADDRESS, SIDE_RPC_URL, SIDE_SHARED_DB_ADDRESS, VALIDATOR_PRIVATE_KEY, HOME_CHAIN_ID,
@ -273,7 +272,7 @@ function parseError (message) {
return result ? result[0] : ''
}
async function sendVote(query, req, res) {
async function sendVote (query, req, res) {
try {
if (await homeSendQuery(query)) {
res.send('Voted\n')
@ -409,19 +408,3 @@ function getForeignBalances (address) {
}, {}))
.catch(err => ({}))
}
function publicKeyToAddress ({ x, y }) {
const compact = (parseInt(y[y.length - 1], 16) % 2 ? '03' : '02') + padZeros(x, 64)
const sha256Hash = crypto.createHash('sha256').update(Buffer.from(compact, 'hex')).digest('hex')
const hash = crypto.createHash('ripemd160').update(Buffer.from(sha256Hash, 'hex')).digest('hex')
const words = bech32.toWords(Buffer.from(hash, 'hex'))
return bech32.encode('tbnb', words)
}
function padZeros (s, len) {
while (s.length < len)
s = '0' + s
return s
}

View File

@ -0,0 +1,26 @@
const crypto = require('crypto')
const bech32 = require('bech32')
function publicKeyToAddress ({ x, y }) {
const compact = (parseInt(y[y.length - 1], 16) % 2 ? '03' : '02') + padZeros(x, 64)
const sha256Hash = sha256(Buffer.from(compact, 'hex'))
const hash = ripemd160(Buffer.from(sha256Hash, 'hex'))
const words = bech32.toWords(Buffer.from(hash, 'hex'))
return bech32.encode('tbnb', words)
}
function padZeros (s, len) {
while (s.length < len)
s = '0' + s
return s
}
function sha256 (bytes) {
return crypto.createHash('sha256').update(bytes).digest('hex')
}
function ripemd160 (bytes) {
return crypto.createHash('ripemd160').update(bytes).digest('hex')
}
module.exports = { publicKeyToAddress, padZeros, sha256 }

View File

@ -9,7 +9,7 @@ COPY ./tss-keygen/package.json /tss/
RUN npm install
COPY ./tss-keygen/keygen-entrypoint.sh ./tss-keygen/keygen.js ./shared/logger.js ./shared/amqp.js /tss/
COPY ./tss-keygen/keygen-entrypoint.sh ./tss-keygen/keygen.js ./shared/logger.js ./shared/amqp.js ./shared/crypto.js /tss/
COPY --from=tss /tss/target/release/gg18_keygen_client /tss/

View File

@ -1,10 +1,9 @@
const exec = require('child_process')
const fs = require('fs')
const crypto = require('crypto')
const bech32 = require('bech32')
const logger = require('./logger')
const { connectRabbit, assertQueue } = require('./amqp')
const { publicKeyToAddress } = require('./crypto')
const { RABBITMQ_URL, PROXY_URL } = process.env
@ -64,17 +63,3 @@ main()
async function confirmKeygen (keysFile) {
exec.execSync(`curl -X POST -H "Content-Type: application/json" -d @"${keysFile}" "${PROXY_URL}/confirmKeygen"`, { stdio: 'pipe' })
}
function publicKeyToAddress ({ x, y }) {
const compact = (parseInt(y[y.length - 1], 16) % 2 ? '03' : '02') + padZeros(x, 64)
const sha256Hash = crypto.createHash('sha256').update(Buffer.from(compact, 'hex')).digest('hex')
const hash = crypto.createHash('ripemd160').update(Buffer.from(sha256Hash, 'hex')).digest('hex')
const words = bech32.toWords(Buffer.from(hash, 'hex'))
return bech32.encode('tbnb', words)
}
function padZeros (s, len) {
while (s.length < len)
s = '0' + s
return s
}

View File

@ -10,7 +10,7 @@ COPY ./tss-sign/package.json /tss/
RUN npm install --no-optional
COPY ./tss-sign/sign-entrypoint.sh ./tss-sign/signer.js ./tss-sign/tx.js ./shared/logger.js ./shared/amqp.js /tss/
COPY ./tss-sign/sign-entrypoint.sh ./tss-sign/signer.js ./tss-sign/tx.js ./shared/logger.js ./shared/amqp.js ./shared/crypto.js /tss/
COPY --from=tss /tss/target/release/gg18_sign_client /tss/

View File

@ -1,12 +1,11 @@
const exec = require('child_process')
const fs = require('fs')
const crypto = require('crypto')
const bech32 = require('bech32')
const BN = require('bignumber.js')
const express = require('express')
const logger = require('./logger')
const { connectRabbit, assertQueue } = require('./amqp')
const { publicKeyToAddress, sha256 } = require('./crypto')
const app = express()
app.get('/restart/:attempt', restart)
@ -62,7 +61,7 @@ async function main () {
memo: `Attempt ${attempt}`
})
const hash = crypto.createHash('sha256').update(tx.getSignBytes()).digest('hex')
const hash = sha256(tx.getSignBytes())
logger.info(`Starting signature generation for transaction hash ${hash}`)
const done = await sign(keysFile, hash, tx, publicKey) && await waitForAccountNonce(from, nonce + 1)
@ -91,7 +90,7 @@ async function main () {
memo: `Attempt ${attempt}`
})
const hash = crypto.createHash('sha256').update(tx.getSignBytes()).digest('hex')
const hash = sha256(tx.getSignBytes())
logger.info(`Starting signature generation for transaction hash ${hash}`)
const done = await sign(keysFile, hash, tx, publicKey) && await waitForAccountNonce(from, nonce + 1)
@ -207,17 +206,3 @@ function sendTx (tx) {
}
})
}
function publicKeyToAddress ({ x, y }) {
const compact = (parseInt(y[y.length - 1], 16) % 2 ? '03' : '02') + padZeros(x, 64)
const sha256Hash = crypto.createHash('sha256').update(Buffer.from(compact, 'hex')).digest('hex')
const hash = crypto.createHash('ripemd160').update(Buffer.from(sha256Hash, 'hex')).digest('hex')
const words = bech32.toWords(Buffer.from(hash, 'hex'))
return bech32.encode('tbnb', words)
}
function padZeros (s, len) {
while (s.length < len)
s = '0' + s
return s
}

View File

@ -3,6 +3,7 @@ const { crypto } = require('@binance-chain/javascript-sdk')
const BN = require('bignumber.js')
const logger = require('./logger')
const { padZeros } = require('./crypto')
const { FOREIGN_CHAIN_ID } = process.env
@ -30,26 +31,26 @@ class Transaction {
coins.sort((a, b) => a.denom > b.denom)
const msg = {
inputs: [{
inputs: [ {
address: accCode,
coins
}],
outputs: [{
} ],
outputs: [ {
address: toAccCode,
coins
}],
} ],
msgType: 'MsgSend'
}
this.signMsg = {
inputs: [{
inputs: [ {
address: from,
coins
}],
outputs: [{
} ],
outputs: [ {
address: to,
coins
}]
} ]
}
this.tx = new TransactionBnc({
@ -75,20 +76,14 @@ class Transaction {
signature.s = n.minus(s).toString(16)
}
const publicKeyEncoded = Buffer.from('eb5ae98721' + (yLast % 2 ? '03' : '02') + padZeros(publicKey.x, 64), 'hex')
this.tx.signatures = [{
this.tx.signatures = [ {
pub_key: publicKeyEncoded,
signature: Buffer.from(padZeros(signature.r, 64) + padZeros(signature.s, 64), 'hex'),
account_number: this.tx.account_number,
sequence: this.tx.sequence,
}]
} ]
return this.tx.serialize()
}
}
function padZeros (s, len) {
while (s.length < len)
s = '0' + s
return s
}
module.exports = Transaction