From 9895862c35cfc45f56bf6279465fe44d392f97a9 Mon Sep 17 00:00:00 2001
From: librelois <elois@ifee.fr>
Date: Sun, 15 Dec 2019 23:22:05 +0100
Subject: [PATCH] [fix] transactions check: auto use new verify fn when dubp
 jump to v12

---
 app/lib/dto/TransactionDTO.ts            | 15 ++++++++++-----
 app/lib/indexer.ts                       |  2 +-
 app/lib/rules/global_rules.ts            | 19 ++++++++++++-------
 app/lib/rules/local_rules.ts             |  2 +-
 app/modules/prover/lib/blockGenerator.ts |  4 ++--
 app/service/TransactionsService.ts       |  4 ++--
 6 files changed, 28 insertions(+), 18 deletions(-)

diff --git a/app/lib/dto/TransactionDTO.ts b/app/lib/dto/TransactionDTO.ts
index aee44796a..64fcd3ed9 100644
--- a/app/lib/dto/TransactionDTO.ts
+++ b/app/lib/dto/TransactionDTO.ts
@@ -13,7 +13,7 @@
 
 import {hashf} from "../common"
 import {Cloneable} from "./Cloneable"
-import {verifyBuggy} from "../common-libs/crypto/keyring"
+import {verify, verifyBuggy} from "../common-libs/crypto/keyring"
 
 export interface BaseDTO {
   base: number
@@ -237,7 +237,7 @@ export class TransactionDTO implements Cloneable {
     }
   }
 
-  getTransactionSigResult() {
+  getTransactionSigResult(dubp_version: number) {
     const sigResult = new TxSignatureResultImpl(this.issuers.slice())
     let i = 0
     const raw = this.getRawTxNoSig()
@@ -245,14 +245,19 @@ export class TransactionDTO implements Cloneable {
     while (matching && i < this.signatures.length) {
       const sig = this.signatures[i]
       const pub = this.issuers[i]
-      sigResult.sigs[i].ok = matching = verifyBuggy(raw, sig, pub)
+      if (dubp_version >= 12) {
+        sigResult.sigs[i].ok = verify(raw, sig, pub)
+      } else {
+        sigResult.sigs[i].ok = verifyBuggy(raw, sig, pub)
+      }
+      matching = sigResult.sigs[i].ok 
       i++
     }
     return sigResult
   }
 
-  checkSignatures() {
-    return this.getTransactionSigResult().allMatching
+  checkSignatures(dubp_version: number) {
+    return this.getTransactionSigResult(dubp_version).allMatching
   }
 
   static fromJSONObject(obj:any, currency:string = "") {
diff --git a/app/lib/indexer.ts b/app/lib/indexer.ts
index 8ea092799..72377e871 100644
--- a/app/lib/indexer.ts
+++ b/app/lib/indexer.ts
@@ -2160,7 +2160,7 @@ function txSourceUnlock(ENTRY:SindexEntry, source:{ conditions: string, written_
   const tx = ENTRY.txObj;
   const unlockParams:string[] = TransactionDTO.unlock2params(ENTRY.unlock || '')
   const unlocksMetadata:UnlockMetadata = {}
-  const sigResult = TransactionDTO.fromJSONObject(tx).getTransactionSigResult()
+  const sigResult = TransactionDTO.fromJSONObject(tx).getTransactionSigResult(HEAD.version)
   if (!source.conditions) {
     return false // Unlock fail
   }
diff --git a/app/lib/rules/global_rules.ts b/app/lib/rules/global_rules.ts
index 4842cd4ee..21779706a 100644
--- a/app/lib/rules/global_rules.ts
+++ b/app/lib/rules/global_rules.ts
@@ -94,7 +94,7 @@ export const GLOBAL_RULES_FUNCTIONS = {
     return true;
   },
 
-  checkSourcesAvailability: async (block:{ transactions:TransactionDTO[], medianTime: number }, conf:ConfDTO, dal:FileDAL, findSourceTx:(txHash:string) => Promise<DBTx|null>) => {
+  checkSourcesAvailability: async (block:{ version: number, transactions:TransactionDTO[], medianTime: number }, conf:ConfDTO, dal:FileDAL, findSourceTx:(txHash:string) => Promise<DBTx|null>) => {
     const txs = block.transactions
     const current = await dal.getCurrentBlockOrNull();
     for (const tx of txs) {
@@ -150,7 +150,7 @@ export const GLOBAL_RULES_FUNCTIONS = {
             unlocksMetadata.elapsedTime = block.medianTime - dbSrc.written_time;
           }
 
-          const sigs = tx.getTransactionSigResult()
+          const sigs = tx.getTransactionSigResult(block.version)
 
           try {
             if (!txunlock(dbSrc.conditions, unlocksForCondition, sigs, unlocksMetadata)) {
@@ -213,13 +213,18 @@ export const GLOBAL_RULES_HELPERS = {
 
   checkSingleTransaction: (
     tx:TransactionDTO,
-    block:{ medianTime: number },
+    dubp_version: number,
+    medianTime: number,
     conf:ConfDTO,
     dal:FileDAL,
-    findSourceTx:(txHash:string) => Promise<DBTx|null>) => GLOBAL_RULES_FUNCTIONS.checkSourcesAvailability({
-    transactions: [tx],
-    medianTime: block.medianTime
-  }, conf, dal, findSourceTx),
+    findSourceTx:(txHash:string) => Promise<DBTx|null>) => GLOBAL_RULES_FUNCTIONS.checkSourcesAvailability(
+      {
+        version: dubp_version,
+        transactions: [tx],
+        medianTime: medianTime
+      },
+      conf, dal, findSourceTx
+    ),
 
   checkTxBlockStamp: async (tx:TransactionDTO, dal:FileDAL) => {
     const number = parseInt(tx.blockstamp.split('-')[0])
diff --git a/app/lib/rules/local_rules.ts b/app/lib/rules/local_rules.ts
index eb71d674c..c943fac63 100644
--- a/app/lib/rules/local_rules.ts
+++ b/app/lib/rules/local_rules.ts
@@ -390,7 +390,7 @@ export const LOCAL_RULES_FUNCTIONS = {
     const txs = block.transactions
     // Check rule against each transaction
     for (const tx of txs) {
-      if (!tx.checkSignatures()) {
+      if (!tx.checkSignatures(block.version)) {
         throw Error('Signature from a transaction must match')
       }
     }
diff --git a/app/modules/prover/lib/blockGenerator.ts b/app/modules/prover/lib/blockGenerator.ts
index 2d7bba355..45529c410 100644
--- a/app/modules/prover/lib/blockGenerator.ts
+++ b/app/modules/prover/lib/blockGenerator.ts
@@ -144,8 +144,8 @@ export class BlockGenerator {
       const tx = TransactionDTO.fromJSONObject(obj);
       try {
         await LOCAL_RULES_HELPERS.checkBunchOfTransactions(passingTxs.concat(tx), this.conf, medianTime, options)
-        const nextBlockWithFakeTimeVariation = { medianTime: current.medianTime + 1 };
-        await GLOBAL_RULES_HELPERS.checkSingleTransaction(tx, nextBlockWithFakeTimeVariation, this.conf, this.dal, async (txHash:string) => {
+        const fakeTimeVariation = current.medianTime + 1;
+        await GLOBAL_RULES_HELPERS.checkSingleTransaction(tx, current.version, fakeTimeVariation, this.conf, this.dal, async (txHash:string) => {
           return Underscore.findWhere(passingTxs, { hash: txHash }) || null
         });
         await GLOBAL_RULES_HELPERS.checkTxBlockStamp(tx, this.dal);
diff --git a/app/service/TransactionsService.ts b/app/service/TransactionsService.ts
index 489ed660e..a343b625b 100644
--- a/app/service/TransactionsService.ts
+++ b/app/service/TransactionsService.ts
@@ -54,11 +54,11 @@ export class TransactionService extends FIFOService {
           throw constants.ERRORS.TX_ALREADY_PROCESSED;
         }
         // Start checks...
-        const nextBlockWithFakeTimeVariation = { medianTime: current.medianTime + 1 };
+        const fakeTimeVariation = current.medianTime + 1;
         const dto = TransactionDTO.fromJSONObject(tx)
         await LOCAL_RULES_HELPERS.checkSingleTransactionLocally(dto, this.conf)
         await GLOBAL_RULES_HELPERS.checkTxBlockStamp(tx, this.dal);
-        await GLOBAL_RULES_HELPERS.checkSingleTransaction(dto, nextBlockWithFakeTimeVariation, this.conf, this.dal, this.dal.getTxByHash.bind(this.dal));
+        await GLOBAL_RULES_HELPERS.checkSingleTransaction(dto, current.version, fakeTimeVariation, this.conf, this.dal, this.dal.getTxByHash.bind(this.dal));
         const server_pubkey = this.conf.pair && this.conf.pair.pub;
         if (!(await this.dal.txsDAL.sandbox.acceptNewSandBoxEntry({
             issuers: tx.issuers,
-- 
GitLab