feat(cli): enhance encryption ux

This commit is contained in:
Wanseob Lim
2020-06-18 16:39:33 +09:00
parent b2dae7079a
commit e42ac84052
4 changed files with 26 additions and 30 deletions

View File

@@ -103,6 +103,10 @@ export class Point {
return Point.from(result[0].toString(), result[1].toString())
}
eq(p: Point): boolean {
return p.toHex() === this.toHex()
}
static GENERATOR: Point = Point.from(
circomlib.babyJub.Generator[0].toString(),
circomlib.babyJub.Generator[1].toString(),

View File

@@ -40,6 +40,7 @@ export default class TransferEth extends App {
let amountWei: string
let confirmedWeiPerByte: string
let tx!: RawTx
let to!: Point
do {
const msgs: string[] = []
const { pubKey } = await this.ask({
@@ -49,7 +50,6 @@ export default class TransferEth extends App {
'0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789',
message: 'Send to? (babyjubjub pub key)',
})
let to!: Point
try {
to = Point.fromHex(pubKey)
} catch (err) {
@@ -101,24 +101,9 @@ export default class TransferEth extends App {
this.print(`Failed to build transaction \n${err.toString()}`)
}
} while (!tx)
const { encryptTo } = await this.ask({
type: 'select',
name: 'encryptTo',
initial: undefined,
message: 'You can add memo field to notify to the recipient',
choices: [
{
title: 'Send without memo. Recipient can lose the note',
value: undefined,
},
...tx.outflow.map((note, i) => ({
title: `To: ${note.pubKey.toHex()}`,
value: i,
})),
],
})
try {
await wallet.sendTx(tx, account, encryptTo)
await wallet.sendTx(tx, account, to)
} catch (err) {
logger.error(err)
logger.error(tx)

View File

@@ -325,16 +325,21 @@ export class ZkWallet {
async sendTx(
tx: RawTx,
account?: ZkAccount,
toMemo?: number,
encryptTo?: Point,
): Promise<Response> {
if (toMemo && !tx.outflow[toMemo]) throw Error('Invalid index')
if (
encryptTo &&
tx.outflow.find(outflow => outflow.pubKey.eq(encryptTo)) === undefined
) {
throw Error('Cannot find the recipient')
}
const from = account || this.account
if (!from) throw Error('Account is not set')
try {
const zkTx = await this.wizard.shield({
tx,
account: from,
toMemo,
encryptTo,
})
const { verifier } = this.node
const snarkValid = await verifier.snarkVerifier.verifyTx(zkTx)

View File

@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/camelcase */
import { ZkAccount } from '@zkopru/account'
import { Field, F, EdDSA } from '@zkopru/babyjubjub'
import { Field, F, EdDSA, Point } from '@zkopru/babyjubjub'
import {
RawTx,
ZkTx,
@@ -48,13 +48,12 @@ export class ZkWizard {
async shield({
tx,
account,
toMemo,
encryptTo,
}: {
tx: RawTx
account: ZkAccount
toMemo?: number
encryptTo?: Point
}): Promise<ZkTx> {
logger.info(`shield to memo(${toMemo})`)
return new Promise<ZkTx>((resolve, reject) => {
const merkleProof: { [hash: string]: MerkleProof<Field> } = {}
const eddsa: { [hash: string]: EdDSA } = {}
@@ -75,7 +74,7 @@ export class ZkWizard {
if (isDataPrepared()) {
const zkTx = await this.buildZkTx({
tx,
toMemo,
encryptTo,
data: { merkleProof, eddsa },
})
resolve(zkTx)
@@ -88,11 +87,11 @@ export class ZkWizard {
private async buildZkTx({
tx,
toMemo,
encryptTo,
data,
}: {
tx: RawTx
toMemo?: number
encryptTo?: Point
data: {
merkleProof: { [hash: string]: MerkleProof<Field> }
eddsa: { [hash: string]: EdDSA }
@@ -242,8 +241,11 @@ export class ZkWizard {
// let { proof, publicSignals } = Utils.genProof(snarkjs.unstringifyBigInts(provingKey), witness);
// TODO handle genProof exception
let memo: Buffer | undefined
if (toMemo !== undefined) {
memo = tx.outflow[toMemo].encrypt()
if (encryptTo !== undefined) {
const noteToEncrypt = tx.outflow.find(outflow =>
outflow.pubKey.eq(encryptTo),
)
if (noteToEncrypt) memo = noteToEncrypt.encrypt()
}
const zkTx: ZkTx = new ZkTx({
inflow: tx.inflow.map((utxo, index) => {