mirror of
https://github.com/tornadocash/tornado-nova
synced 2024-02-02 14:53:56 +01:00
tmp-encrypt
This commit is contained in:
parent
a210e2a2cc
commit
5bdc8d7871
@ -25,6 +25,7 @@
|
||||
"circom_runtime": "^0.1.13",
|
||||
"circomlib": "git+https://github.com/tornadocash/circomlib.git#d20d53411d1bef61f38c99a8b36d5d0cc4836aa1",
|
||||
"dotenv": "^10.0.0",
|
||||
"eth-sig-util": "^3.0.1",
|
||||
"ethereum-waffle": "^3.2.0",
|
||||
"ethers": "^5.0.0",
|
||||
"ffiasm": "^0.1.3",
|
||||
|
@ -47,6 +47,8 @@ async function getProof({ inputs, outputs, tree, extAmount, fee, recipient, rela
|
||||
const outputIndex = tree.elements().length - 1
|
||||
const outputPath = tree.path(outputIndex).pathElements
|
||||
|
||||
//encrypt(encryptedPublicKey, { data }, 'x25519-xsalsa20-poly1305')
|
||||
|
||||
const extData = {
|
||||
recipient: toFixedHex(recipient, 20),
|
||||
relayer: toFixedHex(relayer, 20),
|
||||
|
@ -1,31 +0,0 @@
|
||||
const { ethers } = require('hardhat')
|
||||
const { BigNumber } = ethers
|
||||
const { randomBN, poseidonHash, toFixedHex } = require('./utils')
|
||||
|
||||
class Keypair {
|
||||
constructor(privkey = randomBN()) {
|
||||
this.privkey = privkey
|
||||
this.pubkey = poseidonHash([this.privkey])
|
||||
this.encryptionKey = 0 // todo
|
||||
}
|
||||
|
||||
toString() {
|
||||
return toFixedHex(this.pubkey) + toFixedHex(this.encryptionKey).slice(2)
|
||||
}
|
||||
|
||||
static fromString(str) {
|
||||
if (str.length === 130) {
|
||||
str = str.slice(2)
|
||||
}
|
||||
if (str.length !== 128) {
|
||||
throw new Error('Invalid key length')
|
||||
}
|
||||
return Object.assign(new Keypair(), {
|
||||
privkey: null,
|
||||
pubkey: BigNumber.from('0x' + str.slice(0, 64)),
|
||||
encryptionKey: BigNumber.from('0x' + str.slice(64, 128)),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Keypair
|
54
src/keypair.js
Normal file
54
src/keypair.js
Normal file
@ -0,0 +1,54 @@
|
||||
const { encrypt, decrypt, getEncryptionPublicKey } = require('eth-sig-util')
|
||||
const { ethers } = require('hardhat')
|
||||
const { BigNumber } = ethers
|
||||
const { randomBN, poseidonHash, toFixedHex } = require('./utils')
|
||||
|
||||
class Keypair {
|
||||
constructor(privkey = ethers.Wallet.createRandom().privateKey) {
|
||||
this.privkey = privkey
|
||||
console.log(privkey)
|
||||
this.pubkey = poseidonHash([this.privkey])
|
||||
this.encryptionKey = getEncryptionPublicKey(privkey.slice(2))
|
||||
console.log('enc key', this.encryptionKey)
|
||||
}
|
||||
|
||||
toString() {
|
||||
return toFixedHex(this.pubkey) + toFixedHex(this.encryptionKey).slice(2)
|
||||
}
|
||||
|
||||
static fromString(str) {
|
||||
if (str.length === 130) {
|
||||
str = str.slice(2)
|
||||
}
|
||||
if (str.length !== 128) {
|
||||
throw new Error('Invalid key length')
|
||||
}
|
||||
return Object.assign(new Keypair(), {
|
||||
privkey: null,
|
||||
pubkey: BigNumber.from('0x' + str.slice(0, 64)),
|
||||
encryptionKey: BigNumber.from('0x' + str.slice(64, 128)),
|
||||
})
|
||||
}
|
||||
|
||||
encrypt({ blinding, amount }) {
|
||||
console.log(BigNumber.from(blinding).toHexString())
|
||||
const bytes = Buffer.concat([
|
||||
Buffer.from(BigNumber.from(blinding).toHexString(), 0, 31),
|
||||
Buffer.from(BigNumber.from(amount).toHexString(), 0, 31),
|
||||
])
|
||||
console.log(bytes)
|
||||
return encrypt(this.encryptionKey, { data: bytes.toString('base64') }, 'x25519-xsalsa20-poly1305')
|
||||
}
|
||||
|
||||
decrypt(data) {
|
||||
const decryptedMessage = decrypt(data, this.privkey.slice(2))
|
||||
const buf = Buffer.from(decryptedMessage, 'base64')
|
||||
console.log(buf)
|
||||
return {
|
||||
blinding: BigNumber.from('0x' + buf.slice(0, 31).toString('hex')),
|
||||
amount: BigNumber.from('0x' + buf.slice(31, 62).toString('hex')),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Keypair
|
32
src/utils.js
32
src/utils.js
@ -55,6 +55,36 @@ async function revertSnapshot(id) {
|
||||
await ethers.provider.send('evm_revert', [id])
|
||||
}
|
||||
|
||||
function packEncryptedMessage(encryptedMessage) {
|
||||
const nonceBuf = Buffer.from(encryptedMessage.nonce, 'base64')
|
||||
const ephemPublicKeyBuf = Buffer.from(encryptedMessage.ephemPublicKey, 'base64')
|
||||
const ciphertextBuf = Buffer.from(encryptedMessage.ciphertext, 'base64')
|
||||
const messageBuff = Buffer.concat([
|
||||
Buffer.alloc(24 - nonceBuf.length),
|
||||
nonceBuf,
|
||||
Buffer.alloc(32 - ephemPublicKeyBuf.length),
|
||||
ephemPublicKeyBuf,
|
||||
ciphertextBuf,
|
||||
])
|
||||
return '0x' + messageBuff.toString('hex')
|
||||
}
|
||||
|
||||
function unpackEncryptedMessage(encryptedMessage) {
|
||||
if (encryptedMessage.slice(0, 2) === '0x') {
|
||||
encryptedMessage = encryptedMessage.slice(2)
|
||||
}
|
||||
const messageBuff = Buffer.from(encryptedMessage, 'hex')
|
||||
const nonceBuf = messageBuff.slice(0, 24)
|
||||
const ephemPublicKeyBuf = messageBuff.slice(24, 56)
|
||||
const ciphertextBuf = messageBuff.slice(56)
|
||||
return {
|
||||
version: 'x25519-xsalsa20-poly1305',
|
||||
nonce: nonceBuf.toString('base64'),
|
||||
ephemPublicKey: ephemPublicKeyBuf.toString('base64'),
|
||||
ciphertext: ciphertextBuf.toString('base64'),
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
FIELD_SIZE,
|
||||
randomBN,
|
||||
@ -65,4 +95,6 @@ module.exports = {
|
||||
getExtDataHash,
|
||||
takeSnapshot,
|
||||
revertSnapshot,
|
||||
packEncryptedMessage,
|
||||
unpackEncryptedMessage,
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
const { ethers } = require('hardhat')
|
||||
const { BigNumber } = ethers
|
||||
const { randomBN, poseidonHash } = require('./utils')
|
||||
const Keypair = require('./kaypair')
|
||||
const Keypair = require('./keypair')
|
||||
|
||||
class Utxo {
|
||||
constructor({ amount = 0, keypair = new Keypair(), blinding = randomBN(), index } = {}) {
|
||||
|
@ -8,6 +8,22 @@ const MERKLE_TREE_HEIGHT = 5
|
||||
const MerkleTree = require('fixed-merkle-tree')
|
||||
|
||||
const { deposit, transact, withdraw, merge } = require('../src/index')
|
||||
const Keypair = require('../src/keypair')
|
||||
|
||||
describe.only('Keypair', () => {
|
||||
it('should work', () => {
|
||||
const blinding = 3
|
||||
const amount = 5
|
||||
const keypair = new Keypair()
|
||||
|
||||
const cyphertext = keypair.encrypt({ blinding, amount})
|
||||
console.log(cyphertext)
|
||||
const result = keypair.decrypt(cyphertext)
|
||||
console.log(result, result.blinding.toString())
|
||||
expect(result.blinding).to.be.equal(blinding)
|
||||
expect(result.amount).to.be.equal(amount)
|
||||
})
|
||||
})
|
||||
|
||||
describe('TornadoPool', () => {
|
||||
let snapshotId, tornadoPool
|
||||
|
10
yarn.lock
10
yarn.lock
@ -3226,6 +3226,16 @@ eth-sig-util@^2.5.2:
|
||||
tweetnacl "^1.0.3"
|
||||
tweetnacl-util "^0.15.0"
|
||||
|
||||
eth-sig-util@^3.0.1:
|
||||
version "3.0.1"
|
||||
resolved "https://registry.yarnpkg.com/eth-sig-util/-/eth-sig-util-3.0.1.tgz#8753297c83a3f58346bd13547b59c4b2cd110c96"
|
||||
integrity sha512-0Us50HiGGvZgjtWTyAI/+qTzYPMLy5Q451D0Xy68bxq1QMWdoOddDwGvsqcFT27uohKgalM9z/yxplyt+mY2iQ==
|
||||
dependencies:
|
||||
ethereumjs-abi "^0.6.8"
|
||||
ethereumjs-util "^5.1.1"
|
||||
tweetnacl "^1.0.3"
|
||||
tweetnacl-util "^0.15.0"
|
||||
|
||||
eth-tx-summary@^3.1.2:
|
||||
version "3.2.4"
|
||||
resolved "https://registry.yarnpkg.com/eth-tx-summary/-/eth-tx-summary-3.2.4.tgz#e10eb95eb57cdfe549bf29f97f1e4f1db679035c"
|
||||
|
Loading…
Reference in New Issue
Block a user