Moved some cryptographic operations in the shared codebase
This commit is contained in:
parent
1f61aa65ba
commit
cded4b2696
|
@ -9,6 +9,6 @@ COPY ./bncWatcher/package.json /watcher/
|
||||||
|
|
||||||
RUN npm install
|
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"]
|
ENTRYPOINT ["node", "bncWatcher.js"]
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
const redis = require('./db')
|
|
||||||
const axios = require('axios')
|
const axios = require('axios')
|
||||||
const bech32 = require('bech32')
|
|
||||||
const BN = require('bignumber.js')
|
const BN = require('bignumber.js')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const crypto = require('crypto')
|
|
||||||
const { computeAddress } = require('ethers').utils
|
const { computeAddress } = require('ethers').utils
|
||||||
|
|
||||||
const logger = require('./logger')
|
const logger = require('./logger')
|
||||||
|
const redis = require('./db')
|
||||||
|
const { publicKeyToAddress } = require('./crypto')
|
||||||
|
|
||||||
const { FOREIGN_URL, PROXY_URL, FOREIGN_ASSET } = process.env
|
const { FOREIGN_URL, PROXY_URL, FOREIGN_ASSET } = process.env
|
||||||
|
|
||||||
|
@ -89,20 +88,6 @@ function getLastForeignAddress () {
|
||||||
return publicKeyToAddress(publicKey)
|
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 () => {
|
initialize().then(async () => {
|
||||||
while (true) {
|
while (true) {
|
||||||
await main()
|
await main()
|
||||||
|
|
|
@ -9,6 +9,6 @@ COPY ./ethWatcher/package.json /watcher/
|
||||||
|
|
||||||
RUN npm install
|
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"]
|
ENTRYPOINT ["node", "ethWatcher.js"]
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
const Web3 = require('web3')
|
const Web3 = require('web3')
|
||||||
const crypto = require('crypto')
|
|
||||||
const utils = require('ethers').utils
|
const utils = require('ethers').utils
|
||||||
const BN = require('bignumber.js')
|
const BN = require('bignumber.js')
|
||||||
const bech32 = require('bech32')
|
|
||||||
|
|
||||||
const logger = require('./logger')
|
const logger = require('./logger')
|
||||||
const redis = require('./db')
|
const redis = require('./db')
|
||||||
const { connectRabbit, assertQueue } = require('./amqp')
|
const { connectRabbit, assertQueue } = require('./amqp')
|
||||||
|
const { publicKeyToAddress } = require('./crypto')
|
||||||
|
|
||||||
const abiToken = require('./contracts_data/IERC20.json').abi
|
const abiToken = require('./contracts_data/IERC20.json').abi
|
||||||
const abiBridge = require('./contracts_data/Bridge.json').abi
|
const abiBridge = require('./contracts_data/Bridge.json').abi
|
||||||
|
@ -76,7 +75,7 @@ async function main () {
|
||||||
await sendKeygen(event)
|
await sendKeygen(event)
|
||||||
break
|
break
|
||||||
case 'NewEpochCancelled':
|
case 'NewEpochCancelled':
|
||||||
sendKeygenCancelation(event)
|
sendKeygenCancellation(event)
|
||||||
break
|
break
|
||||||
case 'NewFundsTransfer':
|
case 'NewFundsTransfer':
|
||||||
await sendSignFundsTransfer(event)
|
await sendSignFundsTransfer(event)
|
||||||
|
@ -122,7 +121,7 @@ async function sendKeygen (event) {
|
||||||
logger.debug('Sent keygen start event')
|
logger.debug('Sent keygen start event')
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendKeygenCancelation (event) {
|
function sendKeygenCancellation (event) {
|
||||||
const epoch = event.returnValues.epoch.toNumber()
|
const epoch = event.returnValues.epoch.toNumber()
|
||||||
cancelKeygenQueue.send({ epoch })
|
cancelKeygenQueue.send({ epoch })
|
||||||
logger.debug('Sent keygen cancellation event')
|
logger.debug('Sent keygen cancellation event')
|
||||||
|
@ -174,17 +173,3 @@ async function sendSign (event) {
|
||||||
redisTx.incr(`foreignNonce${epoch}`)
|
redisTx.incr(`foreignNonce${epoch}`)
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -6,6 +6,6 @@ COPY ./proxy/package.json /proxy/
|
||||||
|
|
||||||
RUN npm install
|
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"]
|
ENTRYPOINT ["node", "index.js"]
|
||||||
|
|
|
@ -224,6 +224,5 @@ module.exports = function (isKeygen, round, value) {
|
||||||
const tokenizer = Tokenizer(value)
|
const tokenizer = Tokenizer(value)
|
||||||
const roundNumber = parseInt(round[round.length - 1])
|
const roundNumber = parseInt(round[round.length - 1])
|
||||||
const decoder = (isKeygen ? keygenDecoders : signDecoders)[roundNumber]
|
const decoder = (isKeygen ? keygenDecoders : signDecoders)[roundNumber]
|
||||||
const decoded = JSON.stringify(decoder(tokenizer))
|
return JSON.stringify(decoder(tokenizer))
|
||||||
return decoded
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
const BN = require('bignumber.js')
|
const BN = require('bignumber.js')
|
||||||
|
|
||||||
function padZeros (s, len) {
|
const { padZeros } = require('./crypto')
|
||||||
while (s.length < len)
|
|
||||||
s = '0' + s
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeBuffer (value, length = 32, base = 16) {
|
function makeBuffer (value, length = 32, base = 16) {
|
||||||
return Buffer.from(padZeros(new BN(value, base).toString(16), length * 2), 'hex')
|
return Buffer.from(padZeros(new BN(value, base).toString(16), length * 2), 'hex')
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
const Web3 = require('web3')
|
const Web3 = require('web3')
|
||||||
const AsyncLock = require('async-lock')
|
const AsyncLock = require('async-lock')
|
||||||
const crypto = require('crypto')
|
|
||||||
const bech32 = require('bech32')
|
|
||||||
const axios = require('axios')
|
const axios = require('axios')
|
||||||
const BN = require('bignumber.js')
|
const BN = require('bignumber.js')
|
||||||
const { utils } = require('ethers')
|
const { utils } = require('ethers')
|
||||||
|
@ -10,6 +8,7 @@ const { utils } = require('ethers')
|
||||||
const encode = require('./encode')
|
const encode = require('./encode')
|
||||||
const decode = require('./decode')
|
const decode = require('./decode')
|
||||||
const logger = require('./logger')
|
const logger = require('./logger')
|
||||||
|
const { publicKeyToAddress } = require('./crypto')
|
||||||
|
|
||||||
const {
|
const {
|
||||||
HOME_RPC_URL, HOME_BRIDGE_ADDRESS, SIDE_RPC_URL, SIDE_SHARED_DB_ADDRESS, VALIDATOR_PRIVATE_KEY, HOME_CHAIN_ID,
|
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] : ''
|
return result ? result[0] : ''
|
||||||
}
|
}
|
||||||
|
|
||||||
async function sendVote(query, req, res) {
|
async function sendVote (query, req, res) {
|
||||||
try {
|
try {
|
||||||
if (await homeSendQuery(query)) {
|
if (await homeSendQuery(query)) {
|
||||||
res.send('Voted\n')
|
res.send('Voted\n')
|
||||||
|
@ -409,19 +408,3 @@ function getForeignBalances (address) {
|
||||||
}, {}))
|
}, {}))
|
||||||
.catch(err => ({}))
|
.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
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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 }
|
|
@ -9,7 +9,7 @@ COPY ./tss-keygen/package.json /tss/
|
||||||
|
|
||||||
RUN npm install
|
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/
|
COPY --from=tss /tss/target/release/gg18_keygen_client /tss/
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
const exec = require('child_process')
|
const exec = require('child_process')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const crypto = require('crypto')
|
|
||||||
const bech32 = require('bech32')
|
|
||||||
|
|
||||||
const logger = require('./logger')
|
const logger = require('./logger')
|
||||||
const { connectRabbit, assertQueue } = require('./amqp')
|
const { connectRabbit, assertQueue } = require('./amqp')
|
||||||
|
const { publicKeyToAddress } = require('./crypto')
|
||||||
|
|
||||||
const { RABBITMQ_URL, PROXY_URL } = process.env
|
const { RABBITMQ_URL, PROXY_URL } = process.env
|
||||||
|
|
||||||
|
@ -64,17 +63,3 @@ main()
|
||||||
async function confirmKeygen (keysFile) {
|
async function confirmKeygen (keysFile) {
|
||||||
exec.execSync(`curl -X POST -H "Content-Type: application/json" -d @"${keysFile}" "${PROXY_URL}/confirmKeygen"`, { stdio: 'pipe' })
|
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ COPY ./tss-sign/package.json /tss/
|
||||||
|
|
||||||
RUN npm install --no-optional
|
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/
|
COPY --from=tss /tss/target/release/gg18_sign_client /tss/
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
const exec = require('child_process')
|
const exec = require('child_process')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const crypto = require('crypto')
|
|
||||||
const bech32 = require('bech32')
|
|
||||||
const BN = require('bignumber.js')
|
const BN = require('bignumber.js')
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
|
|
||||||
const logger = require('./logger')
|
const logger = require('./logger')
|
||||||
const { connectRabbit, assertQueue } = require('./amqp')
|
const { connectRabbit, assertQueue } = require('./amqp')
|
||||||
|
const { publicKeyToAddress, sha256 } = require('./crypto')
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
app.get('/restart/:attempt', restart)
|
app.get('/restart/:attempt', restart)
|
||||||
|
@ -62,7 +61,7 @@ async function main () {
|
||||||
memo: `Attempt ${attempt}`
|
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}`)
|
logger.info(`Starting signature generation for transaction hash ${hash}`)
|
||||||
const done = await sign(keysFile, hash, tx, publicKey) && await waitForAccountNonce(from, nonce + 1)
|
const done = await sign(keysFile, hash, tx, publicKey) && await waitForAccountNonce(from, nonce + 1)
|
||||||
|
|
||||||
|
@ -91,7 +90,7 @@ async function main () {
|
||||||
memo: `Attempt ${attempt}`
|
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}`)
|
logger.info(`Starting signature generation for transaction hash ${hash}`)
|
||||||
const done = await sign(keysFile, hash, tx, publicKey) && await waitForAccountNonce(from, nonce + 1)
|
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ const { crypto } = require('@binance-chain/javascript-sdk')
|
||||||
const BN = require('bignumber.js')
|
const BN = require('bignumber.js')
|
||||||
|
|
||||||
const logger = require('./logger')
|
const logger = require('./logger')
|
||||||
|
const { padZeros } = require('./crypto')
|
||||||
|
|
||||||
const { FOREIGN_CHAIN_ID } = process.env
|
const { FOREIGN_CHAIN_ID } = process.env
|
||||||
|
|
||||||
|
@ -30,26 +31,26 @@ class Transaction {
|
||||||
coins.sort((a, b) => a.denom > b.denom)
|
coins.sort((a, b) => a.denom > b.denom)
|
||||||
|
|
||||||
const msg = {
|
const msg = {
|
||||||
inputs: [{
|
inputs: [ {
|
||||||
address: accCode,
|
address: accCode,
|
||||||
coins
|
coins
|
||||||
}],
|
} ],
|
||||||
outputs: [{
|
outputs: [ {
|
||||||
address: toAccCode,
|
address: toAccCode,
|
||||||
coins
|
coins
|
||||||
}],
|
} ],
|
||||||
msgType: 'MsgSend'
|
msgType: 'MsgSend'
|
||||||
}
|
}
|
||||||
|
|
||||||
this.signMsg = {
|
this.signMsg = {
|
||||||
inputs: [{
|
inputs: [ {
|
||||||
address: from,
|
address: from,
|
||||||
coins
|
coins
|
||||||
}],
|
} ],
|
||||||
outputs: [{
|
outputs: [ {
|
||||||
address: to,
|
address: to,
|
||||||
coins
|
coins
|
||||||
}]
|
} ]
|
||||||
}
|
}
|
||||||
|
|
||||||
this.tx = new TransactionBnc({
|
this.tx = new TransactionBnc({
|
||||||
|
@ -75,20 +76,14 @@ class Transaction {
|
||||||
signature.s = n.minus(s).toString(16)
|
signature.s = n.minus(s).toString(16)
|
||||||
}
|
}
|
||||||
const publicKeyEncoded = Buffer.from('eb5ae98721' + (yLast % 2 ? '03' : '02') + padZeros(publicKey.x, 64), 'hex')
|
const publicKeyEncoded = Buffer.from('eb5ae98721' + (yLast % 2 ? '03' : '02') + padZeros(publicKey.x, 64), 'hex')
|
||||||
this.tx.signatures = [{
|
this.tx.signatures = [ {
|
||||||
pub_key: publicKeyEncoded,
|
pub_key: publicKeyEncoded,
|
||||||
signature: Buffer.from(padZeros(signature.r, 64) + padZeros(signature.s, 64), 'hex'),
|
signature: Buffer.from(padZeros(signature.r, 64) + padZeros(signature.s, 64), 'hex'),
|
||||||
account_number: this.tx.account_number,
|
account_number: this.tx.account_number,
|
||||||
sequence: this.tx.sequence,
|
sequence: this.tx.sequence,
|
||||||
}]
|
} ]
|
||||||
return this.tx.serialize()
|
return this.tx.serialize()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function padZeros (s, len) {
|
|
||||||
while (s.length < len)
|
|
||||||
s = '0' + s
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = Transaction
|
module.exports = Transaction
|
||||||
|
|
Loading…
Reference in New Issue