From 6b5cfbb4b02010e133ba5bdf4d18b661353bc14b Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 13:45:34 +0200 Subject: [PATCH 01/43] signed KV --- src/komodo.h | 2 + src/komodo_curve25519.h | 1154 ++++++++++++++++++++++++++++++++++++++ src/komodo_gateway.h | 91 +-- src/komodo_kv.h | 175 ++++++ src/komodo_structs.h | 8 +- src/rpcblockchain.cpp | 8 +- src/rpcclient.cpp | 2 +- src/wallet/rpcwallet.cpp | 59 +- 8 files changed, 1394 insertions(+), 105 deletions(-) create mode 100644 src/komodo_curve25519.h create mode 100644 src/komodo_kv.h diff --git a/src/komodo.h b/src/komodo.h index f963ab819..ed90ffccd 100644 --- a/src/komodo.h +++ b/src/komodo.h @@ -36,6 +36,7 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block); #include "komodo_structs.h" #include "komodo_globals.h" #include "komodo_utils.h" +#include "komodo_curve25519.h" #include "cJSON.c" #include "komodo_bitcoind.h" @@ -44,6 +45,7 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block); #include "komodo_notary.h" int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest); +#include "komodo_kv.h" #include "komodo_gateway.h" #include "komodo_events.h" diff --git a/src/komodo_curve25519.h b/src/komodo_curve25519.h new file mode 100644 index 000000000..1b97328b2 --- /dev/null +++ b/src/komodo_curve25519.h @@ -0,0 +1,1154 @@ +/****************************************************************************** + * Copyright © 2014-2017 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#ifndef H_KOMODO25519_H +#define H_KOMODO25519_H +// derived from curve25519_donna + +#include +#include +#include + +bits320 fmul(const bits320 in2,const bits320 in); +bits320 fexpand(bits256 basepoint); +bits256 fcontract(const bits320 input); +void cmult(bits320 *resultx,bits320 *resultz,bits256 secret,const bits320 q); +bits320 crecip(const bits320 z); +bits256 curve25519(bits256 mysecret,bits256 basepoint); + +// Sum two numbers: output += in +static inline bits320 fsum(bits320 output,bits320 in) +{ + int32_t i; + for (i=0; i<5; i++) + output.ulongs[i] += in.ulongs[i]; + return(output); +} + +static inline void fdifference_backwards(uint64_t *out,const uint64_t *in) +{ + static const uint64_t two54m152 = (((uint64_t)1) << 54) - 152; // 152 is 19 << 3 + static const uint64_t two54m8 = (((uint64_t)1) << 54) - 8; + int32_t i; + out[0] = in[0] + two54m152 - out[0]; + for (i=1; i<5; i++) + out[i] = in[i] + two54m8 - out[i]; +} + +void store_limb(uint8_t *out,uint64_t in) +{ + int32_t i; + for (i=0; i<8; i++,in>>=8) + out[i] = (in & 0xff); +} + +static inline uint64_t load_limb(uint8_t *in) +{ + return + ((uint64_t)in[0]) | + (((uint64_t)in[1]) << 8) | + (((uint64_t)in[2]) << 16) | + (((uint64_t)in[3]) << 24) | + (((uint64_t)in[4]) << 32) | + (((uint64_t)in[5]) << 40) | + (((uint64_t)in[6]) << 48) | + (((uint64_t)in[7]) << 56); +} + +// Take a little-endian, 32-byte number and expand it into polynomial form +bits320 fexpand(bits256 basepoint) +{ + bits320 out; + out.ulongs[0] = load_limb(basepoint.bytes) & 0x7ffffffffffffLL; + out.ulongs[1] = (load_limb(basepoint.bytes+6) >> 3) & 0x7ffffffffffffLL; + out.ulongs[2] = (load_limb(basepoint.bytes+12) >> 6) & 0x7ffffffffffffLL; + out.ulongs[3] = (load_limb(basepoint.bytes+19) >> 1) & 0x7ffffffffffffLL; + out.ulongs[4] = (load_limb(basepoint.bytes+24) >> 12) & 0x7ffffffffffffLL; + return(out); +} + +#if __amd64__ +// donna: special gcc mode for 128-bit integers. It's implemented on 64-bit platforms only as far as I know. +typedef unsigned uint128_t __attribute__((mode(TI))); + +// Multiply a number by a scalar: output = in * scalar +static inline bits320 fscalar_product(const bits320 in,const uint64_t scalar) +{ + int32_t i; uint128_t a = 0; bits320 output; + a = ((uint128_t)in.ulongs[0]) * scalar; + output.ulongs[0] = ((uint64_t)a) & 0x7ffffffffffffLL; + for (i=1; i<5; i++) + { + a = ((uint128_t)in.ulongs[i]) * scalar + ((uint64_t) (a >> 51)); + output.ulongs[i] = ((uint64_t)a) & 0x7ffffffffffffLL; + } + output.ulongs[0] += (a >> 51) * 19; + return(output); +} + +// Multiply two numbers: output = in2 * in +// output must be distinct to both inputs. The inputs are reduced coefficient form, the output is not. +// Assumes that in[i] < 2**55 and likewise for in2. On return, output[i] < 2**52 +bits320 fmul(const bits320 in2,const bits320 in) +{ + uint128_t t[5]; uint64_t r0,r1,r2,r3,r4,s0,s1,s2,s3,s4,c; bits320 out; + r0 = in.ulongs[0], r1 = in.ulongs[1], r2 = in.ulongs[2], r3 = in.ulongs[3], r4 = in.ulongs[4]; + s0 = in2.ulongs[0], s1 = in2.ulongs[1], s2 = in2.ulongs[2], s3 = in2.ulongs[3], s4 = in2.ulongs[4]; + t[0] = ((uint128_t) r0) * s0; + t[1] = ((uint128_t) r0) * s1 + ((uint128_t) r1) * s0; + t[2] = ((uint128_t) r0) * s2 + ((uint128_t) r2) * s0 + ((uint128_t) r1) * s1; + t[3] = ((uint128_t) r0) * s3 + ((uint128_t) r3) * s0 + ((uint128_t) r1) * s2 + ((uint128_t) r2) * s1; + t[4] = ((uint128_t) r0) * s4 + ((uint128_t) r4) * s0 + ((uint128_t) r3) * s1 + ((uint128_t) r1) * s3 + ((uint128_t) r2) * s2; + r4 *= 19, r1 *= 19, r2 *= 19, r3 *= 19; + t[0] += ((uint128_t) r4) * s1 + ((uint128_t) r1) * s4 + ((uint128_t) r2) * s3 + ((uint128_t) r3) * s2; + t[1] += ((uint128_t) r4) * s2 + ((uint128_t) r2) * s4 + ((uint128_t) r3) * s3; + t[2] += ((uint128_t) r4) * s3 + ((uint128_t) r3) * s4; + t[3] += ((uint128_t) r4) * s4; + r0 = (uint64_t)t[0] & 0x7ffffffffffffLL; c = (uint64_t)(t[0] >> 51); + t[1] += c; r1 = (uint64_t)t[1] & 0x7ffffffffffffLL; c = (uint64_t)(t[1] >> 51); + t[2] += c; r2 = (uint64_t)t[2] & 0x7ffffffffffffLL; c = (uint64_t)(t[2] >> 51); + t[3] += c; r3 = (uint64_t)t[3] & 0x7ffffffffffffLL; c = (uint64_t)(t[3] >> 51); + t[4] += c; r4 = (uint64_t)t[4] & 0x7ffffffffffffLL; c = (uint64_t)(t[4] >> 51); + r0 += c * 19; c = r0 >> 51; r0 = r0 & 0x7ffffffffffffLL; + r1 += c; c = r1 >> 51; r1 = r1 & 0x7ffffffffffffLL; + r2 += c; + out.ulongs[0] = r0, out.ulongs[1] = r1, out.ulongs[2] = r2, out.ulongs[3] = r3, out.ulongs[4] = r4; + return(out); +} + +bits320 fsquare_times(const bits320 in,uint64_t count) +{ + uint128_t t[5]; uint64_t r0,r1,r2,r3,r4,c,d0,d1,d2,d4,d419; bits320 out; + r0 = in.ulongs[0], r1 = in.ulongs[1], r2 = in.ulongs[2], r3 = in.ulongs[3], r4 = in.ulongs[4]; + do + { + d0 = r0 * 2; + d1 = r1 * 2; + d2 = r2 * 2 * 19; + d419 = r4 * 19; + d4 = d419 * 2; + t[0] = ((uint128_t) r0) * r0 + ((uint128_t) d4) * r1 + (((uint128_t) d2) * (r3 )); + t[1] = ((uint128_t) d0) * r1 + ((uint128_t) d4) * r2 + (((uint128_t) r3) * (r3 * 19)); + t[2] = ((uint128_t) d0) * r2 + ((uint128_t) r1) * r1 + (((uint128_t) d4) * (r3 )); + t[3] = ((uint128_t) d0) * r3 + ((uint128_t) d1) * r2 + (((uint128_t) r4) * (d419 )); + t[4] = ((uint128_t) d0) * r4 + ((uint128_t) d1) * r3 + (((uint128_t) r2) * (r2 )); + + r0 = (uint64_t)t[0] & 0x7ffffffffffffLL; c = (uint64_t)(t[0] >> 51); + t[1] += c; r1 = (uint64_t)t[1] & 0x7ffffffffffffLL; c = (uint64_t)(t[1] >> 51); + t[2] += c; r2 = (uint64_t)t[2] & 0x7ffffffffffffLL; c = (uint64_t)(t[2] >> 51); + t[3] += c; r3 = (uint64_t)t[3] & 0x7ffffffffffffL; c = (uint64_t)(t[3] >> 51); + t[4] += c; r4 = (uint64_t)t[4] & 0x7ffffffffffffLL; c = (uint64_t)(t[4] >> 51); + r0 += c * 19; c = r0 >> 51; r0 = r0 & 0x7ffffffffffffLL; + r1 += c; c = r1 >> 51; r1 = r1 & 0x7ffffffffffffLL; + r2 += c; + } while( --count ); + out.ulongs[0] = r0, out.ulongs[1] = r1, out.ulongs[2] = r2, out.ulongs[3] = r3, out.ulongs[4] = r4; + return(out); +} + +static inline void fcontract_iter(uint128_t t[5],int32_t flag) +{ + int32_t i; uint64_t mask = 0x7ffffffffffffLL; + for (i=0; i<4; i++) + t[i+1] += t[i] >> 51, t[i] &= mask; + if ( flag != 0 ) + t[0] += 19 * (t[4] >> 51); t[4] &= mask; +} + +// donna: Take a fully reduced polynomial form number and contract it into a little-endian, 32-byte array +bits256 fcontract(const bits320 input) +{ + uint128_t t[5]; int32_t i; bits256 out; + for (i=0; i<5; i++) + t[i] = input.ulongs[i]; + fcontract_iter(t,1), fcontract_iter(t,1); + // donna: now t is between 0 and 2^255-1, properly carried. + // donna: case 1: between 0 and 2^255-20. case 2: between 2^255-19 and 2^255-1. + t[0] += 19, fcontract_iter(t,1); + // now between 19 and 2^255-1 in both cases, and offset by 19. + t[0] += 0x8000000000000 - 19; + for (i=1; i<5; i++) + t[i] += 0x8000000000000 - 1; + // now between 2^255 and 2^256-20, and offset by 2^255. + fcontract_iter(t,0); + store_limb(out.bytes,t[0] | (t[1] << 51)); + store_limb(out.bytes+8,(t[1] >> 13) | (t[2] << 38)); + store_limb(out.bytes+16,(t[2] >> 26) | (t[3] << 25)); + store_limb(out.bytes+24,(t[3] >> 39) | (t[4] << 12)); + return(out); +} + +bits256 curve25519(bits256 mysecret,bits256 basepoint) +{ + bits320 bp,x,z; + mysecret.bytes[0] &= 0xf8, mysecret.bytes[31] &= 0x7f, mysecret.bytes[31] |= 0x40; + bp = fexpand(basepoint); + cmult(&x,&z,mysecret,bp); + return(fcontract(fmul(x,crecip(z)))); +} + +#else +// from curve25519-donna.c +typedef uint8_t u8; +typedef int32_t s32; +typedef int64_t limb; + +/* Multiply a number by a scalar: output = in * scalar */ +static void fscalar_product32(limb *output, const limb *in, const limb scalar) { + unsigned i; + for (i = 0; i < 10; ++i) { + output[i] = in[i] * scalar; + } +} + +/* Multiply two numbers: output = in2 * in + * + * output must be distinct to both inputs. The inputs are reduced coefficient + * form, the output is not. + * + * output[x] <= 14 * the largest product of the input limbs. + static void fproduct(limb *output, const limb *in2, const limb *in) { + output[0] = ((limb) ((s32) in2[0])) * ((s32) in[0]); + output[1] = ((limb) ((s32) in2[0])) * ((s32) in[1]) + + ((limb) ((s32) in2[1])) * ((s32) in[0]); + output[2] = 2 * ((limb) ((s32) in2[1])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[2]) + + ((limb) ((s32) in2[2])) * ((s32) in[0]); + output[3] = ((limb) ((s32) in2[1])) * ((s32) in[2]) + + ((limb) ((s32) in2[2])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[3]) + + ((limb) ((s32) in2[3])) * ((s32) in[0]); + output[4] = ((limb) ((s32) in2[2])) * ((s32) in[2]) + + 2 * (((limb) ((s32) in2[1])) * ((s32) in[3]) + + ((limb) ((s32) in2[3])) * ((s32) in[1])) + + ((limb) ((s32) in2[0])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[0]); + output[5] = ((limb) ((s32) in2[2])) * ((s32) in[3]) + + ((limb) ((s32) in2[3])) * ((s32) in[2]) + + ((limb) ((s32) in2[1])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[0]); + output[6] = 2 * (((limb) ((s32) in2[3])) * ((s32) in[3]) + + ((limb) ((s32) in2[1])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[1])) + + ((limb) ((s32) in2[2])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[2]) + + ((limb) ((s32) in2[0])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[0]); + output[7] = ((limb) ((s32) in2[3])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[3]) + + ((limb) ((s32) in2[2])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[2]) + + ((limb) ((s32) in2[1])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[0]); + output[8] = ((limb) ((s32) in2[4])) * ((s32) in[4]) + + 2 * (((limb) ((s32) in2[3])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[3]) + + ((limb) ((s32) in2[1])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[1])) + + ((limb) ((s32) in2[2])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[2]) + + ((limb) ((s32) in2[0])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[0]); + output[9] = ((limb) ((s32) in2[4])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[4]) + + ((limb) ((s32) in2[3])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[3]) + + ((limb) ((s32) in2[2])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[2]) + + ((limb) ((s32) in2[1])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[0]); + output[10] = 2 * (((limb) ((s32) in2[5])) * ((s32) in[5]) + + ((limb) ((s32) in2[3])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[3]) + + ((limb) ((s32) in2[1])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[1])) + + ((limb) ((s32) in2[4])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[4]) + + ((limb) ((s32) in2[2])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[2]); + output[11] = ((limb) ((s32) in2[5])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[5]) + + ((limb) ((s32) in2[4])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[4]) + + ((limb) ((s32) in2[3])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[3]) + + ((limb) ((s32) in2[2])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[2]); + output[12] = ((limb) ((s32) in2[6])) * ((s32) in[6]) + + 2 * (((limb) ((s32) in2[5])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[5]) + + ((limb) ((s32) in2[3])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[3])) + + ((limb) ((s32) in2[4])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[4]); + output[13] = ((limb) ((s32) in2[6])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[6]) + + ((limb) ((s32) in2[5])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[5]) + + ((limb) ((s32) in2[4])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[4]); + output[14] = 2 * (((limb) ((s32) in2[7])) * ((s32) in[7]) + + ((limb) ((s32) in2[5])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[5])) + + ((limb) ((s32) in2[6])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[6]); + output[15] = ((limb) ((s32) in2[7])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[7]) + + ((limb) ((s32) in2[6])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[6]); + output[16] = ((limb) ((s32) in2[8])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in2[7])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[7])); + output[17] = ((limb) ((s32) in2[8])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[8]); + output[18] = 2 * ((limb) ((s32) in2[9])) * ((s32) in[9]); + }*/ + +/* Reduce a long form to a short form by taking the input mod 2^255 - 19. + * + * On entry: |output[i]| < 14*2^54 + * On exit: |output[0..8]| < 280*2^54 */ +static void freduce_degree(limb *output) { + /* Each of these shifts and adds ends up multiplying the value by 19. + * + * For output[0..8], the absolute entry value is < 14*2^54 and we add, at + * most, 19*14*2^54 thus, on exit, |output[0..8]| < 280*2^54. */ + output[8] += output[18] << 4; + output[8] += output[18] << 1; + output[8] += output[18]; + output[7] += output[17] << 4; + output[7] += output[17] << 1; + output[7] += output[17]; + output[6] += output[16] << 4; + output[6] += output[16] << 1; + output[6] += output[16]; + output[5] += output[15] << 4; + output[5] += output[15] << 1; + output[5] += output[15]; + output[4] += output[14] << 4; + output[4] += output[14] << 1; + output[4] += output[14]; + output[3] += output[13] << 4; + output[3] += output[13] << 1; + output[3] += output[13]; + output[2] += output[12] << 4; + output[2] += output[12] << 1; + output[2] += output[12]; + output[1] += output[11] << 4; + output[1] += output[11] << 1; + output[1] += output[11]; + output[0] += output[10] << 4; + output[0] += output[10] << 1; + output[0] += output[10]; +} + +#if (-1 & 3) != 3 +#error "This code only works on a two's complement system" +#endif + +/* return v / 2^26, using only shifts and adds. + * + * On entry: v can take any value. */ +static inline limb +div_by_2_26(const limb v) +{ + /* High word of v; no shift needed. */ + const uint32_t highword = (uint32_t) (((uint64_t) v) >> 32); + /* Set to all 1s if v was negative; else set to 0s. */ + const int32_t sign = ((int32_t) highword) >> 31; + /* Set to 0x3ffffff if v was negative; else set to 0. */ + const int32_t roundoff = ((uint32_t) sign) >> 6; + /* Should return v / (1<<26) */ + return (v + roundoff) >> 26; +} + +/* return v / (2^25), using only shifts and adds. + * + * On entry: v can take any value. */ +static inline limb +div_by_2_25(const limb v) +{ + /* High word of v; no shift needed*/ + const uint32_t highword = (uint32_t) (((uint64_t) v) >> 32); + /* Set to all 1s if v was negative; else set to 0s. */ + const int32_t sign = ((int32_t) highword) >> 31; + /* Set to 0x1ffffff if v was negative; else set to 0. */ + const int32_t roundoff = ((uint32_t) sign) >> 7; + /* Should return v / (1<<25) */ + return (v + roundoff) >> 25; +} + +/* Reduce all coefficients of the short form input so that |x| < 2^26. + * + * On entry: |output[i]| < 280*2^54 */ +static void freduce_coefficients(limb *output) { + unsigned i; + + output[10] = 0; + + for (i = 0; i < 10; i += 2) { + limb over = div_by_2_26(output[i]); + /* The entry condition (that |output[i]| < 280*2^54) means that over is, at + * most, 280*2^28 in the first iteration of this loop. This is added to the + * next limb and we can approximate the resulting bound of that limb by + * 281*2^54. */ + output[i] -= over << 26; + output[i+1] += over; + + /* For the first iteration, |output[i+1]| < 281*2^54, thus |over| < + * 281*2^29. When this is added to the next limb, the resulting bound can + * be approximated as 281*2^54. + * + * For subsequent iterations of the loop, 281*2^54 remains a conservative + * bound and no overflow occurs. */ + over = div_by_2_25(output[i+1]); + output[i+1] -= over << 25; + output[i+2] += over; + } + /* Now |output[10]| < 281*2^29 and all other coefficients are reduced. */ + output[0] += output[10] << 4; + output[0] += output[10] << 1; + output[0] += output[10]; + + output[10] = 0; + + /* Now output[1..9] are reduced, and |output[0]| < 2^26 + 19*281*2^29 + * So |over| will be no more than 2^16. */ + { + limb over = div_by_2_26(output[0]); + output[0] -= over << 26; + output[1] += over; + } + + /* Now output[0,2..9] are reduced, and |output[1]| < 2^25 + 2^16 < 2^26. The + * bound on |output[1]| is sufficient to meet our needs. */ +} + +/* A helpful wrapper around fproduct: output = in * in2. + * + * On entry: |in[i]| < 2^27 and |in2[i]| < 2^27. + * + * output must be distinct to both inputs. The output is reduced degree + * (indeed, one need only provide storage for 10 limbs) and |output[i]| < 2^26. + static void fmul32(limb *output, const limb *in, const limb *in2) + { + limb t[19]; + fproduct(t, in, in2); + //|t[i]| < 14*2^54 + freduce_degree(t); + freduce_coefficients(t); + // |t[i]| < 2^26 + memcpy(output, t, sizeof(limb) * 10); + }*/ + +/* Square a number: output = in**2 + * + * output must be distinct from the input. The inputs are reduced coefficient + * form, the output is not. + * + * output[x] <= 14 * the largest product of the input limbs. */ +static void fsquare_inner(limb *output, const limb *in) { + output[0] = ((limb) ((s32) in[0])) * ((s32) in[0]); + output[1] = 2 * ((limb) ((s32) in[0])) * ((s32) in[1]); + output[2] = 2 * (((limb) ((s32) in[1])) * ((s32) in[1]) + + ((limb) ((s32) in[0])) * ((s32) in[2])); + output[3] = 2 * (((limb) ((s32) in[1])) * ((s32) in[2]) + + ((limb) ((s32) in[0])) * ((s32) in[3])); + output[4] = ((limb) ((s32) in[2])) * ((s32) in[2]) + + 4 * ((limb) ((s32) in[1])) * ((s32) in[3]) + + 2 * ((limb) ((s32) in[0])) * ((s32) in[4]); + output[5] = 2 * (((limb) ((s32) in[2])) * ((s32) in[3]) + + ((limb) ((s32) in[1])) * ((s32) in[4]) + + ((limb) ((s32) in[0])) * ((s32) in[5])); + output[6] = 2 * (((limb) ((s32) in[3])) * ((s32) in[3]) + + ((limb) ((s32) in[2])) * ((s32) in[4]) + + ((limb) ((s32) in[0])) * ((s32) in[6]) + + 2 * ((limb) ((s32) in[1])) * ((s32) in[5])); + output[7] = 2 * (((limb) ((s32) in[3])) * ((s32) in[4]) + + ((limb) ((s32) in[2])) * ((s32) in[5]) + + ((limb) ((s32) in[1])) * ((s32) in[6]) + + ((limb) ((s32) in[0])) * ((s32) in[7])); + output[8] = ((limb) ((s32) in[4])) * ((s32) in[4]) + + 2 * (((limb) ((s32) in[2])) * ((s32) in[6]) + + ((limb) ((s32) in[0])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in[1])) * ((s32) in[7]) + + ((limb) ((s32) in[3])) * ((s32) in[5]))); + output[9] = 2 * (((limb) ((s32) in[4])) * ((s32) in[5]) + + ((limb) ((s32) in[3])) * ((s32) in[6]) + + ((limb) ((s32) in[2])) * ((s32) in[7]) + + ((limb) ((s32) in[1])) * ((s32) in[8]) + + ((limb) ((s32) in[0])) * ((s32) in[9])); + output[10] = 2 * (((limb) ((s32) in[5])) * ((s32) in[5]) + + ((limb) ((s32) in[4])) * ((s32) in[6]) + + ((limb) ((s32) in[2])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in[3])) * ((s32) in[7]) + + ((limb) ((s32) in[1])) * ((s32) in[9]))); + output[11] = 2 * (((limb) ((s32) in[5])) * ((s32) in[6]) + + ((limb) ((s32) in[4])) * ((s32) in[7]) + + ((limb) ((s32) in[3])) * ((s32) in[8]) + + ((limb) ((s32) in[2])) * ((s32) in[9])); + output[12] = ((limb) ((s32) in[6])) * ((s32) in[6]) + + 2 * (((limb) ((s32) in[4])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in[5])) * ((s32) in[7]) + + ((limb) ((s32) in[3])) * ((s32) in[9]))); + output[13] = 2 * (((limb) ((s32) in[6])) * ((s32) in[7]) + + ((limb) ((s32) in[5])) * ((s32) in[8]) + + ((limb) ((s32) in[4])) * ((s32) in[9])); + output[14] = 2 * (((limb) ((s32) in[7])) * ((s32) in[7]) + + ((limb) ((s32) in[6])) * ((s32) in[8]) + + 2 * ((limb) ((s32) in[5])) * ((s32) in[9])); + output[15] = 2 * (((limb) ((s32) in[7])) * ((s32) in[8]) + + ((limb) ((s32) in[6])) * ((s32) in[9])); + output[16] = ((limb) ((s32) in[8])) * ((s32) in[8]) + + 4 * ((limb) ((s32) in[7])) * ((s32) in[9]); + output[17] = 2 * ((limb) ((s32) in[8])) * ((s32) in[9]); + output[18] = 2 * ((limb) ((s32) in[9])) * ((s32) in[9]); +} + +/* fsquare sets output = in^2. + * + * On entry: The |in| argument is in reduced coefficients form and |in[i]| < + * 2^27. + * + * On exit: The |output| argument is in reduced coefficients form (indeed, one + * need only provide storage for 10 limbs) and |out[i]| < 2^26. */ +static void +fsquare32(limb *output, const limb *in) { + limb t[19]; + fsquare_inner(t, in); + /* |t[i]| < 14*2^54 because the largest product of two limbs will be < + * 2^(27+27) and fsquare_inner adds together, at most, 14 of those + * products. */ + freduce_degree(t); + freduce_coefficients(t); + /* |t[i]| < 2^26 */ + memcpy(output, t, sizeof(limb) * 10); +} + +#if (-32 >> 1) != -16 +#error "This code only works when >> does sign-extension on negative numbers" +#endif + +/* s32_eq returns 0xffffffff iff a == b and zero otherwise. */ +static s32 s32_eq(s32 a, s32 b) { + a = ~(a ^ b); + a &= a << 16; + a &= a << 8; + a &= a << 4; + a &= a << 2; + a &= a << 1; + return a >> 31; +} + +/* s32_gte returns 0xffffffff if a >= b and zero otherwise, where a and b are + * both non-negative. */ +static s32 s32_gte(s32 a, s32 b) { + a -= b; + /* a >= 0 iff a >= b. */ + return ~(a >> 31); +} + +/* Take a fully reduced polynomial form number and contract it into a + * little-endian, 32-byte array. + * + * On entry: |input_limbs[i]| < 2^26 */ +static void fcontract32(u8 *output, limb *input_limbs) +{ + int i; + int j; + s32 input[10]; + s32 mask; + + /* |input_limbs[i]| < 2^26, so it's valid to convert to an s32. */ + for (i = 0; i < 10; i++) + input[i] = (s32)input_limbs[i]; + + for (j = 0; j < 2; ++j) { + for (i = 0; i < 9; ++i) { + if ((i & 1) == 1) { + /* This calculation is a time-invariant way to make input[i] + * non-negative by borrowing from the next-larger limb. */ + const s32 mask = input[i] >> 31; + const s32 carry = -((input[i] & mask) >> 25); + input[i] = input[i] + (carry << 25); + input[i+1] = input[i+1] - carry; + } else { + const s32 mask = input[i] >> 31; + const s32 carry = -((input[i] & mask) >> 26); + input[i] = input[i] + (carry << 26); + input[i+1] = input[i+1] - carry; + } + } + + /* There's no greater limb for input[9] to borrow from, but we can multiply + * by 19 and borrow from input[0], which is valid mod 2^255-19. */ + { + const s32 mask = input[9] >> 31; + const s32 carry = -((input[9] & mask) >> 25); + input[9] = input[9] + (carry << 25); + input[0] = input[0] - (carry * 19); + } + + /* After the first iteration, input[1..9] are non-negative and fit within + * 25 or 26 bits, depending on position. However, input[0] may be + * negative. */ + } + + /* The first borrow-propagation pass above ended with every limb + except (possibly) input[0] non-negative. + + If input[0] was negative after the first pass, then it was because of a + carry from input[9]. On entry, input[9] < 2^26 so the carry was, at most, + one, since (2**26-1) >> 25 = 1. Thus input[0] >= -19. + + In the second pass, each limb is decreased by at most one. Thus the second + borrow-propagation pass could only have wrapped around to decrease + input[0] again if the first pass left input[0] negative *and* input[1] + through input[9] were all zero. In that case, input[1] is now 2^25 - 1, + and this last borrow-propagation step will leave input[1] non-negative. */ + { + const s32 mask = input[0] >> 31; + const s32 carry = -((input[0] & mask) >> 26); + input[0] = input[0] + (carry << 26); + input[1] = input[1] - carry; + } + + /* All input[i] are now non-negative. However, there might be values between + * 2^25 and 2^26 in a limb which is, nominally, 25 bits wide. */ + for (j = 0; j < 2; j++) { + for (i = 0; i < 9; i++) { + if ((i & 1) == 1) { + const s32 carry = input[i] >> 25; + input[i] &= 0x1ffffff; + input[i+1] += carry; + } else { + const s32 carry = input[i] >> 26; + input[i] &= 0x3ffffff; + input[i+1] += carry; + } + } + + { + const s32 carry = input[9] >> 25; + input[9] &= 0x1ffffff; + input[0] += 19*carry; + } + } + + /* If the first carry-chain pass, just above, ended up with a carry from + * input[9], and that caused input[0] to be out-of-bounds, then input[0] was + * < 2^26 + 2*19, because the carry was, at most, two. + * + * If the second pass carried from input[9] again then input[0] is < 2*19 and + * the input[9] -> input[0] carry didn't push input[0] out of bounds. */ + + /* It still remains the case that input might be between 2^255-19 and 2^255. + * In this case, input[1..9] must take their maximum value and input[0] must + * be >= (2^255-19) & 0x3ffffff, which is 0x3ffffed. */ + mask = s32_gte(input[0], 0x3ffffed); + for (i = 1; i < 10; i++) { + if ((i & 1) == 1) { + mask &= s32_eq(input[i], 0x1ffffff); + } else { + mask &= s32_eq(input[i], 0x3ffffff); + } + } + + /* mask is either 0xffffffff (if input >= 2^255-19) and zero otherwise. Thus + * this conditionally subtracts 2^255-19. */ + input[0] -= mask & 0x3ffffed; + + for (i = 1; i < 10; i++) { + if ((i & 1) == 1) { + input[i] -= mask & 0x1ffffff; + } else { + input[i] -= mask & 0x3ffffff; + } + } + + input[1] <<= 2; + input[2] <<= 3; + input[3] <<= 5; + input[4] <<= 6; + input[6] <<= 1; + input[7] <<= 3; + input[8] <<= 4; + input[9] <<= 6; +#define F(i, s) \ +output[s+0] |= input[i] & 0xff; \ +output[s+1] = (input[i] >> 8) & 0xff; \ +output[s+2] = (input[i] >> 16) & 0xff; \ +output[s+3] = (input[i] >> 24) & 0xff; + output[0] = 0; + output[16] = 0; + F(0,0); + F(1,3); + F(2,6); + F(3,9); + F(4,12); + F(5,16); + F(6,19); + F(7,22); + F(8,25); + F(9,28); +#undef F +} + +bits320 bits320_limbs(limb limbs[10]) +{ + bits320 output; int32_t i; + for (i=0; i<10; i++) + output.uints[i] = limbs[i]; + return(output); +} + +static inline bits320 fscalar_product(const bits320 in,const uint64_t scalar) +{ + limb output[10],input[10]; int32_t i; + for (i=0; i<10; i++) + input[i] = in.uints[i]; + fscalar_product32(output,input,scalar); + return(bits320_limbs(output)); +} + +static inline bits320 fsquare_times(const bits320 in,uint64_t count) +{ + limb output[10],input[10]; int32_t i; + for (i=0; i<10; i++) + input[i] = in.uints[i]; + for (i=0; i Output: 2Q, Q+Q' +// x2 z2: long form && x3 z3: long form +// x z: short form, destroyed && xprime zprime: short form, destroyed +// qmqp: short form, preserved +static inline void +fmonty(bits320 *x2, bits320 *z2, // output 2Q + bits320 *x3, bits320 *z3, // output Q + Q' + bits320 *x, bits320 *z, // input Q + bits320 *xprime, bits320 *zprime, // input Q' + const bits320 qmqp) // input Q - Q' +{ + bits320 origx,origxprime,zzz,xx,zz,xxprime,zzprime; + origx = *x; + *x = fsum(*x,*z), fdifference_backwards(z->ulongs,origx.ulongs); // does x - z + origxprime = *xprime; + *xprime = fsum(*xprime,*zprime), fdifference_backwards(zprime->ulongs,origxprime.ulongs); + xxprime = fmul(*xprime,*z), zzprime = fmul(*x,*zprime); + origxprime = xxprime; + xxprime = fsum(xxprime,zzprime), fdifference_backwards(zzprime.ulongs,origxprime.ulongs); + *x3 = fsquare_times(xxprime,1), *z3 = fmul(fsquare_times(zzprime,1),qmqp); + xx = fsquare_times(*x,1), zz = fsquare_times(*z,1); + *x2 = fmul(xx,zz); + fdifference_backwards(zz.ulongs,xx.ulongs); // does zz = xx - zz + zzz = fscalar_product(zz,121665); + *z2 = fmul(zz,fsum(zzz,xx)); +} + +// ----------------------------------------------------------------------------- +// Maybe swap the contents of two limb arrays (@a and @b), each @len elements +// long. Perform the swap iff @swap is non-zero. +// This function performs the swap without leaking any side-channel information. +// ----------------------------------------------------------------------------- +static inline void swap_conditional(bits320 *a,bits320 *b,uint64_t iswap) +{ + int32_t i; const uint64_t swap = -iswap; + for (i=0; i<5; ++i) + { + const uint64_t x = swap & (a->ulongs[i] ^ b->ulongs[i]); + a->ulongs[i] ^= x, b->ulongs[i] ^= x; + } +} + +// Calculates nQ where Q is the x-coordinate of a point on the curve +// resultx/resultz: the x coordinate of the resulting curve point (short form) +// n: a little endian, 32-byte number +// q: a point of the curve (short form) +void cmult(bits320 *resultx,bits320 *resultz,bits256 secret,const bits320 q) +{ + int32_t i,j; bits320 a,b,c,d,e,f,g,h,*t; + bits320 Zero320bits,One320bits, *nqpqx = &a,*nqpqz = &b,*nqx = &c,*nqz = &d,*nqpqx2 = &e,*nqpqz2 = &f,*nqx2 = &g,*nqz2 = &h; + memset(&Zero320bits,0,sizeof(Zero320bits)); + memset(&One320bits,0,sizeof(One320bits)), One320bits.ulongs[0] = 1; + a = d = e = g = Zero320bits, b = c = f = h = One320bits; + *nqpqx = q; + for (i=0; i<32; i++) + { + uint8_t byte = secret.bytes[31 - i]; + for (j=0; j<8; j++) + { + const uint64_t bit = byte >> 7; + swap_conditional(nqx,nqpqx,bit), swap_conditional(nqz,nqpqz,bit); + fmonty(nqx2,nqz2,nqpqx2,nqpqz2,nqx,nqz,nqpqx,nqpqz,q); + swap_conditional(nqx2,nqpqx2,bit), swap_conditional(nqz2,nqpqz2,bit); + t = nqx, nqx = nqx2, nqx2 = t; + t = nqz, nqz = nqz2, nqz2 = t; + t = nqpqx, nqpqx = nqpqx2, nqpqx2 = t; + t = nqpqz, nqpqz = nqpqz2, nqpqz2 = t; + byte <<= 1; + } + } + *resultx = *nqx, *resultz = *nqz; +} + +// Shamelessly copied from donna's code that copied djb's code, changed a little +inline bits320 crecip(const bits320 z) +{ + bits320 a,t0,b,c; + /* 2 */ a = fsquare_times(z, 1); // a = 2 + /* 8 */ t0 = fsquare_times(a, 2); + /* 9 */ b = fmul(t0, z); // b = 9 + /* 11 */ a = fmul(b, a); // a = 11 + /* 22 */ t0 = fsquare_times(a, 1); + /* 2^5 - 2^0 = 31 */ b = fmul(t0, b); + /* 2^10 - 2^5 */ t0 = fsquare_times(b, 5); + /* 2^10 - 2^0 */ b = fmul(t0, b); + /* 2^20 - 2^10 */ t0 = fsquare_times(b, 10); + /* 2^20 - 2^0 */ c = fmul(t0, b); + /* 2^40 - 2^20 */ t0 = fsquare_times(c, 20); + /* 2^40 - 2^0 */ t0 = fmul(t0, c); + /* 2^50 - 2^10 */ t0 = fsquare_times(t0, 10); + /* 2^50 - 2^0 */ b = fmul(t0, b); + /* 2^100 - 2^50 */ t0 = fsquare_times(b, 50); + /* 2^100 - 2^0 */ c = fmul(t0, b); + /* 2^200 - 2^100 */ t0 = fsquare_times(c, 100); + /* 2^200 - 2^0 */ t0 = fmul(t0, c); + /* 2^250 - 2^50 */ t0 = fsquare_times(t0, 50); + /* 2^250 - 2^0 */ t0 = fmul(t0, b); + /* 2^255 - 2^5 */ t0 = fsquare_times(t0, 5); + /* 2^255 - 21 */ return(fmul(t0, a)); +} + +void OS_randombytes(unsigned char *x,long xlen); + +bits256 rand256(int32_t privkeyflag) +{ + bits256 randval; + OS_randombytes(randval.bytes,sizeof(randval)); + if ( privkeyflag != 0 ) + randval.bytes[0] &= 0xf8, randval.bytes[31] &= 0x7f, randval.bytes[31] |= 0x40; + return(randval); +} + +bits256 curve25519_basepoint9() +{ + bits256 basepoint; + memset(&basepoint,0,sizeof(basepoint)); + basepoint.bytes[0] = 9; + return(basepoint); +} + +bits256 curve25519_keypair(bits256 *pubkeyp) +{ + bits256 privkey; + privkey = rand256(1); + *pubkeyp = curve25519(privkey,curve25519_basepoint9()); + //printf("[%llx %llx] ",privkey.txid,(*pubkeyp).txid); + return(privkey); +} + +bits256 curve25519_shared(bits256 privkey,bits256 otherpub) +{ + bits256 shared,hash; + shared = curve25519(privkey,otherpub); + vcalc_sha256(0,hash.bytes,shared.bytes,sizeof(shared)); + //printf("priv.%llx pub.%llx shared.%llx -> hash.%llx\n",privkey.txid,pubkey.txid,shared.txid,hash.txid); + //hash.bytes[0] &= 0xf8, hash.bytes[31] &= 0x7f, hash.bytes[31] |= 64; + return(hash); +} + +int32_t curve25519_donna(uint8_t *mypublic,const uint8_t *secret,const uint8_t *basepoint) +{ + bits256 val,p,bp; + memcpy(p.bytes,secret,sizeof(p)); + memcpy(bp.bytes,basepoint,sizeof(bp)); + val = curve25519(p,bp); + memcpy(mypublic,val.bytes,sizeof(val)); + return(0); +} + +uint64_t conv_NXTpassword(unsigned char *mysecret,unsigned char *mypublic,uint8_t *pass,int32_t passlen) +{ + static uint8_t basepoint[32] = {9}; + uint64_t addr; uint8_t hash[32]; + if ( pass != 0 && passlen != 0 ) + vcalc_sha256(0,mysecret,pass,passlen); + mysecret[0] &= 248, mysecret[31] &= 127, mysecret[31] |= 64; + curve25519_donna(mypublic,mysecret,basepoint); + vcalc_sha256(0,hash,mypublic,32); + memcpy(&addr,hash,sizeof(addr)); + return(addr); +} + +bits256 komodo_kvprivkey(bits256 *pubkeyp,char *passphrase) +{ + bits256 privkey; + conv_NXTpassword(privkey.bytes,pubkeyp->bytes,(uint8_t *)passphrase,(int32_t)strlen(passphrase)); + return(privkey); +} + +bits256 komodo_kvsig(uint8_t *buf,int32_t len,bits256 privkey) +{ + bits256 sig,hash,otherpub; + vcalc_sha256(0,hash.bytes,buf,len); + otherpub = curve25519(hash,curve25519_basepoint9()); + sig = curve25519_shared(privkey,otherpub); + return(sig); +} + +int32_t komodo_kvsigverify(uint8_t *buf,int32_t len,bits256 pubkey,bits256 sig) +{ + bits256 hash,checksig; + vcalc_sha256(0,hash.bytes,buf,len); + checksig = curve25519_shared(hash,pubkey); + if ( memcmp(&checksig,&sig,sizeof(sig)) != 0 ) + return(-1); + else return(0); +} + +/* +#include + +bits256 GENESIS_PUBKEY,GENESIS_PRIVKEY; + +bits256 acct777_pubkey(bits256 privkey) +{ + static uint8_t basepoint[32] = {9}; + bits256 pubkey; + privkey.bytes[0] &= 248, privkey.bytes[31] &= 127, privkey.bytes[31] |= 64; + curve25519_donna(pubkey.bytes,privkey.bytes,basepoint); + return(pubkey); +} + +uint64_t acct777_nxt64bits(bits256 pubkey) +{ + bits256 acct; + vcalc_sha256(0,acct.bytes,pubkey.bytes,sizeof(pubkey)); + return(acct.txid); +} + +bits256 acct777_msgprivkey(uint8_t *data,int32_t datalen) +{ + bits256 hash; + vcalc_sha256(0,hash.bytes,data,datalen); + return(hash); +} + +bits256 acct777_msgpubkey(uint8_t *data,int32_t datalen) +{ + return(acct777_pubkey(acct777_msgprivkey(data,datalen))); +} + +bits256 acct777_hashiter(bits256 privkey,bits256 pubkey,int32_t lockdays,uint8_t chainlen) +{ + uint16_t lockseed,signlen = 0; uint8_t signbuf[16]; bits256 shared,lockhash; + lockseed = (chainlen & 0x7f) | (lockdays << 7); + signlen = 0, signbuf[signlen++] = lockseed & 0xff, signbuf[signlen++] = (lockseed >> 8) & 0xff; + + privkey.bytes[0] &= 248, privkey.bytes[31] &= 127, privkey.bytes[31] |= 64; + shared = curve25519(privkey,pubkey); + vcalc_sha256cat(lockhash.bytes,shared.bytes,sizeof(shared),signbuf,signlen); + return(lockhash); +} + +bits256 acct777_lockhash(bits256 pubkey,int32_t lockdays,uint8_t chainlen) +{ + bits256 lockhash = GENESIS_PRIVKEY; + while ( chainlen > 0 ) + lockhash = acct777_hashiter(lockhash,pubkey,lockdays,chainlen--); + return(lockhash); +} + +bits256 acct777_invoicehash(bits256 *invoicehash,uint16_t lockdays,uint8_t chainlen) +{ + int32_t i; bits256 lockhash,privkey; + OS_randombytes(privkey.bytes,sizeof(privkey)); // both privkey and pubkey are sensitive. pubkey allows verification, privkey proves owner + lockhash = privkey; + for (i=0; isigbits.bytes), len += sizeof(bits256); + iguana_rwbignum(rwflag,&serialized[len],sizeof(bits256),sig->pubkey.bytes),len += sizeof(bits256); + iguana_rwnum(rwflag,&serialized[len],sizeof(sig->signer64bits),&sig->signer64bits),len += sizeof(sig->signer64bits); + iguana_rwnum(rwflag,&serialized[len],sizeof(sig->timestamp),&sig->timestamp),len += sizeof(sig->timestamp); + iguana_rwnum(rwflag,&serialized[len],sizeof(sig->allocsize),&sig->allocsize),len += sizeof(sig->allocsize); +} + +int32_t acct777_sigcheck(struct acct777_sig *sig) +{ +#define IGUANA_GENESIS 1453075200 +#define IGUANA_MAXPACKETSIZE (1024 * 1024 * 2) +#define TEN_YEARS (10 * 365 * 24 * 3600) + if ( sig->allocsize < sizeof(*sig) || sig->allocsize > IGUANA_MAXPACKETSIZE ) + { + //printf("acct777_sign: invalid datalen.%d hex.%08x\n",sig->allocsize,sig->allocsize); + return(-1); + } + if ( sig->timestamp < IGUANA_GENESIS || sig->timestamp > (long)IGUANA_GENESIS+TEN_YEARS ) + { + printf("acct777_sign: invalid timestamp.%u (%u %u)\n",sig->timestamp,IGUANA_GENESIS,IGUANA_GENESIS+TEN_YEARS); + return(-1); + } + return(sig->allocsize); +} + +uint64_t acct777_sign(struct acct777_sig *sig,bits256 privkey,bits256 otherpubkey,uint32_t timestamp,uint8_t *serialized,int32_t datalen) +{ + bits256 pubkey; bits256 shared; uint8_t buf[sizeof(*sig)]; + pubkey = acct777_pubkey(privkey); + if ( memcmp(sig->pubkey.bytes,otherpubkey.bytes,sizeof(bits256)) != 0 ) + { + //char str[65],str2[65]; + //printf("set sig fields.(%s) != (%s)\n",bits256_str(str,sig->pubkey),bits256_str(str2,otherpubkey)); + sig->pubkey = pubkey; + sig->timestamp = timestamp; + sig->allocsize = (int32_t)(datalen + sizeof(*sig)); + sig->signer64bits = acct777_nxt64bits(sig->pubkey); + } + sig->sigbits = shared = curve25519(privkey,otherpubkey); + if ( acct777_sigcheck(sig) < 0 ) + return(0); + memset(buf,0,sizeof(buf)); + acct777_rwsig(1,buf,sig); + //int32_t i; for (i=0; iallocsize,(long long)sig->signer64bits,sig->timestamp); + //char str[65]; printf("shared.(%s) crc.%u datalen.%d\n",bits256_str(str,shared),calc_crc32(0,buf,sizeof(buf)),datalen); + vcalc_sha256cat(sig->sigbits.bytes,buf,sizeof(buf),serialized,datalen); + //printf(" calcsig.%llx pubkey.%llx signer.%llu | t%u crc.%08x len.%d shared.%llx <- %llx * %llx\n",(long long)sig->sigbits.txid,(long long)sig->pubkey.txid,(long long)sig->signer64bits,timestamp,calc_crc32(0,serialized,datalen),datalen,(long long)shared.txid,(long long)privkey.txid,(long long)otherpubkey.txid); + return(sig->signer64bits); +} + +uint64_t acct777_validate(struct acct777_sig *sig,bits256 privkey,bits256 pubkey) +{ + struct acct777_sig checksig; uint64_t signerbits; int32_t datalen; uint8_t *serialized; + datalen = (int32_t)(sig->allocsize - sizeof(*sig)); + checksig = *sig; +#if defined(_M_X64) + serialized = (uint8_t *)((unsigned char *)sig + sizeof(*sig)); +#else + serialized = (uint8_t *)((long)sig + sizeof(*sig)); +#endif + //{ int32_t i; for (i=0; itimestamp,serialized,datalen); + if ( memcmp(checksig.sigbits.bytes,sig->sigbits.bytes,sizeof(checksig.sigbits)) != 0 ) + { + //char *bits256_str(); + //char str[65],str2[65]; printf("sig compare error using sig->pub from %llu\n>>>>>>>> sig.(%s) vs (%s)",(long long)acct777_nxt64bits(sig->pubkey),bits256_str(str,checksig.sigbits),bits256_str(str2,sig->sigbits)); + return(0); + } + signerbits = acct777_nxt64bits(sig->pubkey); + if ( signerbits == checksig.signer64bits ) + return(signerbits); + else return(0); +} + +uint64_t acct777_signtx(struct acct777_sig *sig,bits256 privkey,uint32_t timestamp,uint8_t *data,int32_t datalen) +{ + return(acct777_sign(sig,privkey,acct777_msgpubkey(data,datalen),timestamp,data,datalen)); +} + +uint64_t acct777_swaptx(bits256 privkey,struct acct777_sig *sig,uint32_t timestamp,uint8_t *data,int32_t datalen) + { + uint64_t othernxt; + if ( (othernxt= acct777_validate(sig)) != sig->signer64bits ) + return(0); + return(acct777_sign(sig,privkey,acct777_msgpubkey(data,datalen),timestamp,data,datalen)); + }*/ + +int32_t _SuperNET_cipher(uint8_t nonce[crypto_box_NONCEBYTES],uint8_t *cipher,uint8_t *message,int32_t len,bits256 destpub,bits256 srcpriv,uint8_t *buf) +{ + memset(cipher,0,len+crypto_box_ZEROBYTES); + memset(buf,0,crypto_box_ZEROBYTES); + memcpy(buf+crypto_box_ZEROBYTES,message,len); + crypto_box(cipher,buf,len+crypto_box_ZEROBYTES,nonce,destpub.bytes,srcpriv.bytes); + return(len + crypto_box_ZEROBYTES); +} + +uint8_t *_SuperNET_decipher(uint8_t nonce[crypto_box_NONCEBYTES],uint8_t *cipher,uint8_t *message,int32_t len,bits256 srcpub,bits256 mypriv) +{ + int32_t err; + if ( (err= crypto_box_open(message,cipher,len,nonce,srcpub.bytes,mypriv.bytes)) == 0 ) + { + message += crypto_box_ZEROBYTES; + len -= crypto_box_ZEROBYTES; + return(message); + } + return(0); +} + +//#undef force_inline + +#endif diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 26e046a25..4ec0b0f04 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -671,52 +671,9 @@ int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above return(0); } -int32_t komodo_kvsearch(int32_t current_height,uint32_t *flagsp,int32_t *heightp,uint8_t value[IGUANA_MAXSCRIPTSIZE],uint8_t *key,int32_t keylen) -{ - struct komodo_kv *ptr; int32_t duration,retval = -1; - *heightp = -1; - *flagsp = 0; - portable_mutex_lock(&KOMODO_KV_mutex); - HASH_FIND(hh,KOMODO_KV,key,keylen,ptr); - if ( ptr != 0 ) - { - duration = ((ptr->flags >> 2) + 1) * KOMODO_KVDURATION; - //printf("duration.%d flags.%d current.%d ht.%d keylen.%d valuesize.%d\n",duration,ptr->flags,current_height,ptr->height,ptr->keylen,ptr->valuesize); - if ( current_height > (ptr->height + duration) ) - { - HASH_DELETE(hh,KOMODO_KV,ptr); - if ( ptr->value != 0 ) - free(ptr->value); - if ( ptr->key != 0 ) - free(ptr->key); - free(ptr); - } - else - { - *heightp = ptr->height; - *flagsp = ptr->flags; - if ( (retval= ptr->valuesize) != 0 ) - memcpy(value,ptr->value,retval); - } - } - portable_mutex_unlock(&KOMODO_KV_mutex); - return(retval); -} - -int32_t komodo_kvcmp(uint8_t *refvalue,uint16_t refvaluesize,uint8_t *value,uint16_t valuesize) -{ - if ( refvalue == 0 && value == 0 ) - return(0); - else if ( refvalue == 0 || value == 0 ) - return(-1); - else if ( refvaluesize != valuesize ) - return(-1); - else return(memcmp(refvalue,value,valuesize)); -} - const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout,char *source) { - uint8_t rmd160[20],rmd160s[64*20],addrtype,shortflag,pubkey33[33],valuebuf[IGUANA_MAXSCRIPTSIZE]; int32_t didstats,i,j,n,kvheight,len,tokomodo,kmdheight,otherheights[64],kmdheights[64]; int8_t baseids[64]; char base[4],coinaddr[64],destaddr[64]; uint256 txids[64]; uint16_t vouts[64]; uint64_t convtoshis,seed; int64_t fee,fiatoshis,komodoshis,checktoshis,values[64],srcvalues[64]; struct pax_transaction *pax,*pax2; struct komodo_state *basesp; double diff; uint32_t flags; + uint8_t rmd160[20],rmd160s[64*20],addrtype,shortflag,pubkey33[33]; int32_t didstats,i,j,n,kvheight,len,tokomodo,kmdheight,otherheights[64],kmdheights[64]; int8_t baseids[64]; char base[4],coinaddr[64],destaddr[64]; uint256 txids[64]; uint16_t vouts[64]; uint64_t convtoshis,seed; int64_t fee,fiatoshis,komodoshis,checktoshis,values[64],srcvalues[64]; struct pax_transaction *pax,*pax2; struct komodo_state *basesp; double diff; const char *typestr = "unknown"; if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) < 0 && opretbuf[0] != 'K' ) { @@ -734,51 +691,7 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3 tokomodo = (komodo_is_issuer() == 0); if ( opretbuf[0] == 'K' && opretlen != 40 ) { - uint16_t keylen,valuesize,newflag = 0,cmpval = 1; uint8_t *key,*valueptr; struct komodo_kv *ptr; - iguana_rwnum(0,&opretbuf[1],sizeof(keylen),&keylen); - iguana_rwnum(0,&opretbuf[3],sizeof(valuesize),&valuesize); - iguana_rwnum(0,&opretbuf[5],sizeof(kmdheight),&kmdheight); - iguana_rwnum(0,&opretbuf[9],sizeof(flags),&flags); - key = &opretbuf[13]; - valueptr = &key[keylen]; - if ( (fee= ((flags>>2)+1)*(opretlen * opretlen / keylen)) < 100000 ) - fee = 100000; - // 6a164b040005006a00000000000000746573743834393333 - //printf("fee %.8f vs %.8f flags.%d keylen.%d valuesize.%d height.%d (%02x %02x %02x) (%02x %02x %02x)\n",(double)fee/COIN,(double)value/COIN,flags,keylen,valuesize,kmdheight,key[0],key[1],key[2],valueptr[0],valueptr[1],valueptr[2]); - if ( value >= fee ) - { - if ( sizeof(flags)+sizeof(kmdheight)+sizeof(keylen)+sizeof(valuesize)+keylen+valuesize+1 == opretlen ) - { - komodo_kvsearch(height,&flags,&kvheight,valuebuf,key,keylen); // expire if needed - portable_mutex_lock(&KOMODO_KV_mutex); - HASH_FIND(hh,KOMODO_KV,key,keylen,ptr); - if ( ptr == 0 ) - { - ptr = (struct komodo_kv *)calloc(1,sizeof(*ptr)); - ptr->key = (uint8_t *)calloc(1,keylen); - ptr->keylen = keylen; - memcpy(ptr->key,key,keylen); - newflag = 1; - HASH_ADD_KEYPTR(hh,KOMODO_KV,ptr->key,ptr->keylen,ptr); - } - if ( newflag != 0 || (ptr->flags & KOMODO_KVPROTECTED) == 0 || (cmpval= komodo_kvcmp(ptr->value,ptr->valuesize,valueptr,valuesize)) == 0 ) - { - if ( cmpval != 0 ) - { - if ( ptr->value != 0 ) - free(ptr->value), ptr->value = 0; - if ( (ptr->valuesize= valuesize) != 0 ) - { - ptr->value = (uint8_t *)calloc(1,valuesize); - memcpy(ptr->value,valueptr,valuesize); - } - } - ptr->height = kmdheight; - ptr->flags = flags; - } - portable_mutex_unlock(&KOMODO_KV_mutex); - } else printf("insufficient fee %.8f vs %.8f flags.%d keylen.%d valuesize.%d height.%d (%02x %02x %02x) (%02x %02x %02x)\n",(double)fee/COIN,(double)value/COIN,flags,keylen,valuesize,kmdheight,key[0],key[1],key[2],valueptr[0],valueptr[1],valueptr[2]); - } else printf("opretlen.%d mismatch keylen.%d valuesize.%d\n",opretlen,keylen,valuesize); + komodo_kvupdate(opretbuf,opretlen,value); } else if ( opretbuf[0] == 'D' ) { diff --git a/src/komodo_kv.h b/src/komodo_kv.h new file mode 100644 index 000000000..ad8bb1ee4 --- /dev/null +++ b/src/komodo_kv.h @@ -0,0 +1,175 @@ +/****************************************************************************** + * Copyright © 2014-2017 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#ifndef H_KOMODOKV_H +#define H_KOMODOKV_H + +int32_t komodo_kvsearch(uint256 *pubkeyp,int32_t current_height,uint32_t *flagsp,int32_t *heightp,uint8_t value[IGUANA_MAXSCRIPTSIZE],uint8_t *key,int32_t keylen) +{ + struct komodo_kv *ptr; int32_t duration,retval = -1; + *heightp = -1; + *flagsp = 0; + memset(pubkeyp,0,sizeof(*pubkeyp)); + portable_mutex_lock(&KOMODO_KV_mutex); + HASH_FIND(hh,KOMODO_KV,key,keylen,ptr); + if ( ptr != 0 ) + { + duration = ((ptr->flags >> 2) + 1) * KOMODO_KVDURATION; + //printf("duration.%d flags.%d current.%d ht.%d keylen.%d valuesize.%d\n",duration,ptr->flags,current_height,ptr->height,ptr->keylen,ptr->valuesize); + if ( current_height > (ptr->height + duration) ) + { + HASH_DELETE(hh,KOMODO_KV,ptr); + if ( ptr->value != 0 ) + free(ptr->value); + if ( ptr->key != 0 ) + free(ptr->key); + free(ptr); + } + else + { + *heightp = ptr->height; + *flagsp = ptr->flags; + memcpy(pubkeyp,ptr->pubkey,sizeof(*pubkeyp)); + if ( (retval= ptr->valuesize) != 0 ) + memcpy(value,ptr->value,retval); + } + } + portable_mutex_unlock(&KOMODO_KV_mutex); + return(retval); +} + +int32_t komodo_kvcmp(uint8_t *refvalue,uint16_t refvaluesize,uint8_t *value,uint16_t valuesize) +{ + if ( refvalue == 0 && value == 0 ) + return(0); + else if ( refvalue == 0 || value == 0 ) + return(-1); + else if ( refvaluesize != valuesize ) + return(-1); + else return(memcmp(refvalue,value,valuesize)); +} + +int32_t komodo_kvnumdays(uint32_t flags) +{ + int32_t numdays; + if ( (numdays= ((flags>>2)&0x3ff)+1) > 365 ) + numdays = 365; + return(numdays); +} + +int32_t komodo_kvduration(uint32_t flags) +{ + return(komodo_kvnumdays(flags) * KOMODO_KVDURATION); +} + +uint64_t komodo_kvfee(uint32_t flags,int32_t opretlen,int32_t keylen) +{ + int32_t numdays; uint64_t fee; + numdays = komodo_kvnumdays(flags); + if ( (fee= (numdays*(opretlen * opretlen / keylen))) < 100000 ) + fee = 100000; + return(fee); +} + +void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) +{ + static bits256 zeroes; + uint32_t flags; bits256 pubkey,sig; int32_t i,transferflag,hassig,coresize,haspubkey,height,kvheight; uint16_t keylen,valuesize,newflag = 0; uint8_t *key,*valueptr,valuebuf[IGUANA_MAXSCRIPTSIZE]; struct komodo_kv *ptr; char *transferpubstr; + iguana_rwnum(0,&opretbuf[1],sizeof(keylen),&keylen); + iguana_rwnum(0,&opretbuf[3],sizeof(valuesize),&valuesize); + iguana_rwnum(0,&opretbuf[5],sizeof(height),&height); + iguana_rwnum(0,&opretbuf[9],sizeof(flags),&flags); + key = &opretbuf[13]; + valueptr = &key[keylen]; + fee = komodo_kvfee(flags,opretlen,keylen); + //printf("fee %.8f vs %.8f flags.%d keylen.%d valuesize.%d height.%d (%02x %02x %02x) (%02x %02x %02x)\n",(double)fee/COIN,(double)value/COIN,flags,keylen,valuesize,height,key[0],key[1],key[2],valueptr[0],valueptr[1],valueptr[2]); + if ( value >= fee ) + { + coresize = (int32_t)(sizeof(flags)+sizeof(height)+sizeof(keylen)+sizeof(valuesize)+keylen+valuesize+1); + if ( opretlen == coresize || opretlen == coresize+sizeof(bits256) || opretlen == coresize+2*sizeof(bits256) ) + { + memset(&pubkey,0,sizeof(pubkey)); + memset(&sig,0,sizeof(sig)); + if ( (haspubkey= (opretlen >= coresize+sizeof(bits256))) != 0 ) + { + for (i=0; i<32; i++) + ((uint8_t *)&pubkey)[i] = opretbuf[coresize+i]; + } + if ( (hassig= (opretlen == coresize+sizeof(bits256)*2)) != 0 ) + { + for (i=0; i<32; i++) + ((uint8_t *)&sig)[i] = opretbuf[coresize+sizeof(bits256)+i]; + } + if ( komodo_kvsearch(&refpubkey,height,&flags,&kvheight,valuebuf,key,keylen) >= 0 ) + { + if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) + { + if ( pubkey != refpubkey || komodo_kvsigverify(opretbuf+13,coresize-13,refpubkey,sig) < 0 ) + { + printf("komodo_kvsigverify error [%d]\n",coresize-13); + return; + } + } + } + portable_mutex_lock(&KOMODO_KV_mutex); + HASH_FIND(hh,KOMODO_KV,key,keylen,ptr); + transferflag = 0; + if ( ptr != 0 ) + { + if ( (ptr->flags & KOMODO_KVPROTECTED) != 0 && memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) + { + transferpubstr = (char *)&value[strlen((char *)"transfer:")]; + if ( strncmp((char *)"transfer:",value,strlen((char *)"transfer:")) == 0 && is_hexstr(transferpubstr,0) == 64 ) + { + transferflag = 1; + printf("transfer.(%s) to [%s]\n",key,transferpubstr); + for (i=0; i<32; i++) + ((uint8_t *)&pubkey)[31-i] = _decode_hex(&transferpubstr[i*2]); + } + } + } + else if ( ptr == 0 ) + { + ptr = (struct komodo_kv *)calloc(1,sizeof(*ptr)); + ptr->pubkey = pubkey; + ptr->key = (uint8_t *)calloc(1,keylen); + ptr->keylen = keylen; + memcpy(ptr->key,key,keylen); + newflag = 1; + HASH_ADD_KEYPTR(hh,KOMODO_KV,ptr->key,ptr->keylen,ptr); + } + if ( transferflag != 0 ) + { + ptr->pubkey = pubkey; + ptr->height = height; + } + else if ( newflag != 0 || (ptr->flags & KOMODO_KVPROTECTED) == 0 ) + { + if ( ptr->value != 0 ) + free(ptr->value), ptr->value = 0; + if ( (ptr->valuesize= valuesize) != 0 ) + { + ptr->value = (uint8_t *)calloc(1,valuesize); + memcpy(ptr->value,valueptr,valuesize); + } + ptr->height = height; + ptr->flags = flags; + } + portable_mutex_unlock(&KOMODO_KV_mutex); + } else printf("insufficient fee %.8f vs %.8f flags.%d keylen.%d valuesize.%d height.%d (%02x %02x %02x) (%02x %02x %02x)\n",(double)fee/COIN,(double)value/COIN,flags,keylen,valuesize,height,key[0],key[1],key[2],valueptr[0],valueptr[1],valueptr[2]); + } else printf("opretlen.%d mismatch keylen.%d valuesize.%d\n",opretlen,keylen,valuesize); +} + +#endif diff --git a/src/komodo_structs.h b/src/komodo_structs.h index 672516ee3..a35e7e008 100644 --- a/src/komodo_structs.h +++ b/src/komodo_structs.h @@ -41,7 +41,13 @@ #define KOMODO_KVBINARY 2 #define KOMODO_KVDURATION 1440 -struct komodo_kv { UT_hash_handle hh; uint8_t *key,*value; int32_t height; uint32_t flags; uint16_t keylen,valuesize; }; +union _bits256 { uint8_t bytes[32]; uint16_t ushorts[16]; uint32_t uints[8]; uint64_t ulongs[4]; uint64_t txid; }; +typedef union _bits256 bits256; + +union _bits320 { uint8_t bytes[40]; uint16_t ushorts[20]; uint32_t uints[10]; uint64_t ulongs[5]; uint64_t txid; }; +typedef union _bits320 bits320; + +struct komodo_kv { UT_hash_handle hh; bits256 pubkey; uint8_t *key,*value; int32_t height; uint32_t flags; uint16_t keylen,valuesize; }; struct komodo_event_notarized { uint256 blockhash,desttxid; int32_t notarizedheight; char dest[16]; }; struct komodo_event_pubkeys { uint8_t num; uint8_t pubkeys[64][33]; }; diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index 82e5d21e6..8f3e67ef9 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -408,11 +408,11 @@ int32_t komodo_notaries(uint8_t pubkeys[64][33],int32_t height); char *bitcoin_address(char *coinaddr,uint8_t addrtype,uint8_t *pubkey_or_rmd160,int32_t len); uint32_t komodo_interest_args(int32_t *txheightp,uint32_t *tiptimep,uint64_t *valuep,uint256 hash,int32_t n); int32_t komodo_minerids(uint8_t *minerids,int32_t height); -int32_t komodo_kvsearch(int32_t current_height,uint32_t *flagsp,int32_t *heightp,uint8_t value[IGUANA_MAXSCRIPTSIZE],uint8_t *key,int32_t keylen); +int32_t komodo_kvsearch(uint256 *refpubkeyp,int32_t current_height,uint32_t *flagsp,int32_t *heightp,uint8_t value[IGUANA_MAXSCRIPTSIZE],uint8_t *key,int32_t keylen); Value kvsearch(const Array& params, bool fHelp) { - Object ret; uint32_t flags; uint8_t value[IGUANA_MAXSCRIPTSIZE],key[IGUANA_MAXSCRIPTSIZE]; int32_t duration,j,height,valuesize,keylen; + Object ret; uint32_t flags; uint8_t value[IGUANA_MAXSCRIPTSIZE],key[IGUANA_MAXSCRIPTSIZE]; int32_t duration,j,height,valuesize,keylen; uint256 refpubkey; static uint256 zeroes; if (fHelp || params.size() != 1 ) throw runtime_error("kvsearch key"); LOCK(cs_main); @@ -425,12 +425,14 @@ Value kvsearch(const Array& params, bool fHelp) if ( keylen < sizeof(key) ) { memcpy(key,params[0].get_str().c_str(),keylen); - if ( (valuesize= komodo_kvsearch(chainActive.Tip()->nHeight,&flags,&height,value,key,keylen)) >= 0 ) + if ( (valuesize= komodo_kvsearch(&refpubkey,chainActive.Tip()->nHeight,&flags,&height,value,key,keylen)) >= 0 ) { std::string val; char *valuestr; val.resize(valuesize); valuestr = (char *)val.data(); memcpy(valuestr,value,valuesize); + if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) + ret.push_back(Pair("owner",refpubkey.GetHex())); ret.push_back(Pair("height",height)); duration = ((flags >> 2) + 1) * KOMODO_KVDURATION; ret.push_back(Pair("expiration", (int64_t)(height+duration))); diff --git a/src/rpcclient.cpp b/src/rpcclient.cpp index a384b063d..5b89b18c4 100644 --- a/src/rpcclient.cpp +++ b/src/rpcclient.cpp @@ -113,7 +113,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "notaries", 1 }, { "minerids", 1 }, { "kvsearch", 1 }, - { "kvupdate", 3 }, + { "kvupdate", 4 }, }; class CRPCConvertTable diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 2c04a53e0..93a168931 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -487,8 +487,12 @@ int32_t komodo_is_issuer(); int32_t iguana_rwnum(int32_t rwflag,uint8_t *serialized,int32_t len,void *endianedp); int32_t komodo_isrealtime(int32_t *kmdheightp); int32_t pax_fiatstatus(uint64_t *available,uint64_t *deposited,uint64_t *issued,uint64_t *withdrawn,uint64_t *approved,uint64_t *redeemed,char *base); -int32_t komodo_kvsearch(int32_t current_height,uint32_t *flagsp,int32_t *heightp,uint8_t value[IGUANA_MAXSCRIPTSIZE],uint8_t *key,int32_t keylen); +int32_t komodo_kvsearch(uint256 *refpubkeyp,int32_t current_height,uint32_t *flagsp,int32_t *heightp,uint8_t value[IGUANA_MAXSCRIPTSIZE],uint8_t *key,int32_t keylen); int32_t komodo_kvcmp(uint8_t *refvalue,uint16_t refvaluesize,uint8_t *value,uint16_t valuesize); +uint64_t komodo_kvfee(uint32_t flags,int32_t opretlen,int32_t keylen); +uint256 komodo_kvsig(uint8_t *buf,int32_t len,uint256 privkey); +int32_t komodo_kvduration(uint32_t flags); +uint256 komodo_kvprivkey(uint256 *pubkeyp,char *passphrase); Value paxdeposit(const Array& params, bool fHelp) { @@ -566,16 +570,27 @@ Value paxwithdraw(const Array& params, bool fHelp) Value kvupdate(const Array& params, bool fHelp) { CWalletTx wtx; Object ret; - uint8_t keyvalue[IGUANA_MAXSCRIPTSIZE],opretbuf[IGUANA_MAXSCRIPTSIZE]; int32_t duration,opretlen,height; uint16_t keylen,valuesize=0,refvaluesize=0; uint8_t *key,*value=0; uint32_t flags,tmpflags; struct komodo_kv *ptr; uint64_t fee; + uint8_t keyvalue[IGUANA_MAXSCRIPTSIZE],opretbuf[IGUANA_MAXSCRIPTSIZE]; int32_t coresize,haveprivkey,duration,opretlen,height; uint16_t keylen,valuesize=0,refvaluesize=0; uint8_t *key,*value=0; uint32_t flags,tmpflags; struct komodo_kv *ptr; uint64_t fee; uint256 privkey,pubkey,refpubkey,sig; if (fHelp || params.size() < 2 ) - throw runtime_error("kvupdate key value [flags]"); + throw runtime_error("kvupdate key value [flags] [passphrase]"); if (!EnsureWalletIsAvailable(fHelp)) return 0; - if ( params.size() == 3 ) + if ( params.size() >= 3 ) { flags = atoi(params[2].get_str().c_str()); //printf("flags.%d (%s)\n",flags,params[2].get_str().c_str()); } else flags = 0; + haveprivkey = 0; + memset(&sig,0,sizeof(sig)); + memset(&privkey,0,sizeof(privkey)); + memset(&pubkey,0,sizeof(pubkey)); + if ( params.size() >= 4 ) + { + privkey = komodo_kvprivkey(&pubkey,params[3].get_str().c_str()); + haveprivkey = 1; + flags |= 1; + //printf("flags.%d (%s)\n",flags,params[2].get_str().c_str()); + } LOCK2(cs_main, pwalletMain->cs_wallet); if ( (keylen= (int32_t)strlen(params[0].get_str().c_str())) > 0 ) { @@ -585,15 +600,25 @@ Value kvupdate(const Array& params, bool fHelp) value = (uint8_t *)params[1].get_str().c_str(); valuesize = (int32_t)strlen(params[1].get_str().c_str()); } - if ( (refvaluesize= komodo_kvsearch(chainActive.Tip()->nHeight,&tmpflags,&height,&keyvalue[keylen],key,keylen)) >= 0 && (tmpflags & KOMODO_KVPROTECTED) != 0 && komodo_kvcmp(refvaluesize==0?0:&keyvalue[keylen],refvaluesize,value,valuesize) != 0 ) + memcpy(keyvalue,key,keylen); + if ( (refvaluesize= komodo_kvsearch(&refpubkey,chainActive.Tip()->nHeight,&tmpflags,&height,&keyvalue[keylen],key,keylen)) >= 0 ) { - ret.push_back(Pair("error",(char *)"cant modify write once key")); - return ret; + if ( (tmpflags & KOMODO_KVPROTECTED) != 0 ) + { + if ( refpubkey != pubkey ) + { + ret.push_back(Pair("error",(char *)"cant modify write once key without passphrase")); + return ret; + } + sig = komodo_kvsig(keyvalue,keylen+valuesize,privkey); + } } ret.push_back(Pair("coin",(char *)(ASSETCHAINS_SYMBOL[0] == 0 ? "KMD" : ASSETCHAINS_SYMBOL))); height = chainActive.Tip()->nHeight; + if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) + ret.push_back(Pair("owner",refpubkey.GetHex())); ret.push_back(Pair("height", (int64_t)height)); - duration = ((flags >> 2) + 1) * KOMODO_KVDURATION; + duration = komodo_kvduration(flags); //((flags >> 2) + 1) * KOMODO_KVDURATION; ret.push_back(Pair("expiration", (int64_t)(height+duration))); ret.push_back(Pair("flags",(int64_t)flags)); ret.push_back(Pair("key",params[0].get_str())); @@ -610,14 +635,26 @@ Value kvupdate(const Array& params, bool fHelp) memcpy(&keyvalue[12],key,keylen); if ( value != 0 ) memcpy(&keyvalue[12 + keylen],value,valuesize); - if ( (opretlen= komodo_opreturnscript(opretbuf,'K',keyvalue,sizeof(flags)+sizeof(height)+sizeof(uint16_t)*2+keylen+valuesize)) == 40 ) + coresize = (int32_t)(sizeof(flags)+sizeof(height)+sizeof(uint16_t)*2+keylen+valuesize); + if ( haveprivkey != 0 ) + { + for (i=0; i<32; i++) + keyvalue[12 + keylen + valuesize + i] = ((uint8_t *)&pubkey)[i]; + coresize += 32; + if ( refvaluesize >=0 ) + { + for (i=0; i<32; i++) + keyvalue[12 + keylen + valuesize + 32 + i] = ((uint8_t *)&sig)[i]; + coresize += 32; + } + } + if ( (opretlen= komodo_opreturnscript(opretbuf,'K',keyvalue,coresize)) == 40 ) opretlen++; //for (i=0; i>2)+1)*(opretlen * opretlen / keylen)) < 100000 ) - fee = 100000; + fee = komodo_kvfee(flags,opretlen,keylen); ret.push_back(Pair("fee",(double)fee/COIN)); CBitcoinAddress destaddress(CRYPTO777_KMDADDR); if (!destaddress.IsValid()) From 87f82355d0cc10f645e50d8ff16f670a81f6c887 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 13:50:47 +0200 Subject: [PATCH 02/43] test --- src/komodo_curve25519.h | 182 ---------------------------------------- src/komodo_kv.h | 10 +-- src/komodo_utils.h | 3 - 3 files changed, 5 insertions(+), 190 deletions(-) diff --git a/src/komodo_curve25519.h b/src/komodo_curve25519.h index 1b97328b2..1353628a2 100644 --- a/src/komodo_curve25519.h +++ b/src/komodo_curve25519.h @@ -969,186 +969,4 @@ int32_t komodo_kvsigverify(uint8_t *buf,int32_t len,bits256 pubkey,bits256 sig) else return(0); } -/* -#include - -bits256 GENESIS_PUBKEY,GENESIS_PRIVKEY; - -bits256 acct777_pubkey(bits256 privkey) -{ - static uint8_t basepoint[32] = {9}; - bits256 pubkey; - privkey.bytes[0] &= 248, privkey.bytes[31] &= 127, privkey.bytes[31] |= 64; - curve25519_donna(pubkey.bytes,privkey.bytes,basepoint); - return(pubkey); -} - -uint64_t acct777_nxt64bits(bits256 pubkey) -{ - bits256 acct; - vcalc_sha256(0,acct.bytes,pubkey.bytes,sizeof(pubkey)); - return(acct.txid); -} - -bits256 acct777_msgprivkey(uint8_t *data,int32_t datalen) -{ - bits256 hash; - vcalc_sha256(0,hash.bytes,data,datalen); - return(hash); -} - -bits256 acct777_msgpubkey(uint8_t *data,int32_t datalen) -{ - return(acct777_pubkey(acct777_msgprivkey(data,datalen))); -} - -bits256 acct777_hashiter(bits256 privkey,bits256 pubkey,int32_t lockdays,uint8_t chainlen) -{ - uint16_t lockseed,signlen = 0; uint8_t signbuf[16]; bits256 shared,lockhash; - lockseed = (chainlen & 0x7f) | (lockdays << 7); - signlen = 0, signbuf[signlen++] = lockseed & 0xff, signbuf[signlen++] = (lockseed >> 8) & 0xff; - - privkey.bytes[0] &= 248, privkey.bytes[31] &= 127, privkey.bytes[31] |= 64; - shared = curve25519(privkey,pubkey); - vcalc_sha256cat(lockhash.bytes,shared.bytes,sizeof(shared),signbuf,signlen); - return(lockhash); -} - -bits256 acct777_lockhash(bits256 pubkey,int32_t lockdays,uint8_t chainlen) -{ - bits256 lockhash = GENESIS_PRIVKEY; - while ( chainlen > 0 ) - lockhash = acct777_hashiter(lockhash,pubkey,lockdays,chainlen--); - return(lockhash); -} - -bits256 acct777_invoicehash(bits256 *invoicehash,uint16_t lockdays,uint8_t chainlen) -{ - int32_t i; bits256 lockhash,privkey; - OS_randombytes(privkey.bytes,sizeof(privkey)); // both privkey and pubkey are sensitive. pubkey allows verification, privkey proves owner - lockhash = privkey; - for (i=0; isigbits.bytes), len += sizeof(bits256); - iguana_rwbignum(rwflag,&serialized[len],sizeof(bits256),sig->pubkey.bytes),len += sizeof(bits256); - iguana_rwnum(rwflag,&serialized[len],sizeof(sig->signer64bits),&sig->signer64bits),len += sizeof(sig->signer64bits); - iguana_rwnum(rwflag,&serialized[len],sizeof(sig->timestamp),&sig->timestamp),len += sizeof(sig->timestamp); - iguana_rwnum(rwflag,&serialized[len],sizeof(sig->allocsize),&sig->allocsize),len += sizeof(sig->allocsize); -} - -int32_t acct777_sigcheck(struct acct777_sig *sig) -{ -#define IGUANA_GENESIS 1453075200 -#define IGUANA_MAXPACKETSIZE (1024 * 1024 * 2) -#define TEN_YEARS (10 * 365 * 24 * 3600) - if ( sig->allocsize < sizeof(*sig) || sig->allocsize > IGUANA_MAXPACKETSIZE ) - { - //printf("acct777_sign: invalid datalen.%d hex.%08x\n",sig->allocsize,sig->allocsize); - return(-1); - } - if ( sig->timestamp < IGUANA_GENESIS || sig->timestamp > (long)IGUANA_GENESIS+TEN_YEARS ) - { - printf("acct777_sign: invalid timestamp.%u (%u %u)\n",sig->timestamp,IGUANA_GENESIS,IGUANA_GENESIS+TEN_YEARS); - return(-1); - } - return(sig->allocsize); -} - -uint64_t acct777_sign(struct acct777_sig *sig,bits256 privkey,bits256 otherpubkey,uint32_t timestamp,uint8_t *serialized,int32_t datalen) -{ - bits256 pubkey; bits256 shared; uint8_t buf[sizeof(*sig)]; - pubkey = acct777_pubkey(privkey); - if ( memcmp(sig->pubkey.bytes,otherpubkey.bytes,sizeof(bits256)) != 0 ) - { - //char str[65],str2[65]; - //printf("set sig fields.(%s) != (%s)\n",bits256_str(str,sig->pubkey),bits256_str(str2,otherpubkey)); - sig->pubkey = pubkey; - sig->timestamp = timestamp; - sig->allocsize = (int32_t)(datalen + sizeof(*sig)); - sig->signer64bits = acct777_nxt64bits(sig->pubkey); - } - sig->sigbits = shared = curve25519(privkey,otherpubkey); - if ( acct777_sigcheck(sig) < 0 ) - return(0); - memset(buf,0,sizeof(buf)); - acct777_rwsig(1,buf,sig); - //int32_t i; for (i=0; iallocsize,(long long)sig->signer64bits,sig->timestamp); - //char str[65]; printf("shared.(%s) crc.%u datalen.%d\n",bits256_str(str,shared),calc_crc32(0,buf,sizeof(buf)),datalen); - vcalc_sha256cat(sig->sigbits.bytes,buf,sizeof(buf),serialized,datalen); - //printf(" calcsig.%llx pubkey.%llx signer.%llu | t%u crc.%08x len.%d shared.%llx <- %llx * %llx\n",(long long)sig->sigbits.txid,(long long)sig->pubkey.txid,(long long)sig->signer64bits,timestamp,calc_crc32(0,serialized,datalen),datalen,(long long)shared.txid,(long long)privkey.txid,(long long)otherpubkey.txid); - return(sig->signer64bits); -} - -uint64_t acct777_validate(struct acct777_sig *sig,bits256 privkey,bits256 pubkey) -{ - struct acct777_sig checksig; uint64_t signerbits; int32_t datalen; uint8_t *serialized; - datalen = (int32_t)(sig->allocsize - sizeof(*sig)); - checksig = *sig; -#if defined(_M_X64) - serialized = (uint8_t *)((unsigned char *)sig + sizeof(*sig)); -#else - serialized = (uint8_t *)((long)sig + sizeof(*sig)); -#endif - //{ int32_t i; for (i=0; itimestamp,serialized,datalen); - if ( memcmp(checksig.sigbits.bytes,sig->sigbits.bytes,sizeof(checksig.sigbits)) != 0 ) - { - //char *bits256_str(); - //char str[65],str2[65]; printf("sig compare error using sig->pub from %llu\n>>>>>>>> sig.(%s) vs (%s)",(long long)acct777_nxt64bits(sig->pubkey),bits256_str(str,checksig.sigbits),bits256_str(str2,sig->sigbits)); - return(0); - } - signerbits = acct777_nxt64bits(sig->pubkey); - if ( signerbits == checksig.signer64bits ) - return(signerbits); - else return(0); -} - -uint64_t acct777_signtx(struct acct777_sig *sig,bits256 privkey,uint32_t timestamp,uint8_t *data,int32_t datalen) -{ - return(acct777_sign(sig,privkey,acct777_msgpubkey(data,datalen),timestamp,data,datalen)); -} - -uint64_t acct777_swaptx(bits256 privkey,struct acct777_sig *sig,uint32_t timestamp,uint8_t *data,int32_t datalen) - { - uint64_t othernxt; - if ( (othernxt= acct777_validate(sig)) != sig->signer64bits ) - return(0); - return(acct777_sign(sig,privkey,acct777_msgpubkey(data,datalen),timestamp,data,datalen)); - }*/ - -int32_t _SuperNET_cipher(uint8_t nonce[crypto_box_NONCEBYTES],uint8_t *cipher,uint8_t *message,int32_t len,bits256 destpub,bits256 srcpriv,uint8_t *buf) -{ - memset(cipher,0,len+crypto_box_ZEROBYTES); - memset(buf,0,crypto_box_ZEROBYTES); - memcpy(buf+crypto_box_ZEROBYTES,message,len); - crypto_box(cipher,buf,len+crypto_box_ZEROBYTES,nonce,destpub.bytes,srcpriv.bytes); - return(len + crypto_box_ZEROBYTES); -} - -uint8_t *_SuperNET_decipher(uint8_t nonce[crypto_box_NONCEBYTES],uint8_t *cipher,uint8_t *message,int32_t len,bits256 srcpub,bits256 mypriv) -{ - int32_t err; - if ( (err= crypto_box_open(message,cipher,len,nonce,srcpub.bytes,mypriv.bytes)) == 0 ) - { - message += crypto_box_ZEROBYTES; - len -= crypto_box_ZEROBYTES; - return(message); - } - return(0); -} - -//#undef force_inline - #endif diff --git a/src/komodo_kv.h b/src/komodo_kv.h index ad8bb1ee4..b77e2f58a 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -41,7 +41,7 @@ int32_t komodo_kvsearch(uint256 *pubkeyp,int32_t current_height,uint32_t *flagsp { *heightp = ptr->height; *flagsp = ptr->flags; - memcpy(pubkeyp,ptr->pubkey,sizeof(*pubkeyp)); + memcpy(pubkeyp,&ptr->pubkey,sizeof(*pubkeyp)); if ( (retval= ptr->valuesize) != 0 ) memcpy(value,ptr->value,retval); } @@ -85,8 +85,8 @@ uint64_t komodo_kvfee(uint32_t flags,int32_t opretlen,int32_t keylen) void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) { - static bits256 zeroes; - uint32_t flags; bits256 pubkey,sig; int32_t i,transferflag,hassig,coresize,haspubkey,height,kvheight; uint16_t keylen,valuesize,newflag = 0; uint8_t *key,*valueptr,valuebuf[IGUANA_MAXSCRIPTSIZE]; struct komodo_kv *ptr; char *transferpubstr; + static uint256 zeroes; + uint32_t flags; bits256 pubkey,refpubkey,sig; int32_t i,transferflag,hassig,coresize,haspubkey,height,kvheight; uint16_t keylen,valuesize,newflag = 0; uint8_t *key,*valueptr,valuebuf[IGUANA_MAXSCRIPTSIZE]; struct komodo_kv *ptr; char *transferpubstr; iguana_rwnum(0,&opretbuf[1],sizeof(keylen),&keylen); iguana_rwnum(0,&opretbuf[3],sizeof(valuesize),&valuesize); iguana_rwnum(0,&opretbuf[5],sizeof(height),&height); @@ -112,7 +112,7 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) for (i=0; i<32; i++) ((uint8_t *)&sig)[i] = opretbuf[coresize+sizeof(bits256)+i]; } - if ( komodo_kvsearch(&refpubkey,height,&flags,&kvheight,valuebuf,key,keylen) >= 0 ) + if ( komodo_kvsearch((uint256 *)&refpubkey,height,&flags,&kvheight,valuebuf,key,keylen) >= 0 ) { if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) { @@ -131,7 +131,7 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) if ( (ptr->flags & KOMODO_KVPROTECTED) != 0 && memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) { transferpubstr = (char *)&value[strlen((char *)"transfer:")]; - if ( strncmp((char *)"transfer:",value,strlen((char *)"transfer:")) == 0 && is_hexstr(transferpubstr,0) == 64 ) + if ( strncmp((char *)"transfer:",(char *)valueptr,strlen((char *)"transfer:")) == 0 && is_hexstr(transferpubstr,0) == 64 ) { transferflag = 1; printf("transfer.(%s) to [%s]\n",key,transferpubstr); diff --git a/src/komodo_utils.h b/src/komodo_utils.h index 57171e2cb..9ab9178b3 100644 --- a/src/komodo_utils.h +++ b/src/komodo_utils.h @@ -30,9 +30,6 @@ typedef struct queue char name[64],initflag; } queue_t; -union _bits256 { uint8_t bytes[32]; uint16_t ushorts[16]; uint32_t uints[8]; uint64_t ulongs[4]; uint64_t txid; }; -typedef union _bits256 bits256; - #include "mini-gmp.c" #define CRYPTO777_PUBSECPSTR "020e46e79a2a8d12b9b5d12c7a91adb4e454edfae43c0a0cb805427d2ac7613fd9" From 29a27378fdd05d36f4b3f0d8fc2f9aa48f22875c Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 13:52:42 +0200 Subject: [PATCH 03/43] test --- src/komodo_kv.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_kv.h b/src/komodo_kv.h index b77e2f58a..c63d0c26b 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -116,7 +116,7 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) { if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) { - if ( pubkey != refpubkey || komodo_kvsigverify(opretbuf+13,coresize-13,refpubkey,sig) < 0 ) + if ( memcmp(&pubkey,&refpubkey,sizeof(pubkey)) != 0 || komodo_kvsigverify(opretbuf+13,coresize-13,refpubkey,sig) < 0 ) { printf("komodo_kvsigverify error [%d]\n",coresize-13); return; @@ -152,7 +152,7 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) } if ( transferflag != 0 ) { - ptr->pubkey = pubkey; + memcpy(&ptr->pubkey,&pubkey,sizeof(ptr->pubkey)); ptr->height = height; } else if ( newflag != 0 || (ptr->flags & KOMODO_KVPROTECTED) == 0 ) From 07ddd543e2fc0fe235690da1bcca4df93630aba6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 13:53:56 +0200 Subject: [PATCH 04/43] test --- src/komodo_kv.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_kv.h b/src/komodo_kv.h index c63d0c26b..6c10685c8 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -86,7 +86,7 @@ uint64_t komodo_kvfee(uint32_t flags,int32_t opretlen,int32_t keylen) void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) { static uint256 zeroes; - uint32_t flags; bits256 pubkey,refpubkey,sig; int32_t i,transferflag,hassig,coresize,haspubkey,height,kvheight; uint16_t keylen,valuesize,newflag = 0; uint8_t *key,*valueptr,valuebuf[IGUANA_MAXSCRIPTSIZE]; struct komodo_kv *ptr; char *transferpubstr; + uint32_t flags; bits256 pubkey,refpubkey,sig; int32_t i,transferflag,hassig,coresize,haspubkey,height,kvheight; uint16_t keylen,valuesize,newflag = 0; uint8_t *key,*valueptr,valuebuf[IGUANA_MAXSCRIPTSIZE]; struct komodo_kv *ptr; char *transferpubstr; uint64_t fee; iguana_rwnum(0,&opretbuf[1],sizeof(keylen),&keylen); iguana_rwnum(0,&opretbuf[3],sizeof(valuesize),&valuesize); iguana_rwnum(0,&opretbuf[5],sizeof(height),&height); @@ -130,7 +130,7 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) { if ( (ptr->flags & KOMODO_KVPROTECTED) != 0 && memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) { - transferpubstr = (char *)&value[strlen((char *)"transfer:")]; + transferpubstr = (char *)&valuestr[strlen((char *)"transfer:")]; if ( strncmp((char *)"transfer:",(char *)valueptr,strlen((char *)"transfer:")) == 0 && is_hexstr(transferpubstr,0) == 64 ) { transferflag = 1; From 084de19dd1ded497c4fabe90c3ad5624d10d4222 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 13:56:28 +0200 Subject: [PATCH 05/43] test --- src/komodo_kv.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/komodo_kv.h b/src/komodo_kv.h index 6c10685c8..624482d03 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -86,7 +86,7 @@ uint64_t komodo_kvfee(uint32_t flags,int32_t opretlen,int32_t keylen) void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) { static uint256 zeroes; - uint32_t flags; bits256 pubkey,refpubkey,sig; int32_t i,transferflag,hassig,coresize,haspubkey,height,kvheight; uint16_t keylen,valuesize,newflag = 0; uint8_t *key,*valueptr,valuebuf[IGUANA_MAXSCRIPTSIZE]; struct komodo_kv *ptr; char *transferpubstr; uint64_t fee; + uint32_t flags; bits256 pubkey,refpubkey,sig; int32_t i,transferflag,hassig,coresize,haspubkey,height,kvheight; uint16_t keylen,valuesize,newflag = 0; uint8_t *key,*valueptr,valuebuf[IGUANA_MAXSCRIPTSIZE]; struct komodo_kv *ptr; char *transferpubstr,*tstr; uint64_t fee; iguana_rwnum(0,&opretbuf[1],sizeof(keylen),&keylen); iguana_rwnum(0,&opretbuf[3],sizeof(valuesize),&valuesize); iguana_rwnum(0,&opretbuf[5],sizeof(height),&height); @@ -130,8 +130,9 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) { if ( (ptr->flags & KOMODO_KVPROTECTED) != 0 && memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) { - transferpubstr = (char *)&valuestr[strlen((char *)"transfer:")]; - if ( strncmp((char *)"transfer:",(char *)valueptr,strlen((char *)"transfer:")) == 0 && is_hexstr(transferpubstr,0) == 64 ) + tstr = (char *)"transfer:"; + transferpubstr = (char *)&valueptr[tlen]; + if ( strncmp(tstr,(char *)valueptr,strlen(tstr) == 0 && is_hexstr(transferpubstr,0) == 64 ) { transferflag = 1; printf("transfer.(%s) to [%s]\n",key,transferpubstr); From a287221ccf62d9f2c94f6ad7e5819bd2c82d60d8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 13:58:27 +0200 Subject: [PATCH 06/43] test --- src/komodo_kv.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_kv.h b/src/komodo_kv.h index 624482d03..274d723e1 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -131,8 +131,8 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) if ( (ptr->flags & KOMODO_KVPROTECTED) != 0 && memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) { tstr = (char *)"transfer:"; - transferpubstr = (char *)&valueptr[tlen]; - if ( strncmp(tstr,(char *)valueptr,strlen(tstr) == 0 && is_hexstr(transferpubstr,0) == 64 ) + transferpubstr = (char *)&valueptr[strlen(tstr)]; + if ( strncmp(tstr,(char *)valueptr,strlen(tstr)) == 0 && is_hexstr(transferpubstr,0) == 64 ) { transferflag = 1; printf("transfer.(%s) to [%s]\n",key,transferpubstr); From 684d42a23643224668fd8464ada7a75e55cd08ee Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 14:01:42 +0200 Subject: [PATCH 07/43] test --- src/wallet/rpcwallet.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 93a168931..fed261dac 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -569,8 +569,9 @@ Value paxwithdraw(const Array& params, bool fHelp) Value kvupdate(const Array& params, bool fHelp) { + static uint256 zeroes; CWalletTx wtx; Object ret; - uint8_t keyvalue[IGUANA_MAXSCRIPTSIZE],opretbuf[IGUANA_MAXSCRIPTSIZE]; int32_t coresize,haveprivkey,duration,opretlen,height; uint16_t keylen,valuesize=0,refvaluesize=0; uint8_t *key,*value=0; uint32_t flags,tmpflags; struct komodo_kv *ptr; uint64_t fee; uint256 privkey,pubkey,refpubkey,sig; + uint8_t keyvalue[IGUANA_MAXSCRIPTSIZE],opretbuf[IGUANA_MAXSCRIPTSIZE]; int32_t i,coresize,haveprivkey,duration,opretlen,height; uint16_t keylen,valuesize=0,refvaluesize=0; uint8_t *key,*value=0; uint32_t flags,tmpflags; struct komodo_kv *ptr; uint64_t fee; uint256 privkey,pubkey,refpubkey,sig; if (fHelp || params.size() < 2 ) throw runtime_error("kvupdate key value [flags] [passphrase]"); if (!EnsureWalletIsAvailable(fHelp)) @@ -586,7 +587,7 @@ Value kvupdate(const Array& params, bool fHelp) memset(&pubkey,0,sizeof(pubkey)); if ( params.size() >= 4 ) { - privkey = komodo_kvprivkey(&pubkey,params[3].get_str().c_str()); + privkey = komodo_kvprivkey(&pubkey,(char *)params[3].get_str().c_str()); haveprivkey = 1; flags |= 1; //printf("flags.%d (%s)\n",flags,params[2].get_str().c_str()); From a8fadb1882bc23f9d79a308b71f1791b52c8e562 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 14:04:13 +0200 Subject: [PATCH 08/43] test --- src/komodo_curve25519.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_curve25519.h b/src/komodo_curve25519.h index 1353628a2..1bcb67382 100644 --- a/src/komodo_curve25519.h +++ b/src/komodo_curve25519.h @@ -943,11 +943,11 @@ uint64_t conv_NXTpassword(unsigned char *mysecret,unsigned char *mypublic,uint8_ return(addr); } -bits256 komodo_kvprivkey(bits256 *pubkeyp,char *passphrase) +uint256 komodo_kvprivkey(uint256 *pubkeyp,char *passphrase) { bits256 privkey; - conv_NXTpassword(privkey.bytes,pubkeyp->bytes,(uint8_t *)passphrase,(int32_t)strlen(passphrase)); - return(privkey); + conv_NXTpassword(privkey.bytes,(uint8_t *)pubkeyp,(uint8_t *)passphrase,(int32_t)strlen(passphrase)); + return(uint256); } bits256 komodo_kvsig(uint8_t *buf,int32_t len,bits256 privkey) From a9aca4da0ea957dd3d4854e3c1e39e893b585fb3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 14:07:09 +0200 Subject: [PATCH 09/43] test --- src/komodo_curve25519.h | 15 ++++++++------- src/komodo_kv.h | 10 +++++----- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/komodo_curve25519.h b/src/komodo_curve25519.h index 1bcb67382..541e4447c 100644 --- a/src/komodo_curve25519.h +++ b/src/komodo_curve25519.h @@ -945,18 +945,19 @@ uint64_t conv_NXTpassword(unsigned char *mysecret,unsigned char *mypublic,uint8_ uint256 komodo_kvprivkey(uint256 *pubkeyp,char *passphrase) { - bits256 privkey; - conv_NXTpassword(privkey.bytes,(uint8_t *)pubkeyp,(uint8_t *)passphrase,(int32_t)strlen(passphrase)); - return(uint256); + uint256 privkey; + conv_NXTpassword((uint8_t *)&privkey,(uint8_t *)pubkeyp,(uint8_t *)passphrase,(int32_t)strlen(passphrase)); + return(privkey); } -bits256 komodo_kvsig(uint8_t *buf,int32_t len,bits256 privkey) +uint256 komodo_kvsig(uint8_t *buf,int32_t len,uint256 privkey) { - bits256 sig,hash,otherpub; + bits256 sig,hash,otherpub; uint256 usig; vcalc_sha256(0,hash.bytes,buf,len); otherpub = curve25519(hash,curve25519_basepoint9()); - sig = curve25519_shared(privkey,otherpub); - return(sig); + sig = curve25519_shared(*(bits256 *)&privkey,otherpub); + memcpy(&usig,&sig,sizeof(usig)); + return(usig); } int32_t komodo_kvsigverify(uint8_t *buf,int32_t len,bits256 pubkey,bits256 sig) diff --git a/src/komodo_kv.h b/src/komodo_kv.h index 274d723e1..38ea99bc1 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -86,7 +86,7 @@ uint64_t komodo_kvfee(uint32_t flags,int32_t opretlen,int32_t keylen) void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) { static uint256 zeroes; - uint32_t flags; bits256 pubkey,refpubkey,sig; int32_t i,transferflag,hassig,coresize,haspubkey,height,kvheight; uint16_t keylen,valuesize,newflag = 0; uint8_t *key,*valueptr,valuebuf[IGUANA_MAXSCRIPTSIZE]; struct komodo_kv *ptr; char *transferpubstr,*tstr; uint64_t fee; + uint32_t flags; uint256 pubkey,refpubkey,sig; int32_t i,transferflag,hassig,coresize,haspubkey,height,kvheight; uint16_t keylen,valuesize,newflag = 0; uint8_t *key,*valueptr,valuebuf[IGUANA_MAXSCRIPTSIZE]; struct komodo_kv *ptr; char *transferpubstr,*tstr; uint64_t fee; iguana_rwnum(0,&opretbuf[1],sizeof(keylen),&keylen); iguana_rwnum(0,&opretbuf[3],sizeof(valuesize),&valuesize); iguana_rwnum(0,&opretbuf[5],sizeof(height),&height); @@ -98,19 +98,19 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) if ( value >= fee ) { coresize = (int32_t)(sizeof(flags)+sizeof(height)+sizeof(keylen)+sizeof(valuesize)+keylen+valuesize+1); - if ( opretlen == coresize || opretlen == coresize+sizeof(bits256) || opretlen == coresize+2*sizeof(bits256) ) + if ( opretlen == coresize || opretlen == coresize+sizeof(uint256) || opretlen == coresize+2*sizeof(uint256) ) { memset(&pubkey,0,sizeof(pubkey)); memset(&sig,0,sizeof(sig)); - if ( (haspubkey= (opretlen >= coresize+sizeof(bits256))) != 0 ) + if ( (haspubkey= (opretlen >= coresize+sizeof(uint256))) != 0 ) { for (i=0; i<32; i++) ((uint8_t *)&pubkey)[i] = opretbuf[coresize+i]; } - if ( (hassig= (opretlen == coresize+sizeof(bits256)*2)) != 0 ) + if ( (hassig= (opretlen == coresize+sizeof(uint256)*2)) != 0 ) { for (i=0; i<32; i++) - ((uint8_t *)&sig)[i] = opretbuf[coresize+sizeof(bits256)+i]; + ((uint8_t *)&sig)[i] = opretbuf[coresize+sizeof(uint256)+i]; } if ( komodo_kvsearch((uint256 *)&refpubkey,height,&flags,&kvheight,valuebuf,key,keylen) >= 0 ) { From d65e1570517c1a156ad599e834e3d4d209155417 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 14:09:15 +0200 Subject: [PATCH 10/43] test --- src/komodo_curve25519.h | 2 +- src/komodo_kv.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_curve25519.h b/src/komodo_curve25519.h index 541e4447c..483c3e0d9 100644 --- a/src/komodo_curve25519.h +++ b/src/komodo_curve25519.h @@ -960,7 +960,7 @@ uint256 komodo_kvsig(uint8_t *buf,int32_t len,uint256 privkey) return(usig); } -int32_t komodo_kvsigverify(uint8_t *buf,int32_t len,bits256 pubkey,bits256 sig) +int32_t komodo_kvsigverify(uint8_t *buf,int32_t len,uint256 pubkey,uint256 sig) { bits256 hash,checksig; vcalc_sha256(0,hash.bytes,buf,len); diff --git a/src/komodo_kv.h b/src/komodo_kv.h index 38ea99bc1..fede46e03 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -144,7 +144,7 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) else if ( ptr == 0 ) { ptr = (struct komodo_kv *)calloc(1,sizeof(*ptr)); - ptr->pubkey = pubkey; + memcpy(&ptr->pubkey,&pubkey,sizeof(ptr->pubkey)); ptr->key = (uint8_t *)calloc(1,keylen); ptr->keylen = keylen; memcpy(ptr->key,key,keylen); From 906d8629ed997c82bf9375663a350806a3d2afa5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 14:11:18 +0200 Subject: [PATCH 11/43] test --- src/komodo_curve25519.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_curve25519.h b/src/komodo_curve25519.h index 483c3e0d9..7271e3741 100644 --- a/src/komodo_curve25519.h +++ b/src/komodo_curve25519.h @@ -964,7 +964,7 @@ int32_t komodo_kvsigverify(uint8_t *buf,int32_t len,uint256 pubkey,uint256 sig) { bits256 hash,checksig; vcalc_sha256(0,hash.bytes,buf,len); - checksig = curve25519_shared(hash,pubkey); + checksig = curve25519_shared(hash,*(bits256 *)&pubkey); if ( memcmp(&checksig,&sig,sizeof(sig)) != 0 ) return(-1); else return(0); From 2e59d684e62b52f513d06f7992fafe47689d6504 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 14:16:27 +0200 Subject: [PATCH 12/43] test --- src/wallet/rpcwallet.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index fed261dac..108efd81a 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -590,6 +590,7 @@ Value kvupdate(const Array& params, bool fHelp) privkey = komodo_kvprivkey(&pubkey,(char *)params[3].get_str().c_str()); haveprivkey = 1; flags |= 1; + printf("have privkey derived from (%s)\n",(char *)params[3].get_str().c_str()); //printf("flags.%d (%s)\n",flags,params[2].get_str().c_str()); } LOCK2(cs_main, pwalletMain->cs_wallet); @@ -611,6 +612,7 @@ Value kvupdate(const Array& params, bool fHelp) ret.push_back(Pair("error",(char *)"cant modify write once key without passphrase")); return ret; } + printf("calc sig for keylen.%d + valuesize.%d\n",keylen,valuesize); sig = komodo_kvsig(keyvalue,keylen+valuesize,privkey); } } From 2159c56aa2d81a0a36189b1b9ca4147b92fa322a Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 14:19:12 +0200 Subject: [PATCH 13/43] test --- src/wallet/rpcwallet.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 108efd81a..6196d488b 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -593,6 +593,7 @@ Value kvupdate(const Array& params, bool fHelp) printf("have privkey derived from (%s)\n",(char *)params[3].get_str().c_str()); //printf("flags.%d (%s)\n",flags,params[2].get_str().c_str()); } + printf("params.%d flags.%d haveprivkey.%d (%s %s)\n",params.size(),flags,haveprivkey,(char *)params[1].get_str().c_str(),(char *)params[3].get_str().c_str()); LOCK2(cs_main, pwalletMain->cs_wallet); if ( (keylen= (int32_t)strlen(params[0].get_str().c_str())) > 0 ) { From 804b008f3222c1dbabcda42ca6eb4b17b12fd0b1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 14:20:39 +0200 Subject: [PATCH 14/43] test --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 6196d488b..01bebfd9b 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -593,7 +593,7 @@ Value kvupdate(const Array& params, bool fHelp) printf("have privkey derived from (%s)\n",(char *)params[3].get_str().c_str()); //printf("flags.%d (%s)\n",flags,params[2].get_str().c_str()); } - printf("params.%d flags.%d haveprivkey.%d (%s %s)\n",params.size(),flags,haveprivkey,(char *)params[1].get_str().c_str(),(char *)params[3].get_str().c_str()); + printf("params.%d flags.%d haveprivkey.%d (%s %s)\n",(int32_t)params.size(),flags,haveprivkey,(char *)params[1].get_str().c_str(),(char *)params[3].get_str().c_str()); LOCK2(cs_main, pwalletMain->cs_wallet); if ( (keylen= (int32_t)strlen(params[0].get_str().c_str())) > 0 ) { From 67abb280f0028c5c3261b4263ed8ba66a2d94a41 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 14:22:52 +0200 Subject: [PATCH 15/43] test --- src/wallet/rpcwallet.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 01bebfd9b..dad420d02 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -579,7 +579,7 @@ Value kvupdate(const Array& params, bool fHelp) if ( params.size() >= 3 ) { flags = atoi(params[2].get_str().c_str()); - //printf("flags.%d (%s)\n",flags,params[2].get_str().c_str()); + printf("flags.%d (%s)\n",flags,params[2].get_str().c_str()); } else flags = 0; haveprivkey = 0; memset(&sig,0,sizeof(sig)); @@ -593,7 +593,6 @@ Value kvupdate(const Array& params, bool fHelp) printf("have privkey derived from (%s)\n",(char *)params[3].get_str().c_str()); //printf("flags.%d (%s)\n",flags,params[2].get_str().c_str()); } - printf("params.%d flags.%d haveprivkey.%d (%s %s)\n",(int32_t)params.size(),flags,haveprivkey,(char *)params[1].get_str().c_str(),(char *)params[3].get_str().c_str()); LOCK2(cs_main, pwalletMain->cs_wallet); if ( (keylen= (int32_t)strlen(params[0].get_str().c_str())) > 0 ) { From b2281764fae510b9848762a58ab76652a0466c1c Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 14:25:15 +0200 Subject: [PATCH 16/43] test --- src/rpcclient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpcclient.cpp b/src/rpcclient.cpp index 5b89b18c4..62278b725 100644 --- a/src/rpcclient.cpp +++ b/src/rpcclient.cpp @@ -113,7 +113,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "notaries", 1 }, { "minerids", 1 }, { "kvsearch", 1 }, - { "kvupdate", 4 }, + { "kvupdate", 5 }, }; class CRPCConvertTable From ad2c025a0ec9340e91d65353b345ceb0a941adf7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 14:27:28 +0200 Subject: [PATCH 17/43] test --- src/rpcclient.cpp | 2 +- src/wallet/rpcwallet.cpp | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/rpcclient.cpp b/src/rpcclient.cpp index 62278b725..5b89b18c4 100644 --- a/src/rpcclient.cpp +++ b/src/rpcclient.cpp @@ -113,7 +113,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "notaries", 1 }, { "minerids", 1 }, { "kvsearch", 1 }, - { "kvupdate", 5 }, + { "kvupdate", 4 }, }; class CRPCConvertTable diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index dad420d02..9a68a5914 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -572,20 +572,20 @@ Value kvupdate(const Array& params, bool fHelp) static uint256 zeroes; CWalletTx wtx; Object ret; uint8_t keyvalue[IGUANA_MAXSCRIPTSIZE],opretbuf[IGUANA_MAXSCRIPTSIZE]; int32_t i,coresize,haveprivkey,duration,opretlen,height; uint16_t keylen,valuesize=0,refvaluesize=0; uint8_t *key,*value=0; uint32_t flags,tmpflags; struct komodo_kv *ptr; uint64_t fee; uint256 privkey,pubkey,refpubkey,sig; - if (fHelp || params.size() < 2 ) - throw runtime_error("kvupdate key value [flags] [passphrase]"); + if (fHelp || params.size() < 4 ) + throw runtime_error("kvupdate key value flags passphrase"); if (!EnsureWalletIsAvailable(fHelp)) return 0; + haveprivkey = 0; + memset(&sig,0,sizeof(sig)); + memset(&privkey,0,sizeof(privkey)); + memset(&pubkey,0,sizeof(pubkey)); if ( params.size() >= 3 ) { flags = atoi(params[2].get_str().c_str()); printf("flags.%d (%s)\n",flags,params[2].get_str().c_str()); } else flags = 0; - haveprivkey = 0; - memset(&sig,0,sizeof(sig)); - memset(&privkey,0,sizeof(privkey)); - memset(&pubkey,0,sizeof(pubkey)); - if ( params.size() >= 4 ) + //if ( params.size() >= 4 ) { privkey = komodo_kvprivkey(&pubkey,(char *)params[3].get_str().c_str()); haveprivkey = 1; From 2987e6502107f70eac7cbb7cbcccf0a2ef5e93be Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 14:30:28 +0200 Subject: [PATCH 18/43] test --- src/wallet/rpcwallet.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 9a68a5914..9791fe4cf 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -572,8 +572,8 @@ Value kvupdate(const Array& params, bool fHelp) static uint256 zeroes; CWalletTx wtx; Object ret; uint8_t keyvalue[IGUANA_MAXSCRIPTSIZE],opretbuf[IGUANA_MAXSCRIPTSIZE]; int32_t i,coresize,haveprivkey,duration,opretlen,height; uint16_t keylen,valuesize=0,refvaluesize=0; uint8_t *key,*value=0; uint32_t flags,tmpflags; struct komodo_kv *ptr; uint64_t fee; uint256 privkey,pubkey,refpubkey,sig; - if (fHelp || params.size() < 4 ) - throw runtime_error("kvupdate key value flags passphrase"); + if (fHelp || params.size() < 3 ) + throw runtime_error("kvupdate key value flags/passphrase"); if (!EnsureWalletIsAvailable(fHelp)) return 0; haveprivkey = 0; @@ -587,10 +587,10 @@ Value kvupdate(const Array& params, bool fHelp) } else flags = 0; //if ( params.size() >= 4 ) { - privkey = komodo_kvprivkey(&pubkey,(char *)params[3].get_str().c_str()); + privkey = komodo_kvprivkey(&pubkey,(char *)params[2].get_str().c_str()); haveprivkey = 1; flags |= 1; - printf("have privkey derived from (%s)\n",(char *)params[3].get_str().c_str()); + printf("have privkey derived from (%s)\n",(char *)params[2].get_str().c_str()); //printf("flags.%d (%s)\n",flags,params[2].get_str().c_str()); } LOCK2(cs_main, pwalletMain->cs_wallet); From c929bc3e0450c0ba37828ce49d57bc35623ea3d9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 19:24:41 +0200 Subject: [PATCH 19/43] test --- src/rpcclient.cpp | 2 +- src/wallet/rpcwallet.cpp | 207 ++++++++++++++++++++------------------- 2 files changed, 107 insertions(+), 102 deletions(-) diff --git a/src/rpcclient.cpp b/src/rpcclient.cpp index 5b89b18c4..933b550a5 100644 --- a/src/rpcclient.cpp +++ b/src/rpcclient.cpp @@ -113,7 +113,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "notaries", 1 }, { "minerids", 1 }, { "kvsearch", 1 }, - { "kvupdate", 4 }, + //{ "kvupdate", 4 }, }; class CRPCConvertTable diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 9791fe4cf..ddb099453 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -494,6 +494,112 @@ uint256 komodo_kvsig(uint8_t *buf,int32_t len,uint256 privkey); int32_t komodo_kvduration(uint32_t flags); uint256 komodo_kvprivkey(uint256 *pubkeyp,char *passphrase); +Value kvupdate(const Array& params, bool fHelp) +{ + static uint256 zeroes; + CWalletTx wtx; Object ret; + uint8_t keyvalue[IGUANA_MAXSCRIPTSIZE],opretbuf[IGUANA_MAXSCRIPTSIZE]; int32_t i,coresize,haveprivkey,duration,opretlen,height; uint16_t keylen,valuesize=0,refvaluesize=0; uint8_t *key,*value=0; uint32_t flags,tmpflags,n; struct komodo_kv *ptr; uint64_t fee; uint256 privkey,pubkey,refpubkey,sig; + if (fHelp || params.size() < 3 ) + throw runtime_error("kvupdate key value flags/passphrase"); + if (!EnsureWalletIsAvailable(fHelp)) + return 0; + haveprivkey = 0; + memset(&sig,0,sizeof(sig)); + memset(&privkey,0,sizeof(privkey)); + memset(&pubkey,0,sizeof(pubkey)); + if ( (n= (int32_t)params.size()) >= 3 ) + { + flags = atoi(params[2].get_str().c_str()); + printf("flags.%d (%s) n.%d\n",flags,params[2].get_str().c_str(),n); + } else flags = 0; + if ( n >= 4 ) + { + privkey = komodo_kvprivkey(&pubkey,(char *)params[3].get_str().c_str()); + haveprivkey = 1; + flags |= 1; + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&privkey)[i]); + printf(" priv, "); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&pubkey)[i]); + printf(" pubkey, privkey derived from (%s)\n",(char *)params[3].get_str().c_str()); + //printf("flags.%d (%s)\n",flags,params[2].get_str().c_str()); + } + LOCK2(cs_main, pwalletMain->cs_wallet); + if ( (keylen= (int32_t)strlen(params[0].get_str().c_str())) > 0 ) + { + key = (uint8_t *)params[0].get_str().c_str(); + if ( n >= 2 && params[1].get_str().c_str() != 0 ) + { + value = (uint8_t *)params[1].get_str().c_str(); + valuesize = (int32_t)strlen(params[1].get_str().c_str()); + } + memcpy(keyvalue,key,keylen); + if ( (refvaluesize= komodo_kvsearch(&refpubkey,chainActive.Tip()->nHeight,&tmpflags,&height,&keyvalue[keylen],key,keylen)) >= 0 ) + { + if ( (tmpflags & KOMODO_KVPROTECTED) != 0 ) + { + if ( refpubkey != pubkey ) + { + ret.push_back(Pair("error",(char *)"cant modify write once key without passphrase")); + return ret; + } + printf("calc sig for keylen.%d + valuesize.%d\n",keylen,valuesize); + sig = komodo_kvsig(keyvalue,keylen+valuesize,privkey); + } + } + ret.push_back(Pair("coin",(char *)(ASSETCHAINS_SYMBOL[0] == 0 ? "KMD" : ASSETCHAINS_SYMBOL))); + height = chainActive.Tip()->nHeight; + if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) + ret.push_back(Pair("owner",refpubkey.GetHex())); + ret.push_back(Pair("height", (int64_t)height)); + duration = komodo_kvduration(flags); //((flags >> 2) + 1) * KOMODO_KVDURATION; + ret.push_back(Pair("expiration", (int64_t)(height+duration))); + ret.push_back(Pair("flags",(int64_t)flags)); + ret.push_back(Pair("key",params[0].get_str())); + ret.push_back(Pair("keylen",(int64_t)keylen)); + if ( n >= 2 && params[1].get_str().c_str() != 0 ) + { + ret.push_back(Pair("value",params[1].get_str())); + ret.push_back(Pair("valuesize",valuesize)); + } + iguana_rwnum(1,&keyvalue[0],sizeof(keylen),&keylen); + iguana_rwnum(1,&keyvalue[2],sizeof(valuesize),&valuesize); + iguana_rwnum(1,&keyvalue[4],sizeof(height),&height); + iguana_rwnum(1,&keyvalue[8],sizeof(flags),&flags); + memcpy(&keyvalue[12],key,keylen); + if ( value != 0 ) + memcpy(&keyvalue[12 + keylen],value,valuesize); + coresize = (int32_t)(sizeof(flags)+sizeof(height)+sizeof(uint16_t)*2+keylen+valuesize); + if ( haveprivkey != 0 ) + { + for (i=0; i<32; i++) + keyvalue[12 + keylen + valuesize + i] = ((uint8_t *)&pubkey)[i]; + coresize += 32; + if ( refvaluesize >=0 ) + { + for (i=0; i<32; i++) + keyvalue[12 + keylen + valuesize + 32 + i] = ((uint8_t *)&sig)[i]; + coresize += 32; + } + } + if ( (opretlen= komodo_opreturnscript(opretbuf,'K',keyvalue,coresize)) == 40 ) + opretlen++; + //for (i=0; i= 3 ) - { - flags = atoi(params[2].get_str().c_str()); - printf("flags.%d (%s)\n",flags,params[2].get_str().c_str()); - } else flags = 0; - //if ( params.size() >= 4 ) - { - privkey = komodo_kvprivkey(&pubkey,(char *)params[2].get_str().c_str()); - haveprivkey = 1; - flags |= 1; - printf("have privkey derived from (%s)\n",(char *)params[2].get_str().c_str()); - //printf("flags.%d (%s)\n",flags,params[2].get_str().c_str()); - } - LOCK2(cs_main, pwalletMain->cs_wallet); - if ( (keylen= (int32_t)strlen(params[0].get_str().c_str())) > 0 ) - { - key = (uint8_t *)params[0].get_str().c_str(); - if ( params.size() >= 2 && params[1].get_str().c_str() != 0 ) - { - value = (uint8_t *)params[1].get_str().c_str(); - valuesize = (int32_t)strlen(params[1].get_str().c_str()); - } - memcpy(keyvalue,key,keylen); - if ( (refvaluesize= komodo_kvsearch(&refpubkey,chainActive.Tip()->nHeight,&tmpflags,&height,&keyvalue[keylen],key,keylen)) >= 0 ) - { - if ( (tmpflags & KOMODO_KVPROTECTED) != 0 ) - { - if ( refpubkey != pubkey ) - { - ret.push_back(Pair("error",(char *)"cant modify write once key without passphrase")); - return ret; - } - printf("calc sig for keylen.%d + valuesize.%d\n",keylen,valuesize); - sig = komodo_kvsig(keyvalue,keylen+valuesize,privkey); - } - } - ret.push_back(Pair("coin",(char *)(ASSETCHAINS_SYMBOL[0] == 0 ? "KMD" : ASSETCHAINS_SYMBOL))); - height = chainActive.Tip()->nHeight; - if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) - ret.push_back(Pair("owner",refpubkey.GetHex())); - ret.push_back(Pair("height", (int64_t)height)); - duration = komodo_kvduration(flags); //((flags >> 2) + 1) * KOMODO_KVDURATION; - ret.push_back(Pair("expiration", (int64_t)(height+duration))); - ret.push_back(Pair("flags",(int64_t)flags)); - ret.push_back(Pair("key",params[0].get_str())); - ret.push_back(Pair("keylen",(int64_t)keylen)); - if ( params.size() >= 2 && params[1].get_str().c_str() != 0 ) - { - ret.push_back(Pair("value",params[1].get_str())); - ret.push_back(Pair("valuesize",valuesize)); - } - iguana_rwnum(1,&keyvalue[0],sizeof(keylen),&keylen); - iguana_rwnum(1,&keyvalue[2],sizeof(valuesize),&valuesize); - iguana_rwnum(1,&keyvalue[4],sizeof(height),&height); - iguana_rwnum(1,&keyvalue[8],sizeof(flags),&flags); - memcpy(&keyvalue[12],key,keylen); - if ( value != 0 ) - memcpy(&keyvalue[12 + keylen],value,valuesize); - coresize = (int32_t)(sizeof(flags)+sizeof(height)+sizeof(uint16_t)*2+keylen+valuesize); - if ( haveprivkey != 0 ) - { - for (i=0; i<32; i++) - keyvalue[12 + keylen + valuesize + i] = ((uint8_t *)&pubkey)[i]; - coresize += 32; - if ( refvaluesize >=0 ) - { - for (i=0; i<32; i++) - keyvalue[12 + keylen + valuesize + 32 + i] = ((uint8_t *)&sig)[i]; - coresize += 32; - } - } - if ( (opretlen= komodo_opreturnscript(opretbuf,'K',keyvalue,coresize)) == 40 ) - opretlen++; - //for (i=0; i Date: Sun, 22 Jan 2017 19:30:01 +0200 Subject: [PATCH 20/43] test --- src/fiat/aud | 2 +- src/fiat/bet | 2 +- src/fiat/bgn | 2 +- src/fiat/bots | 2 +- src/fiat/brl | 2 +- src/fiat/cad | 2 +- src/fiat/chf | 2 +- src/fiat/cny | 2 +- src/fiat/crypto | 2 +- src/fiat/czk | 2 +- src/fiat/dex | 2 +- src/fiat/dkk | 2 +- src/fiat/eur | 2 +- src/fiat/gbp | 2 +- src/fiat/hkd | 2 +- src/fiat/hodl | 2 +- src/fiat/hrk | 2 +- src/fiat/huf | 2 +- src/fiat/idr | 2 +- src/fiat/ils | 2 +- src/fiat/inr | 2 +- src/fiat/jpy | 2 +- src/fiat/jumblr | 2 +- src/fiat/krw | 2 +- src/fiat/kv | 2 +- src/fiat/mgw | 2 +- src/fiat/mvp | 2 +- src/fiat/mxn | 2 +- src/fiat/myr | 2 +- src/fiat/nok | 2 +- src/fiat/nzd | 2 +- src/fiat/pangea | 2 +- src/fiat/php | 2 +- src/fiat/pln | 2 +- src/fiat/revs | 2 +- src/fiat/ron | 2 +- src/fiat/rub | 2 +- src/fiat/sek | 2 +- src/fiat/sgd | 2 +- src/fiat/shark | 2 +- src/fiat/supernet | 2 +- src/fiat/thb | 2 +- src/fiat/try | 2 +- src/fiat/usd | 2 +- src/fiat/wireless | 2 +- src/fiat/zar | 2 +- 46 files changed, 46 insertions(+), 46 deletions(-) diff --git a/src/fiat/aud b/src/fiat/aud index bfacdc4e0..6fa6e2cbb 100755 --- a/src/fiat/aud +++ b/src/fiat/aud @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=AUD $1 $2 $3 $4 +./komodo-cli -ac_name=AUD $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/bet b/src/fiat/bet index 73208cab5..ed99ce59f 100755 --- a/src/fiat/bet +++ b/src/fiat/bet @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=BET $1 $2 $3 $4 +./komodo-cli -ac_name=BET $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/bgn b/src/fiat/bgn index dad8778a6..fdc0f5763 100755 --- a/src/fiat/bgn +++ b/src/fiat/bgn @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=BGN $1 $2 $3 $4 +./komodo-cli -ac_name=BGN $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/bots b/src/fiat/bots index 4fc22f5eb..15c0fc288 100755 --- a/src/fiat/bots +++ b/src/fiat/bots @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=BOTS $1 $2 $3 $4 +./komodo-cli -ac_name=BOTS $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/brl b/src/fiat/brl index 489749d08..b36365d99 100755 --- a/src/fiat/brl +++ b/src/fiat/brl @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=BRL $1 $2 $3 $4 +./komodo-cli -ac_name=BRL $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/cad b/src/fiat/cad index 4a199a30a..509150867 100755 --- a/src/fiat/cad +++ b/src/fiat/cad @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=CAD $1 $2 $3 $4 +./komodo-cli -ac_name=CAD $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/chf b/src/fiat/chf index fd2a5dbdb..fbe57148c 100755 --- a/src/fiat/chf +++ b/src/fiat/chf @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=CHF $1 $2 $3 $4 +./komodo-cli -ac_name=CHF $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/cny b/src/fiat/cny index f67ccd215..aa4e2ca25 100755 --- a/src/fiat/cny +++ b/src/fiat/cny @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=CNY $1 $2 $3 $4 +./komodo-cli -ac_name=CNY $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/crypto b/src/fiat/crypto index cb35025fa..5a3d46188 100755 --- a/src/fiat/crypto +++ b/src/fiat/crypto @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=CRYPTO $1 $2 $3 $4 +./komodo-cli -ac_name=CRYPTO $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/czk b/src/fiat/czk index fb3101564..c0c1072d2 100755 --- a/src/fiat/czk +++ b/src/fiat/czk @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=CZK $1 $2 $3 $4 +./komodo-cli -ac_name=CZK $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/dex b/src/fiat/dex index b1ad72e09..67331201a 100755 --- a/src/fiat/dex +++ b/src/fiat/dex @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=DEX $1 $2 $3 $4 +./komodo-cli -ac_name=DEX $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/dkk b/src/fiat/dkk index e8b65848c..aaa6108e0 100755 --- a/src/fiat/dkk +++ b/src/fiat/dkk @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=DKK $1 $2 $3 $4 +./komodo-cli -ac_name=DKK $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/eur b/src/fiat/eur index 60e66af1c..67de01b7d 100755 --- a/src/fiat/eur +++ b/src/fiat/eur @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=EUR $1 $2 $3 $4 +./komodo-cli -ac_name=EUR $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/gbp b/src/fiat/gbp index fa1263e3f..eb4528f95 100755 --- a/src/fiat/gbp +++ b/src/fiat/gbp @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=GBP $1 $2 $3 $4 +./komodo-cli -ac_name=GBP $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/hkd b/src/fiat/hkd index dcb9bf97c..006ba7fe6 100755 --- a/src/fiat/hkd +++ b/src/fiat/hkd @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=HKD $1 $2 $3 $4 +./komodo-cli -ac_name=HKD $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/hodl b/src/fiat/hodl index 192326ada..a5430db59 100755 --- a/src/fiat/hodl +++ b/src/fiat/hodl @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=HODL $1 $2 $3 $4 +./komodo-cli -ac_name=HODL $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/hrk b/src/fiat/hrk index 283794d04..393b21fe3 100755 --- a/src/fiat/hrk +++ b/src/fiat/hrk @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=HRK $1 $2 $3 $4 +./komodo-cli -ac_name=HRK $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/huf b/src/fiat/huf index 336a75bb4..55a4f7c50 100755 --- a/src/fiat/huf +++ b/src/fiat/huf @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=HUF $1 $2 $3 $4 +./komodo-cli -ac_name=HUF $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/idr b/src/fiat/idr index 34b7a204c..62ce7968d 100755 --- a/src/fiat/idr +++ b/src/fiat/idr @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=IDR $1 $2 $3 $4 +./komodo-cli -ac_name=IDR $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/ils b/src/fiat/ils index c06588689..de1e552b8 100755 --- a/src/fiat/ils +++ b/src/fiat/ils @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=ILS $1 $2 $3 $4 +./komodo-cli -ac_name=ILS $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/inr b/src/fiat/inr index b4d230622..10ffc9d51 100755 --- a/src/fiat/inr +++ b/src/fiat/inr @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=INR $1 $2 $3 $4 +./komodo-cli -ac_name=INR $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/jpy b/src/fiat/jpy index 6d437f02b..5b8fd3768 100755 --- a/src/fiat/jpy +++ b/src/fiat/jpy @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=JPY $1 $2 $3 $4 +./komodo-cli -ac_name=JPY $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/jumblr b/src/fiat/jumblr index 24d332184..2969c5009 100755 --- a/src/fiat/jumblr +++ b/src/fiat/jumblr @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=JUMBLR $1 $2 $3 $4 +./komodo-cli -ac_name=JUMBLR $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/krw b/src/fiat/krw index 51b59dc12..7b895b1f4 100755 --- a/src/fiat/krw +++ b/src/fiat/krw @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=KRW $1 $2 $3 $4 +./komodo-cli -ac_name=KRW $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/kv b/src/fiat/kv index c735138e3..997fccb33 100755 --- a/src/fiat/kv +++ b/src/fiat/kv @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=KV $1 $2 $3 $4 +./komodo-cli -ac_name=KV $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/mgw b/src/fiat/mgw index d498ed85a..9afa86b7a 100755 --- a/src/fiat/mgw +++ b/src/fiat/mgw @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=MGW $1 $2 $3 $4 +./komodo-cli -ac_name=MGW $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/mvp b/src/fiat/mvp index 3bbe64bfb..998d700a3 100755 --- a/src/fiat/mvp +++ b/src/fiat/mvp @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=MVP $1 $2 $3 $4 +./komodo-cli -ac_name=MVP $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/mxn b/src/fiat/mxn index a4f119517..d5d8d97fb 100755 --- a/src/fiat/mxn +++ b/src/fiat/mxn @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=MXN $1 $2 $3 $4 +./komodo-cli -ac_name=MXN $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/myr b/src/fiat/myr index 5de0c80da..8c700a53c 100755 --- a/src/fiat/myr +++ b/src/fiat/myr @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=MYR $1 $2 $3 $4 +./komodo-cli -ac_name=MYR $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/nok b/src/fiat/nok index fb1a25430..e0e35b444 100755 --- a/src/fiat/nok +++ b/src/fiat/nok @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=NOK $1 $2 $3 $4 +./komodo-cli -ac_name=NOK $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/nzd b/src/fiat/nzd index af598570a..c9610b218 100755 --- a/src/fiat/nzd +++ b/src/fiat/nzd @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=NZD $1 $2 $3 $4 +./komodo-cli -ac_name=NZD $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/pangea b/src/fiat/pangea index de19a2d6d..9920189ea 100755 --- a/src/fiat/pangea +++ b/src/fiat/pangea @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=PANGEA $1 $2 $3 $4 +./komodo-cli -ac_name=PANGEA $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/php b/src/fiat/php index f333251fd..c22e73293 100755 --- a/src/fiat/php +++ b/src/fiat/php @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=PHP $1 $2 $3 $4 +./komodo-cli -ac_name=PHP $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/pln b/src/fiat/pln index 5fcc12ba4..6dfb0debb 100755 --- a/src/fiat/pln +++ b/src/fiat/pln @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=PLN $1 $2 $3 $4 +./komodo-cli -ac_name=PLN $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/revs b/src/fiat/revs index 5c8782ae5..b898eb5dc 100755 --- a/src/fiat/revs +++ b/src/fiat/revs @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=REVS $1 $2 $3 $4 +./komodo-cli -ac_name=REVS $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/ron b/src/fiat/ron index 3ea339896..0b2a6effd 100755 --- a/src/fiat/ron +++ b/src/fiat/ron @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=RON $1 $2 $3 $4 +./komodo-cli -ac_name=RON $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/rub b/src/fiat/rub index 367dbf46d..8d1575578 100755 --- a/src/fiat/rub +++ b/src/fiat/rub @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=RUB $1 $2 $3 $4 +./komodo-cli -ac_name=RUB $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/sek b/src/fiat/sek index 9fb37e674..25a14d414 100755 --- a/src/fiat/sek +++ b/src/fiat/sek @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=SEK $1 $2 $3 $4 +./komodo-cli -ac_name=SEK $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/sgd b/src/fiat/sgd index f9c75ade8..89a1bc5ae 100755 --- a/src/fiat/sgd +++ b/src/fiat/sgd @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=SGD $1 $2 $3 $4 +./komodo-cli -ac_name=SGD $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/shark b/src/fiat/shark index 4c25f6a7e..4bda53889 100755 --- a/src/fiat/shark +++ b/src/fiat/shark @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=SHARK $1 $2 $3 $4 +./komodo-cli -ac_name=SHARK $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/supernet b/src/fiat/supernet index 8afb833ca..d841ffa0e 100755 --- a/src/fiat/supernet +++ b/src/fiat/supernet @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=SUPERNET $1 $2 $3 $4 +./komodo-cli -ac_name=SUPERNET $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/thb b/src/fiat/thb index a5a7eb017..ed72913c1 100755 --- a/src/fiat/thb +++ b/src/fiat/thb @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=THB $1 $2 $3 $4 +./komodo-cli -ac_name=THB $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/try b/src/fiat/try index a06a2050d..dcfa574a6 100755 --- a/src/fiat/try +++ b/src/fiat/try @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=TRY $1 $2 $3 $4 +./komodo-cli -ac_name=TRY $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/usd b/src/fiat/usd index 12eb06e0d..81b9e16c6 100755 --- a/src/fiat/usd +++ b/src/fiat/usd @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=USD $1 $2 $3 $4 +./komodo-cli -ac_name=USD $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/wireless b/src/fiat/wireless index a4869b189..c6e092a41 100755 --- a/src/fiat/wireless +++ b/src/fiat/wireless @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=WIRELESS $1 $2 $3 $4 +./komodo-cli -ac_name=WIRELESS $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/zar b/src/fiat/zar index e5fa51785..a3dd891c0 100755 --- a/src/fiat/zar +++ b/src/fiat/zar @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=ZAR $1 $2 $3 $4 +./komodo-cli -ac_name=ZAR $1 $2 $3 $4 $5 $6 From 36a759fc5ce9cf153661cbe553636df23510eb11 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 19:37:01 +0200 Subject: [PATCH 21/43] test --- src/komodo_kv.h | 14 ++++---------- src/wallet/rpcwallet.cpp | 7 +++++-- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/komodo_kv.h b/src/komodo_kv.h index fede46e03..f4f312724 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -86,7 +86,7 @@ uint64_t komodo_kvfee(uint32_t flags,int32_t opretlen,int32_t keylen) void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) { static uint256 zeroes; - uint32_t flags; uint256 pubkey,refpubkey,sig; int32_t i,transferflag,hassig,coresize,haspubkey,height,kvheight; uint16_t keylen,valuesize,newflag = 0; uint8_t *key,*valueptr,valuebuf[IGUANA_MAXSCRIPTSIZE]; struct komodo_kv *ptr; char *transferpubstr,*tstr; uint64_t fee; + uint32_t flags; uint256 pubkey,refpubkey,sig; int32_t i,hassig,coresize,haspubkey,height,kvheight; uint16_t keylen,valuesize,newflag = 0; uint8_t *key,*valueptr,valuebuf[IGUANA_MAXSCRIPTSIZE]; struct komodo_kv *ptr; char *transferpubstr,*tstr; uint64_t fee; iguana_rwnum(0,&opretbuf[1],sizeof(keylen),&keylen); iguana_rwnum(0,&opretbuf[3],sizeof(valuesize),&valuesize); iguana_rwnum(0,&opretbuf[5],sizeof(height),&height); @@ -125,7 +125,6 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) } portable_mutex_lock(&KOMODO_KV_mutex); HASH_FIND(hh,KOMODO_KV,key,keylen,ptr); - transferflag = 0; if ( ptr != 0 ) { if ( (ptr->flags & KOMODO_KVPROTECTED) != 0 && memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) @@ -134,7 +133,8 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) transferpubstr = (char *)&valueptr[strlen(tstr)]; if ( strncmp(tstr,(char *)valueptr,strlen(tstr)) == 0 && is_hexstr(transferpubstr,0) == 64 ) { - transferflag = 1; + memcpy(&ptr->pubkey,&pubkey,sizeof(ptr->pubkey)); + ptr->height = height; printf("transfer.(%s) to [%s]\n",key,transferpubstr); for (i=0; i<32; i++) ((uint8_t *)&pubkey)[31-i] = _decode_hex(&transferpubstr[i*2]); @@ -144,19 +144,13 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) else if ( ptr == 0 ) { ptr = (struct komodo_kv *)calloc(1,sizeof(*ptr)); - memcpy(&ptr->pubkey,&pubkey,sizeof(ptr->pubkey)); ptr->key = (uint8_t *)calloc(1,keylen); ptr->keylen = keylen; memcpy(ptr->key,key,keylen); newflag = 1; HASH_ADD_KEYPTR(hh,KOMODO_KV,ptr->key,ptr->keylen,ptr); } - if ( transferflag != 0 ) - { - memcpy(&ptr->pubkey,&pubkey,sizeof(ptr->pubkey)); - ptr->height = height; - } - else if ( newflag != 0 || (ptr->flags & KOMODO_KVPROTECTED) == 0 ) + if ( newflag != 0 || (ptr->flags & KOMODO_KVPROTECTED) == 0 ) { if ( ptr->value != 0 ) free(ptr->value), ptr->value = 0; diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index ddb099453..4acdacea4 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -544,10 +544,13 @@ Value kvupdate(const Array& params, bool fHelp) ret.push_back(Pair("error",(char *)"cant modify write once key without passphrase")); return ret; } - printf("calc sig for keylen.%d + valuesize.%d\n",keylen,valuesize); - sig = komodo_kvsig(keyvalue,keylen+valuesize,privkey); + sig = komodo_kvsig(keyvalue,keylen+refvaluesize,privkey); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&sig)[i]); + printf(" sig for keylen.%d + valuesize.%d\n",keylen,refvaluesize); } } + printf("refvaluesize.%d tmpflags.%d\n",refvaluesize,tmpflags); ret.push_back(Pair("coin",(char *)(ASSETCHAINS_SYMBOL[0] == 0 ? "KMD" : ASSETCHAINS_SYMBOL))); height = chainActive.Tip()->nHeight; if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) From a9d3adf5a49b2de8b9062dfec60bd7f0f3c60ae7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 19:42:51 +0200 Subject: [PATCH 22/43] test --- src/komodo_kv.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/komodo_kv.h b/src/komodo_kv.h index f4f312724..166063a47 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -133,8 +133,6 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) transferpubstr = (char *)&valueptr[strlen(tstr)]; if ( strncmp(tstr,(char *)valueptr,strlen(tstr)) == 0 && is_hexstr(transferpubstr,0) == 64 ) { - memcpy(&ptr->pubkey,&pubkey,sizeof(ptr->pubkey)); - ptr->height = height; printf("transfer.(%s) to [%s]\n",key,transferpubstr); for (i=0; i<32; i++) ((uint8_t *)&pubkey)[31-i] = _decode_hex(&transferpubstr[i*2]); @@ -159,9 +157,10 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) ptr->value = (uint8_t *)calloc(1,valuesize); memcpy(ptr->value,valueptr,valuesize); } - ptr->height = height; - ptr->flags = flags; } + memcpy(&ptr->pubkey,&pubkey,sizeof(ptr->pubkey)); + ptr->height = height; + ptr->flags = flags; portable_mutex_unlock(&KOMODO_KV_mutex); } else printf("insufficient fee %.8f vs %.8f flags.%d keylen.%d valuesize.%d height.%d (%02x %02x %02x) (%02x %02x %02x)\n",(double)fee/COIN,(double)value/COIN,flags,keylen,valuesize,height,key[0],key[1],key[2],valueptr[0],valueptr[1],valueptr[2]); } else printf("opretlen.%d mismatch keylen.%d valuesize.%d\n",opretlen,keylen,valuesize); From d5f1c653841a8ab2b75fa9ff85545a8cee5d8bec Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 19:47:33 +0200 Subject: [PATCH 23/43] test --- src/komodo_curve25519.h | 15 +++++++++------ src/wallet/rpcwallet.cpp | 10 +++++----- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/komodo_curve25519.h b/src/komodo_curve25519.h index 7271e3741..8bca4e7fb 100644 --- a/src/komodo_curve25519.h +++ b/src/komodo_curve25519.h @@ -962,12 +962,15 @@ uint256 komodo_kvsig(uint8_t *buf,int32_t len,uint256 privkey) int32_t komodo_kvsigverify(uint8_t *buf,int32_t len,uint256 pubkey,uint256 sig) { - bits256 hash,checksig; - vcalc_sha256(0,hash.bytes,buf,len); - checksig = curve25519_shared(hash,*(bits256 *)&pubkey); - if ( memcmp(&checksig,&sig,sizeof(sig)) != 0 ) - return(-1); - else return(0); + bits256 hash,checksig; static uint256 zeroes; + if ( memcmp(&pubkey,&zeroes,sizeof(pubkey)) != 0 ) + { + vcalc_sha256(0,hash.bytes,buf,len); + checksig = curve25519_shared(hash,*(bits256 *)&pubkey); + if ( memcmp(&checksig,&sig,sizeof(sig)) != 0 ) + return(-1); + } + return(0); } #endif diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 4acdacea4..838c8a963 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -506,6 +506,7 @@ Value kvupdate(const Array& params, bool fHelp) haveprivkey = 0; memset(&sig,0,sizeof(sig)); memset(&privkey,0,sizeof(privkey)); + memset(&refprivkey,0,sizeof(refpubkey)); memset(&pubkey,0,sizeof(pubkey)); if ( (n= (int32_t)params.size()) >= 3 ) { @@ -544,13 +545,12 @@ Value kvupdate(const Array& params, bool fHelp) ret.push_back(Pair("error",(char *)"cant modify write once key without passphrase")); return ret; } - sig = komodo_kvsig(keyvalue,keylen+refvaluesize,privkey); - for (i=0; i<32; i++) - printf("%02x",((uint8_t *)&sig)[i]); - printf(" sig for keylen.%d + valuesize.%d\n",keylen,refvaluesize); } + sig = komodo_kvsig(keyvalue,keylen+refvaluesize,privkey); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&sig)[i]); + printf(" sig for keylen.%d + valuesize.%d\n",keylen,refvaluesize); } - printf("refvaluesize.%d tmpflags.%d\n",refvaluesize,tmpflags); ret.push_back(Pair("coin",(char *)(ASSETCHAINS_SYMBOL[0] == 0 ? "KMD" : ASSETCHAINS_SYMBOL))); height = chainActive.Tip()->nHeight; if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) From 3bd135000243ac52a5c297e2b254d657e04ae34f Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 19:49:53 +0200 Subject: [PATCH 24/43] test --- src/rpcclient.cpp | 2 +- src/wallet/rpcwallet.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rpcclient.cpp b/src/rpcclient.cpp index 933b550a5..5b89b18c4 100644 --- a/src/rpcclient.cpp +++ b/src/rpcclient.cpp @@ -113,7 +113,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "notaries", 1 }, { "minerids", 1 }, { "kvsearch", 1 }, - //{ "kvupdate", 4 }, + { "kvupdate", 4 }, }; class CRPCConvertTable diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 838c8a963..9395ab64c 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -506,7 +506,7 @@ Value kvupdate(const Array& params, bool fHelp) haveprivkey = 0; memset(&sig,0,sizeof(sig)); memset(&privkey,0,sizeof(privkey)); - memset(&refprivkey,0,sizeof(refpubkey)); + memset(&refpubkey,0,sizeof(refpubkey)); memset(&pubkey,0,sizeof(pubkey)); if ( (n= (int32_t)params.size()) >= 3 ) { From 66dc0c54b422f2f0aaa589eeee8632f4a49cc0f5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 20:03:15 +0200 Subject: [PATCH 25/43] test --- src/komodo_curve25519.h | 29 ++++++++++++++++++++++++----- src/wallet/rpcwallet.cpp | 28 +++++++++++++--------------- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/komodo_curve25519.h b/src/komodo_curve25519.h index 8bca4e7fb..906c343a5 100644 --- a/src/komodo_curve25519.h +++ b/src/komodo_curve25519.h @@ -950,23 +950,42 @@ uint256 komodo_kvprivkey(uint256 *pubkeyp,char *passphrase) return(privkey); } -uint256 komodo_kvsig(uint8_t *buf,int32_t len,uint256 privkey) +uint256 komodo_kvsig(uint8_t *buf,int32_t len,uint256 _privkey) { - bits256 sig,hash,otherpub; uint256 usig; + bits256 sig,hash,otherpub,checksig,pubkey,privkey; uint256 usig; + memcpy(&privkey,&_privkey,sizeof(privkey)); vcalc_sha256(0,hash.bytes,buf,len); otherpub = curve25519(hash,curve25519_basepoint9()); - sig = curve25519_shared(*(bits256 *)&privkey,otherpub); + pubkey = curve25519(privkey,curve25519_basepoint9()); + sig = curve25519_shared(privkey,otherpub); + checksig = curve25519_shared(hash,pubkey); + int32_t i; for (i=0; i "); + for (i=0; i "); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&checksig)[i]); + printf(" checksig\n"); memcpy(&usig,&sig,sizeof(usig)); return(usig); } -int32_t komodo_kvsigverify(uint8_t *buf,int32_t len,uint256 pubkey,uint256 sig) +int32_t komodo_kvsigverify(uint8_t *buf,int32_t len,uint256 _pubkey,uint256 sig) { - bits256 hash,checksig; static uint256 zeroes; + bits256 hash,checksig,pubkey; static uint256 zeroes; + memcpy(&pubkey,&_pubkey,sizeof(pubkey)); if ( memcmp(&pubkey,&zeroes,sizeof(pubkey)) != 0 ) { vcalc_sha256(0,hash.bytes,buf,len); checksig = curve25519_shared(hash,*(bits256 *)&pubkey); + int32_t i; for (i=0; i "); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&checksig)[i]); + printf(" sig\n"); if ( memcmp(&checksig,&sig,sizeof(sig)) != 0 ) return(-1); } diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 9395ab64c..4414452a8 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -514,18 +514,16 @@ Value kvupdate(const Array& params, bool fHelp) printf("flags.%d (%s) n.%d\n",flags,params[2].get_str().c_str(),n); } else flags = 0; if ( n >= 4 ) - { - privkey = komodo_kvprivkey(&pubkey,(char *)params[3].get_str().c_str()); - haveprivkey = 1; - flags |= 1; - for (i=0; i<32; i++) - printf("%02x",((uint8_t *)&privkey)[i]); - printf(" priv, "); - for (i=0; i<32; i++) - printf("%02x",((uint8_t *)&pubkey)[i]); - printf(" pubkey, privkey derived from (%s)\n",(char *)params[3].get_str().c_str()); + privkey = komodo_kvprivkey(&pubkey,(char *)(n >= 4 ? params[3].get_str().c_str() : "password")); + haveprivkey = 1; + flags |= 1; + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&privkey)[i]); + printf(" priv, "); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&pubkey)[i]); + printf(" pubkey, privkey derived from (%s)\n",(char *)params[3].get_str().c_str()); //printf("flags.%d (%s)\n",flags,params[2].get_str().c_str()); - } LOCK2(cs_main, pwalletMain->cs_wallet); if ( (keylen= (int32_t)strlen(params[0].get_str().c_str())) > 0 ) { @@ -546,11 +544,11 @@ Value kvupdate(const Array& params, bool fHelp) return ret; } } - sig = komodo_kvsig(keyvalue,keylen+refvaluesize,privkey); - for (i=0; i<32; i++) - printf("%02x",((uint8_t *)&sig)[i]); - printf(" sig for keylen.%d + valuesize.%d\n",keylen,refvaluesize); } + sig = komodo_kvsig(keyvalue,keylen+refvaluesize,privkey); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&sig)[i]); + printf(" sig for keylen.%d + valuesize.%d\n",keylen,refvaluesize); ret.push_back(Pair("coin",(char *)(ASSETCHAINS_SYMBOL[0] == 0 ? "KMD" : ASSETCHAINS_SYMBOL))); height = chainActive.Tip()->nHeight; if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) From 33cdc2e49c049d21c6048540f09d61a27755d483 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 20:14:08 +0200 Subject: [PATCH 26/43] test --- src/komodo_curve25519.h | 2 +- src/komodo_kv.h | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/komodo_curve25519.h b/src/komodo_curve25519.h index 906c343a5..4c11d74d5 100644 --- a/src/komodo_curve25519.h +++ b/src/komodo_curve25519.h @@ -962,7 +962,7 @@ uint256 komodo_kvsig(uint8_t *buf,int32_t len,uint256 _privkey) int32_t i; for (i=0; i "); - for (i=0; i "); for (i=0; i<32; i++) diff --git a/src/komodo_kv.h b/src/komodo_kv.h index 166063a47..b054adc5a 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -116,7 +116,7 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) { if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) { - if ( memcmp(&pubkey,&refpubkey,sizeof(pubkey)) != 0 || komodo_kvsigverify(opretbuf+13,coresize-13,refpubkey,sig) < 0 ) + if ( komodo_kvsigverify(opretbuf+13,coresize-13,refpubkey,sig) < 0 ) { printf("komodo_kvsigverify error [%d]\n",coresize-13); return; @@ -162,7 +162,8 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) ptr->height = height; ptr->flags = flags; portable_mutex_unlock(&KOMODO_KV_mutex); - } else printf("insufficient fee %.8f vs %.8f flags.%d keylen.%d valuesize.%d height.%d (%02x %02x %02x) (%02x %02x %02x)\n",(double)fee/COIN,(double)value/COIN,flags,keylen,valuesize,height,key[0],key[1],key[2],valueptr[0],valueptr[1],valueptr[2]); + } else printf("size mismatch %d vs %d\n",opretlen,coresize); + } else printf("insufficient fee %.8f vs %.8f flags.%d keylen.%d valuesize.%d height.%d (%02x %02x %02x) (%02x %02x %02x)\n",(double)fee/COIN,(double)value/COIN,flags,keylen,valuesize,height,key[0],key[1],key[2],valueptr[0],valueptr[1],valueptr[2]); } else printf("opretlen.%d mismatch keylen.%d valuesize.%d\n",opretlen,keylen,valuesize); } From 161001e50aecf3fa8ffc1185dee3b772cebcdece Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 20:15:34 +0200 Subject: [PATCH 27/43] test --- src/komodo_kv.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/komodo_kv.h b/src/komodo_kv.h index b054adc5a..116714878 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -164,7 +164,6 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) portable_mutex_unlock(&KOMODO_KV_mutex); } else printf("size mismatch %d vs %d\n",opretlen,coresize); } else printf("insufficient fee %.8f vs %.8f flags.%d keylen.%d valuesize.%d height.%d (%02x %02x %02x) (%02x %02x %02x)\n",(double)fee/COIN,(double)value/COIN,flags,keylen,valuesize,height,key[0],key[1],key[2],valueptr[0],valueptr[1],valueptr[2]); - } else printf("opretlen.%d mismatch keylen.%d valuesize.%d\n",opretlen,keylen,valuesize); } #endif From 467666f5b27044f54f8ad9b96570090ec2723159 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 20:20:43 +0200 Subject: [PATCH 28/43] test --- src/komodo_curve25519.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/komodo_curve25519.h b/src/komodo_curve25519.h index 4c11d74d5..70f58137b 100644 --- a/src/komodo_curve25519.h +++ b/src/komodo_curve25519.h @@ -963,11 +963,11 @@ uint256 komodo_kvsig(uint8_t *buf,int32_t len,uint256 _privkey) printf("%02x",buf[i]); printf(" -> "); for (i=0; i<32; i++) - printf("%02x",((uint8_t *)&sig)[i]); + printf("%02x",((uint8_t *)&hash)[i]); printf(" -> "); for (i=0; i<32; i++) - printf("%02x",((uint8_t *)&checksig)[i]); - printf(" checksig\n"); + printf("%02x",((uint8_t *)&pubkey)[i]); + printf(" pubkey\n"); memcpy(&usig,&sig,sizeof(usig)); return(usig); } @@ -979,13 +979,16 @@ int32_t komodo_kvsigverify(uint8_t *buf,int32_t len,uint256 _pubkey,uint256 sig) if ( memcmp(&pubkey,&zeroes,sizeof(pubkey)) != 0 ) { vcalc_sha256(0,hash.bytes,buf,len); - checksig = curve25519_shared(hash,*(bits256 *)&pubkey); + checksig = curve25519_shared(hash,pubkey); int32_t i; for (i=0; i "); for (i=0; i<32; i++) - printf("%02x",((uint8_t *)&checksig)[i]); - printf(" sig\n"); + printf("%02x",((uint8_t *)&hash)[i]); + printf(" -> "); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&pubkey)[i]); + printf(" pubkey\n"); if ( memcmp(&checksig,&sig,sizeof(sig)) != 0 ) return(-1); } From 557917929127202f41b708c9af1e0227fbe1e4bc Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 20:31:26 +0200 Subject: [PATCH 29/43] test --- src/komodo_curve25519.h | 2 +- src/wallet/rpcwallet.cpp | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/komodo_curve25519.h b/src/komodo_curve25519.h index 70f58137b..897cb53c1 100644 --- a/src/komodo_curve25519.h +++ b/src/komodo_curve25519.h @@ -963,7 +963,7 @@ uint256 komodo_kvsig(uint8_t *buf,int32_t len,uint256 _privkey) printf("%02x",buf[i]); printf(" -> "); for (i=0; i<32; i++) - printf("%02x",((uint8_t *)&hash)[i]); + printf("%02x",((uint8_t *)&privkey)[i]); printf(" -> "); for (i=0; i<32; i++) printf("%02x",((uint8_t *)&pubkey)[i]); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 4414452a8..896ca7f4e 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -498,7 +498,7 @@ Value kvupdate(const Array& params, bool fHelp) { static uint256 zeroes; CWalletTx wtx; Object ret; - uint8_t keyvalue[IGUANA_MAXSCRIPTSIZE],opretbuf[IGUANA_MAXSCRIPTSIZE]; int32_t i,coresize,haveprivkey,duration,opretlen,height; uint16_t keylen,valuesize=0,refvaluesize=0; uint8_t *key,*value=0; uint32_t flags,tmpflags,n; struct komodo_kv *ptr; uint64_t fee; uint256 privkey,pubkey,refpubkey,sig; + uint8_t keyvalue[IGUANA_MAXSCRIPTSIZE],opretbuf[IGUANA_MAXSCRIPTSIZE]; int32_t i,coresize,haveprivkey,duration,opretlen,height; uint16_t keylen=0,valuesize=0,refvaluesize=0; uint8_t *key,*value=0; uint32_t flags,tmpflags,n; struct komodo_kv *ptr; uint64_t fee; uint256 privkey,pubkey,refpubkey,sig; if (fHelp || params.size() < 3 ) throw runtime_error("kvupdate key value flags/passphrase"); if (!EnsureWalletIsAvailable(fHelp)) @@ -544,8 +544,9 @@ Value kvupdate(const Array& params, bool fHelp) return ret; } } + if ( keylen+refvaluesize <= sizeof(keyvalue) ) + sig = komodo_kvsig(keyvalue,keylen+refvaluesize,privkey); } - sig = komodo_kvsig(keyvalue,keylen+refvaluesize,privkey); for (i=0; i<32; i++) printf("%02x",((uint8_t *)&sig)[i]); printf(" sig for keylen.%d + valuesize.%d\n",keylen,refvaluesize); From a663f2365b74fb2952e3e839b0bae57be646dcd7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 20:49:08 +0200 Subject: [PATCH 30/43] test --- src/komodo_curve25519.h | 2 +- src/komodo_kv.h | 77 +++++++++++++++++++++++------------------ 2 files changed, 44 insertions(+), 35 deletions(-) diff --git a/src/komodo_curve25519.h b/src/komodo_curve25519.h index 897cb53c1..cc2ce34e4 100644 --- a/src/komodo_curve25519.h +++ b/src/komodo_curve25519.h @@ -988,7 +988,7 @@ int32_t komodo_kvsigverify(uint8_t *buf,int32_t len,uint256 _pubkey,uint256 sig) printf(" -> "); for (i=0; i<32; i++) printf("%02x",((uint8_t *)&pubkey)[i]); - printf(" pubkey\n"); + printf(" verify pubkey\n"); if ( memcmp(&checksig,&sig,sizeof(sig)) != 0 ) return(-1); } diff --git a/src/komodo_kv.h b/src/komodo_kv.h index 116714878..786266938 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -16,40 +16,6 @@ #ifndef H_KOMODOKV_H #define H_KOMODOKV_H -int32_t komodo_kvsearch(uint256 *pubkeyp,int32_t current_height,uint32_t *flagsp,int32_t *heightp,uint8_t value[IGUANA_MAXSCRIPTSIZE],uint8_t *key,int32_t keylen) -{ - struct komodo_kv *ptr; int32_t duration,retval = -1; - *heightp = -1; - *flagsp = 0; - memset(pubkeyp,0,sizeof(*pubkeyp)); - portable_mutex_lock(&KOMODO_KV_mutex); - HASH_FIND(hh,KOMODO_KV,key,keylen,ptr); - if ( ptr != 0 ) - { - duration = ((ptr->flags >> 2) + 1) * KOMODO_KVDURATION; - //printf("duration.%d flags.%d current.%d ht.%d keylen.%d valuesize.%d\n",duration,ptr->flags,current_height,ptr->height,ptr->keylen,ptr->valuesize); - if ( current_height > (ptr->height + duration) ) - { - HASH_DELETE(hh,KOMODO_KV,ptr); - if ( ptr->value != 0 ) - free(ptr->value); - if ( ptr->key != 0 ) - free(ptr->key); - free(ptr); - } - else - { - *heightp = ptr->height; - *flagsp = ptr->flags; - memcpy(pubkeyp,&ptr->pubkey,sizeof(*pubkeyp)); - if ( (retval= ptr->valuesize) != 0 ) - memcpy(value,ptr->value,retval); - } - } - portable_mutex_unlock(&KOMODO_KV_mutex); - return(retval); -} - int32_t komodo_kvcmp(uint8_t *refvalue,uint16_t refvaluesize,uint8_t *value,uint16_t valuesize) { if ( refvalue == 0 && value == 0 ) @@ -83,6 +49,43 @@ uint64_t komodo_kvfee(uint32_t flags,int32_t opretlen,int32_t keylen) return(fee); } +int32_t komodo_kvsearch(uint256 *pubkeyp,int32_t current_height,uint32_t *flagsp,int32_t *heightp,uint8_t value[IGUANA_MAXSCRIPTSIZE],uint8_t *key,int32_t keylen) +{ + struct komodo_kv *ptr; int32_t duration,retval = -1; + *heightp = -1; + *flagsp = 0; + memset(pubkeyp,0,sizeof(*pubkeyp)); + portable_mutex_lock(&KOMODO_KV_mutex); + HASH_FIND(hh,KOMODO_KV,key,keylen,ptr); + if ( ptr != 0 ) + { + duration = komodo_kvduration(ptr->flags); + //printf("duration.%d flags.%d current.%d ht.%d keylen.%d valuesize.%d\n",duration,ptr->flags,current_height,ptr->height,ptr->keylen,ptr->valuesize); + if ( current_height > (ptr->height + duration) ) + { + HASH_DELETE(hh,KOMODO_KV,ptr); + if ( ptr->value != 0 ) + free(ptr->value); + if ( ptr->key != 0 ) + free(ptr->key); + free(ptr); + } + else + { + *heightp = ptr->height; + *flagsp = ptr->flags; + int32_t i; for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&ptr->pubkey)[31-i]); + printf(" ptr->pubkey\n"); + memcpy(pubkeyp,&ptr->pubkey,sizeof(*pubkeyp)); + if ( (retval= ptr->valuesize) != 0 ) + memcpy(value,ptr->value,retval); + } + } + portable_mutex_unlock(&KOMODO_KV_mutex); + return(retval); +} + void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) { static uint256 zeroes; @@ -158,6 +161,12 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) memcpy(ptr->value,valueptr,valuesize); } } + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&ptr->pubkey)[i]); + printf(" <- "); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&pubkey)[i]); + printf(" new pubkey\n"); memcpy(&ptr->pubkey,&pubkey,sizeof(ptr->pubkey)); ptr->height = height; ptr->flags = flags; From 70b7f3eae549b50c0d793af1ab795001689f732f Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 20:55:35 +0200 Subject: [PATCH 31/43] test --- src/komodo_curve25519.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/komodo_curve25519.h b/src/komodo_curve25519.h index cc2ce34e4..ce27ac143 100644 --- a/src/komodo_curve25519.h +++ b/src/komodo_curve25519.h @@ -989,8 +989,15 @@ int32_t komodo_kvsigverify(uint8_t *buf,int32_t len,uint256 _pubkey,uint256 sig) for (i=0; i<32; i++) printf("%02x",((uint8_t *)&pubkey)[i]); printf(" verify pubkey\n"); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&sig)[i]); + printf(" sig vs"); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&checksig)[i]); + printf(" checksig\n"); if ( memcmp(&checksig,&sig,sizeof(sig)) != 0 ) return(-1); + else printf("VALIDATED\n"); } return(0); } From a87d62aefdc9fa88e114d483de46206f844f4b35 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 20:58:38 +0200 Subject: [PATCH 32/43] test --- src/komodo_kv.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_kv.h b/src/komodo_kv.h index 786266938..246f7bdf0 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -89,7 +89,7 @@ int32_t komodo_kvsearch(uint256 *pubkeyp,int32_t current_height,uint32_t *flagsp void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) { static uint256 zeroes; - uint32_t flags; uint256 pubkey,refpubkey,sig; int32_t i,hassig,coresize,haspubkey,height,kvheight; uint16_t keylen,valuesize,newflag = 0; uint8_t *key,*valueptr,valuebuf[IGUANA_MAXSCRIPTSIZE]; struct komodo_kv *ptr; char *transferpubstr,*tstr; uint64_t fee; + uint32_t flags; uint256 pubkey,refpubkey,sig; int32_t i,refvaluesize,hassig,coresize,haspubkey,height,kvheight; uint16_t keylen,valuesize,newflag = 0; uint8_t *key,*valueptr,valuebuf[IGUANA_MAXSCRIPTSIZE]; struct komodo_kv *ptr; char *transferpubstr,*tstr; uint64_t fee; iguana_rwnum(0,&opretbuf[1],sizeof(keylen),&keylen); iguana_rwnum(0,&opretbuf[3],sizeof(valuesize),&valuesize); iguana_rwnum(0,&opretbuf[5],sizeof(height),&height); @@ -115,11 +115,11 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) for (i=0; i<32; i++) ((uint8_t *)&sig)[i] = opretbuf[coresize+sizeof(uint256)+i]; } - if ( komodo_kvsearch((uint256 *)&refpubkey,height,&flags,&kvheight,valuebuf,key,keylen) >= 0 ) + if ( (refvaluesize= komodo_kvsearch((uint256 *)&refpubkey,height,&flags,&kvheight,valuebuf,key,keylen)) >= 0 ) { if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) { - if ( komodo_kvsigverify(opretbuf+13,coresize-13,refpubkey,sig) < 0 ) + if ( komodo_kvsigverify(valuebuf,keylen+refvaluesize,refpubkey,sig) < 0 ) { printf("komodo_kvsigverify error [%d]\n",coresize-13); return; From 31c5d00228ea3bb18183a5131ffddd06651d866b Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 21:02:51 +0200 Subject: [PATCH 33/43] test --- src/komodo_kv.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/komodo_kv.h b/src/komodo_kv.h index 246f7bdf0..a238e2e8e 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -75,7 +75,10 @@ int32_t komodo_kvsearch(uint256 *pubkeyp,int32_t current_height,uint32_t *flagsp *heightp = ptr->height; *flagsp = ptr->flags; int32_t i; for (i=0; i<32; i++) + { printf("%02x",((uint8_t *)&ptr->pubkey)[31-i]); + ((uint8_t *)pubkeyp)[i] = ((uint8_t *)&ptr->pubkey)[31-i]; + } printf(" ptr->pubkey\n"); memcpy(pubkeyp,&ptr->pubkey,sizeof(*pubkeyp)); if ( (retval= ptr->valuesize) != 0 ) From 178eeb4566aa9f4966c680add65c55af5e2f8eae Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 21:06:55 +0200 Subject: [PATCH 34/43] test --- src/komodo_kv.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_kv.h b/src/komodo_kv.h index a238e2e8e..0c4708b60 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -118,7 +118,7 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) for (i=0; i<32; i++) ((uint8_t *)&sig)[i] = opretbuf[coresize+sizeof(uint256)+i]; } - if ( (refvaluesize= komodo_kvsearch((uint256 *)&refpubkey,height,&flags,&kvheight,valuebuf,key,keylen)) >= 0 ) + if ( (refvaluesize= komodo_kvsearch((uint256 *)&refpubkey,height,&flags,&kvheight,&valuebuf[keylen],key,keylen)) >= 0 ) { if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) { From e2843ac1fb80fad8a23b725a7651aa2d3ce141ca Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 21:38:28 +0200 Subject: [PATCH 35/43] test --- src/komodo_kv.h | 1 + src/wallet/rpcwallet.cpp | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/src/komodo_kv.h b/src/komodo_kv.h index 0c4708b60..baa3a470d 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -118,6 +118,7 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) for (i=0; i<32; i++) ((uint8_t *)&sig)[i] = opretbuf[coresize+sizeof(uint256)+i]; } + memcpy(keybuf,key,keylen); if ( (refvaluesize= komodo_kvsearch((uint256 *)&refpubkey,height,&flags,&kvheight,&valuebuf[keylen],key,keylen)) >= 0 ) { if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 896ca7f4e..808c80b4e 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -545,7 +545,12 @@ Value kvupdate(const Array& params, bool fHelp) } } if ( keylen+refvaluesize <= sizeof(keyvalue) ) + { sig = komodo_kvsig(keyvalue,keylen+refvaluesize,privkey); + if ( komodo_kvsigverify(keyvalue,keylen+refvaluesize,refpubkey,sig) < 0 ) + printf("VERIFY ERROR\n"); + else printf("verified immediately\n"); + } } for (i=0; i<32; i++) printf("%02x",((uint8_t *)&sig)[i]); From b884794730024cbdcfbdbde605a134da573a722e Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 21:40:03 +0200 Subject: [PATCH 36/43] test --- src/komodo_kv.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/komodo_kv.h b/src/komodo_kv.h index baa3a470d..4ae4819aa 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -92,7 +92,7 @@ int32_t komodo_kvsearch(uint256 *pubkeyp,int32_t current_height,uint32_t *flagsp void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) { static uint256 zeroes; - uint32_t flags; uint256 pubkey,refpubkey,sig; int32_t i,refvaluesize,hassig,coresize,haspubkey,height,kvheight; uint16_t keylen,valuesize,newflag = 0; uint8_t *key,*valueptr,valuebuf[IGUANA_MAXSCRIPTSIZE]; struct komodo_kv *ptr; char *transferpubstr,*tstr; uint64_t fee; + uint32_t flags; uint256 pubkey,refpubkey,sig; int32_t i,refvaluesize,hassig,coresize,haspubkey,height,kvheight; uint16_t keylen,valuesize,newflag = 0; uint8_t *key,*valueptr,keyvalue[IGUANA_MAXSCRIPTSIZE]; struct komodo_kv *ptr; char *transferpubstr,*tstr; uint64_t fee; iguana_rwnum(0,&opretbuf[1],sizeof(keylen),&keylen); iguana_rwnum(0,&opretbuf[3],sizeof(valuesize),&valuesize); iguana_rwnum(0,&opretbuf[5],sizeof(height),&height); @@ -118,12 +118,12 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) for (i=0; i<32; i++) ((uint8_t *)&sig)[i] = opretbuf[coresize+sizeof(uint256)+i]; } - memcpy(keybuf,key,keylen); - if ( (refvaluesize= komodo_kvsearch((uint256 *)&refpubkey,height,&flags,&kvheight,&valuebuf[keylen],key,keylen)) >= 0 ) + memcpy(keyvalue,key,keylen); + if ( (refvaluesize= komodo_kvsearch((uint256 *)&refpubkey,height,&flags,&kvheight,&keyvalue[keylen],key,keylen)) >= 0 ) { if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) { - if ( komodo_kvsigverify(valuebuf,keylen+refvaluesize,refpubkey,sig) < 0 ) + if ( komodo_kvsigverify(keyvalue,keylen+refvaluesize,refpubkey,sig) < 0 ) { printf("komodo_kvsigverify error [%d]\n",coresize-13); return; From 7112e5a7da0ecdbaf52439ef1370d6527a6ffba0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 21:48:18 +0200 Subject: [PATCH 37/43] test --- src/wallet/rpcwallet.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 808c80b4e..aeac661a7 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -493,6 +493,7 @@ uint64_t komodo_kvfee(uint32_t flags,int32_t opretlen,int32_t keylen); uint256 komodo_kvsig(uint8_t *buf,int32_t len,uint256 privkey); int32_t komodo_kvduration(uint32_t flags); uint256 komodo_kvprivkey(uint256 *pubkeyp,char *passphrase); +int32_t komodo_kvsigverify(uint8_t *buf,int32_t len,uint256 _pubkey,uint256 sig); Value kvupdate(const Array& params, bool fHelp) { From ebeae1ebae09aac5fa91a4bf3998438ba8963255 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 21:56:13 +0200 Subject: [PATCH 38/43] test --- src/komodo_curve25519.h | 10 +++++----- src/komodo_kv.h | 8 ++++---- src/wallet/rpcwallet.cpp | 19 +++++++++++-------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/komodo_curve25519.h b/src/komodo_curve25519.h index ce27ac143..6a0ee3fa6 100644 --- a/src/komodo_curve25519.h +++ b/src/komodo_curve25519.h @@ -959,7 +959,7 @@ uint256 komodo_kvsig(uint8_t *buf,int32_t len,uint256 _privkey) pubkey = curve25519(privkey,curve25519_basepoint9()); sig = curve25519_shared(privkey,otherpub); checksig = curve25519_shared(hash,pubkey); - int32_t i; for (i=0; i "); for (i=0; i<32; i++) @@ -967,7 +967,7 @@ uint256 komodo_kvsig(uint8_t *buf,int32_t len,uint256 _privkey) printf(" -> "); for (i=0; i<32; i++) printf("%02x",((uint8_t *)&pubkey)[i]); - printf(" pubkey\n"); + printf(" pubkey\n");*/ memcpy(&usig,&sig,sizeof(usig)); return(usig); } @@ -980,7 +980,7 @@ int32_t komodo_kvsigverify(uint8_t *buf,int32_t len,uint256 _pubkey,uint256 sig) { vcalc_sha256(0,hash.bytes,buf,len); checksig = curve25519_shared(hash,pubkey); - int32_t i; for (i=0; i "); for (i=0; i<32; i++) @@ -994,10 +994,10 @@ int32_t komodo_kvsigverify(uint8_t *buf,int32_t len,uint256 _pubkey,uint256 sig) printf(" sig vs"); for (i=0; i<32; i++) printf("%02x",((uint8_t *)&checksig)[i]); - printf(" checksig\n"); + printf(" checksig\n");*/ if ( memcmp(&checksig,&sig,sizeof(sig)) != 0 ) return(-1); - else printf("VALIDATED\n"); + //else printf("VALIDATED\n"); } return(0); } diff --git a/src/komodo_kv.h b/src/komodo_kv.h index 4ae4819aa..78b756618 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -76,10 +76,10 @@ int32_t komodo_kvsearch(uint256 *pubkeyp,int32_t current_height,uint32_t *flagsp *flagsp = ptr->flags; int32_t i; for (i=0; i<32; i++) { - printf("%02x",((uint8_t *)&ptr->pubkey)[31-i]); + //printf("%02x",((uint8_t *)&ptr->pubkey)[31-i]); ((uint8_t *)pubkeyp)[i] = ((uint8_t *)&ptr->pubkey)[31-i]; } - printf(" ptr->pubkey\n"); + //printf(" ptr->pubkey\n"); memcpy(pubkeyp,&ptr->pubkey,sizeof(*pubkeyp)); if ( (retval= ptr->valuesize) != 0 ) memcpy(value,ptr->value,retval); @@ -165,12 +165,12 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) memcpy(ptr->value,valueptr,valuesize); } } - for (i=0; i<32; i++) + /*for (i=0; i<32; i++) printf("%02x",((uint8_t *)&ptr->pubkey)[i]); printf(" <- "); for (i=0; i<32; i++) printf("%02x",((uint8_t *)&pubkey)[i]); - printf(" new pubkey\n"); + printf(" new pubkey\n");*/ memcpy(&ptr->pubkey,&pubkey,sizeof(ptr->pubkey)); ptr->height = height; ptr->flags = flags; diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index aeac661a7..151f2c835 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -512,19 +512,19 @@ Value kvupdate(const Array& params, bool fHelp) if ( (n= (int32_t)params.size()) >= 3 ) { flags = atoi(params[2].get_str().c_str()); - printf("flags.%d (%s) n.%d\n",flags,params[2].get_str().c_str(),n); + //printf("flags.%d (%s) n.%d\n",flags,params[2].get_str().c_str(),n); } else flags = 0; if ( n >= 4 ) privkey = komodo_kvprivkey(&pubkey,(char *)(n >= 4 ? params[3].get_str().c_str() : "password")); haveprivkey = 1; flags |= 1; - for (i=0; i<32; i++) + /*for (i=0; i<32; i++) printf("%02x",((uint8_t *)&privkey)[i]); printf(" priv, "); for (i=0; i<32; i++) printf("%02x",((uint8_t *)&pubkey)[i]); printf(" pubkey, privkey derived from (%s)\n",(char *)params[3].get_str().c_str()); - //printf("flags.%d (%s)\n",flags,params[2].get_str().c_str()); + */ LOCK2(cs_main, pwalletMain->cs_wallet); if ( (keylen= (int32_t)strlen(params[0].get_str().c_str())) > 0 ) { @@ -539,7 +539,7 @@ Value kvupdate(const Array& params, bool fHelp) { if ( (tmpflags & KOMODO_KVPROTECTED) != 0 ) { - if ( refpubkey != pubkey ) + if ( memcmp(&refpubkey,&pubkey,sizeof(refpubkey)) != 0 ) { ret.push_back(Pair("error",(char *)"cant modify write once key without passphrase")); return ret; @@ -549,13 +549,16 @@ Value kvupdate(const Array& params, bool fHelp) { sig = komodo_kvsig(keyvalue,keylen+refvaluesize,privkey); if ( komodo_kvsigverify(keyvalue,keylen+refvaluesize,refpubkey,sig) < 0 ) + { + ret.push_back(Pair("error",(char *)"error verifying sig, passphrase is probably wrong")); printf("VERIFY ERROR\n"); - else printf("verified immediately\n"); + return ret; + } // else printf("verified immediately\n"); } } - for (i=0; i<32; i++) - printf("%02x",((uint8_t *)&sig)[i]); - printf(" sig for keylen.%d + valuesize.%d\n",keylen,refvaluesize); + //for (i=0; i<32; i++) + // printf("%02x",((uint8_t *)&sig)[i]); + //printf(" sig for keylen.%d + valuesize.%d\n",keylen,refvaluesize); ret.push_back(Pair("coin",(char *)(ASSETCHAINS_SYMBOL[0] == 0 ? "KMD" : ASSETCHAINS_SYMBOL))); height = chainActive.Tip()->nHeight; if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) From 618dcd5b0229ffa47bf771e917cf2ed85ca59525 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 22:25:41 +0200 Subject: [PATCH 39/43] test --- src/komodo_kv.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_kv.h b/src/komodo_kv.h index 78b756618..99183d5f0 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -125,7 +125,7 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) { if ( komodo_kvsigverify(keyvalue,keylen+refvaluesize,refpubkey,sig) < 0 ) { - printf("komodo_kvsigverify error [%d]\n",coresize-13); + //printf("komodo_kvsigverify error [%d]\n",coresize-13); return; } } @@ -138,9 +138,9 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) { tstr = (char *)"transfer:"; transferpubstr = (char *)&valueptr[strlen(tstr)]; + printf("transfer.(%s) to [%s]? ishex.%d\n",key,transferpubstr,is_hexstr(transferpubstr,0)); if ( strncmp(tstr,(char *)valueptr,strlen(tstr)) == 0 && is_hexstr(transferpubstr,0) == 64 ) { - printf("transfer.(%s) to [%s]\n",key,transferpubstr); for (i=0; i<32; i++) ((uint8_t *)&pubkey)[31-i] = _decode_hex(&transferpubstr[i*2]); } From 190833b941589c0b5a160bbdea9804b1d23946d2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 22:31:16 +0200 Subject: [PATCH 40/43] test --- src/komodo_kv.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_kv.h b/src/komodo_kv.h index 99183d5f0..4dd6eae4f 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -134,7 +134,7 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) HASH_FIND(hh,KOMODO_KV,key,keylen,ptr); if ( ptr != 0 ) { - if ( (ptr->flags & KOMODO_KVPROTECTED) != 0 && memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) + if ( (ptr->flags & KOMODO_KVPROTECTED) != 0 ) { tstr = (char *)"transfer:"; transferpubstr = (char *)&valueptr[strlen(tstr)]; From 83af8cbf35611275d2e51ddaad73d8802ebec8d5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 22:57:49 +0200 Subject: [PATCH 41/43] test --- src/komodo_kv.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_kv.h b/src/komodo_kv.h index 4dd6eae4f..6961bd8e0 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -134,7 +134,7 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) HASH_FIND(hh,KOMODO_KV,key,keylen,ptr); if ( ptr != 0 ) { - if ( (ptr->flags & KOMODO_KVPROTECTED) != 0 ) + //if ( (ptr->flags & KOMODO_KVPROTECTED) != 0 ) { tstr = (char *)"transfer:"; transferpubstr = (char *)&valueptr[strlen(tstr)]; @@ -173,7 +173,7 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) printf(" new pubkey\n");*/ memcpy(&ptr->pubkey,&pubkey,sizeof(ptr->pubkey)); ptr->height = height; - ptr->flags = flags; + ptr->flags = flags | 1; portable_mutex_unlock(&KOMODO_KV_mutex); } else printf("size mismatch %d vs %d\n",opretlen,coresize); } else printf("insufficient fee %.8f vs %.8f flags.%d keylen.%d valuesize.%d height.%d (%02x %02x %02x) (%02x %02x %02x)\n",(double)fee/COIN,(double)value/COIN,flags,keylen,valuesize,height,key[0],key[1],key[2],valueptr[0],valueptr[1],valueptr[2]); From fdab5c731099959bae39a1daf20a5f1c87feea50 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 23:04:23 +0200 Subject: [PATCH 42/43] test --- src/komodo_kv.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_kv.h b/src/komodo_kv.h index 6961bd8e0..30d584634 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -138,9 +138,9 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) { tstr = (char *)"transfer:"; transferpubstr = (char *)&valueptr[strlen(tstr)]; - printf("transfer.(%s) to [%s]? ishex.%d\n",key,transferpubstr,is_hexstr(transferpubstr,0)); if ( strncmp(tstr,(char *)valueptr,strlen(tstr)) == 0 && is_hexstr(transferpubstr,0) == 64 ) { + printf("transfer.(%s) to [%s]? ishex.%d\n",key,transferpubstr,is_hexstr(transferpubstr,0)); for (i=0; i<32; i++) ((uint8_t *)&pubkey)[31-i] = _decode_hex(&transferpubstr[i*2]); } From 579d23744253921fc683d2c65099b76dc3797da4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 23 Jan 2017 15:01:44 +0200 Subject: [PATCH 43/43] test --- src/komodo_kv.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/komodo_kv.h b/src/komodo_kv.h index 30d584634..07adadf2b 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -42,9 +42,11 @@ int32_t komodo_kvduration(uint32_t flags) uint64_t komodo_kvfee(uint32_t flags,int32_t opretlen,int32_t keylen) { - int32_t numdays; uint64_t fee; + int32_t numdays,k; uint64_t fee; + if ( (k= keylen) > 32 ) + k = 32; numdays = komodo_kvnumdays(flags); - if ( (fee= (numdays*(opretlen * opretlen / keylen))) < 100000 ) + if ( (fee= (numdays*(opretlen * opretlen / k))) < 100000 ) fee = 100000; return(fee); }