jl777
7 years ago
committed by
GitHub
35 changed files with 6340 additions and 348 deletions
@ -0,0 +1,2 @@ |
|||
source userpass |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"autoprice\",\"base\":\"GRS\",\"rel\":\"KMD\",\"margin\":0.05,\"refbase\":\"groestlcoin\",\"refrel\":\"coinmarketcap\"}" |
File diff suppressed because one or more lines are too long
@ -1,3 +0,0 @@ |
|||
coin=DEX |
|||
price=0.0030 |
|||
invprice=333 |
@ -1,18 +1,18 @@ |
|||
#!/bin/bash |
|||
source userpass |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getprice\",\"base\":\"REVS\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getprice\",\"base\":\"CHIPS\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getprice\",\"base\":\"KMD\",\"rel\":\"BTC\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getprice\",\"base\":\"SUPERNET\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getprice\",\"base\":\"CRYPTO\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getprice\",\"base\":\"DEX\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getprice\",\"base\":\"BOTS\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getprice\",\"base\":\"BET\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getprice\",\"base\":\"HODL\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getprice\",\"base\":\"MSHARK\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getprice\",\"base\":\"MGW\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getprice\",\"base\":\"PANGEA\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getprice\",\"base\":\"JUMBLR\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getprice\",\"base\":\"HUSH\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getprice\",\"base\":\"BTCH\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getmyprice\",\"base\":\"REVS\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getmyprice\",\"base\":\"CHIPS\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getmyprice\",\"base\":\"KMD\",\"rel\":\"BTC\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getmyprice\",\"base\":\"SUPERNET\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getmyprice\",\"base\":\"CRYPTO\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getmyprice\",\"base\":\"DEX\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getmyprice\",\"base\":\"BOTS\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getmyprice\",\"base\":\"MGW\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getmyprice\",\"base\":\"BET\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getmyprice\",\"base\":\"PANGEA\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getmyprice\",\"base\":\"HODL\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getmyprice\",\"base\":\"MSHARK\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getmyprice\",\"base\":\"JUMBLR\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getmyprice\",\"base\":\"HUSH\",\"rel\":\"KMD\"}" |
|||
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"getmyprice\",\"base\":\"BTCH\",\"rel\":\"KMD\"}" |
|||
|
|||
|
File diff suppressed because it is too large
@ -0,0 +1,329 @@ |
|||
/* $Id: sph_groestl.h 216 2010-06-08 09:46:57Z tp $ */ |
|||
/**
|
|||
* Groestl interface. This code implements Groestl with the recommended |
|||
* parameters for SHA-3, with outputs of 224, 256, 384 and 512 bits. |
|||
* |
|||
* ==========================(LICENSE BEGIN)============================ |
|||
* |
|||
* Copyright (c) 2007-2010 Projet RNRT SAPHIR |
|||
* |
|||
* Permission is hereby granted, free of charge, to any person obtaining |
|||
* a copy of this software and associated documentation files (the |
|||
* "Software"), to deal in the Software without restriction, including |
|||
* without limitation the rights to use, copy, modify, merge, publish, |
|||
* distribute, sublicense, and/or sell copies of the Software, and to |
|||
* permit persons to whom the Software is furnished to do so, subject to |
|||
* the following conditions: |
|||
* |
|||
* The above copyright notice and this permission notice shall be |
|||
* included in all copies or substantial portions of the Software. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
|||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
|||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|||
* |
|||
* ===========================(LICENSE END)============================= |
|||
* |
|||
* @file sph_groestl.h |
|||
* @author Thomas Pornin <thomas.pornin@cryptolog.com> |
|||
*/ |
|||
|
|||
#ifndef SPH_GROESTL_H__ |
|||
#define SPH_GROESTL_H__ |
|||
|
|||
#ifdef __cplusplus |
|||
extern "C"{ |
|||
#endif |
|||
|
|||
#include <stddef.h> |
|||
#include "sph_types.h" |
|||
|
|||
/**
|
|||
* Output size (in bits) for Groestl-224. |
|||
*/ |
|||
#define SPH_SIZE_groestl224 224 |
|||
|
|||
/**
|
|||
* Output size (in bits) for Groestl-256. |
|||
*/ |
|||
#define SPH_SIZE_groestl256 256 |
|||
|
|||
/**
|
|||
* Output size (in bits) for Groestl-384. |
|||
*/ |
|||
#define SPH_SIZE_groestl384 384 |
|||
|
|||
/**
|
|||
* Output size (in bits) for Groestl-512. |
|||
*/ |
|||
#define SPH_SIZE_groestl512 512 |
|||
|
|||
/**
|
|||
* This structure is a context for Groestl-224 and Groestl-256 computations: |
|||
* it contains the intermediate values and some data from the last |
|||
* entered block. Once a Groestl computation has been performed, the |
|||
* context can be reused for another computation. |
|||
* |
|||
* The contents of this structure are private. A running Groestl |
|||
* computation can be cloned by copying the context (e.g. with a simple |
|||
* <code>memcpy()</code>). |
|||
*/ |
|||
typedef struct { |
|||
#ifndef DOXYGEN_IGNORE |
|||
unsigned char buf[64]; /* first field, for alignment */ |
|||
size_t ptr; |
|||
union { |
|||
#if SPH_64 |
|||
sph_u64 wide[8]; |
|||
#endif |
|||
sph_u32 narrow[16]; |
|||
} state; |
|||
#if SPH_64 |
|||
sph_u64 count; |
|||
#else |
|||
sph_u32 count_high, count_low; |
|||
#endif |
|||
#endif |
|||
} sph_groestl_small_context; |
|||
|
|||
/**
|
|||
* This structure is a context for Groestl-224 computations. It is |
|||
* identical to the common <code>sph_groestl_small_context</code>. |
|||
*/ |
|||
typedef sph_groestl_small_context sph_groestl224_context; |
|||
|
|||
/**
|
|||
* This structure is a context for Groestl-256 computations. It is |
|||
* identical to the common <code>sph_groestl_small_context</code>. |
|||
*/ |
|||
typedef sph_groestl_small_context sph_groestl256_context; |
|||
|
|||
/**
|
|||
* This structure is a context for Groestl-384 and Groestl-512 computations: |
|||
* it contains the intermediate values and some data from the last |
|||
* entered block. Once a Groestl computation has been performed, the |
|||
* context can be reused for another computation. |
|||
* |
|||
* The contents of this structure are private. A running Groestl |
|||
* computation can be cloned by copying the context (e.g. with a simple |
|||
* <code>memcpy()</code>). |
|||
*/ |
|||
typedef struct { |
|||
#ifndef DOXYGEN_IGNORE |
|||
unsigned char buf[128]; /* first field, for alignment */ |
|||
size_t ptr; |
|||
union { |
|||
#if SPH_64 |
|||
sph_u64 wide[16]; |
|||
#endif |
|||
sph_u32 narrow[32]; |
|||
} state; |
|||
#if SPH_64 |
|||
sph_u64 count; |
|||
#else |
|||
sph_u32 count_high, count_low; |
|||
#endif |
|||
#endif |
|||
} sph_groestl_big_context; |
|||
|
|||
/**
|
|||
* This structure is a context for Groestl-384 computations. It is |
|||
* identical to the common <code>sph_groestl_small_context</code>. |
|||
*/ |
|||
typedef sph_groestl_big_context sph_groestl384_context; |
|||
|
|||
/**
|
|||
* This structure is a context for Groestl-512 computations. It is |
|||
* identical to the common <code>sph_groestl_small_context</code>. |
|||
*/ |
|||
typedef sph_groestl_big_context sph_groestl512_context; |
|||
|
|||
/**
|
|||
* Initialize a Groestl-224 context. This process performs no memory allocation. |
|||
* |
|||
* @param cc the Groestl-224 context (pointer to a |
|||
* <code>sph_groestl224_context</code>) |
|||
*/ |
|||
void sph_groestl224_init(void *cc); |
|||
|
|||
/**
|
|||
* Process some data bytes. It is acceptable that <code>len</code> is zero |
|||
* (in which case this function does nothing). |
|||
* |
|||
* @param cc the Groestl-224 context |
|||
* @param data the input data |
|||
* @param len the input data length (in bytes) |
|||
*/ |
|||
void sph_groestl224(void *cc, const void *data, size_t len); |
|||
|
|||
/**
|
|||
* Terminate the current Groestl-224 computation and output the result into |
|||
* the provided buffer. The destination buffer must be wide enough to |
|||
* accomodate the result (28 bytes). The context is automatically |
|||
* reinitialized. |
|||
* |
|||
* @param cc the Groestl-224 context |
|||
* @param dst the destination buffer |
|||
*/ |
|||
void sph_groestl224_close(void *cc, void *dst); |
|||
|
|||
/**
|
|||
* Add a few additional bits (0 to 7) to the current computation, then |
|||
* terminate it and output the result in the provided buffer, which must |
|||
* be wide enough to accomodate the result (28 bytes). If bit number i |
|||
* in <code>ub</code> has value 2^i, then the extra bits are those |
|||
* numbered 7 downto 8-n (this is the big-endian convention at the byte |
|||
* level). The context is automatically reinitialized. |
|||
* |
|||
* @param cc the Groestl-224 context |
|||
* @param ub the extra bits |
|||
* @param n the number of extra bits (0 to 7) |
|||
* @param dst the destination buffer |
|||
*/ |
|||
void sph_groestl224_addbits_and_close( |
|||
void *cc, unsigned ub, unsigned n, void *dst); |
|||
|
|||
/**
|
|||
* Initialize a Groestl-256 context. This process performs no memory allocation. |
|||
* |
|||
* @param cc the Groestl-256 context (pointer to a |
|||
* <code>sph_groestl256_context</code>) |
|||
*/ |
|||
void sph_groestl256_init(void *cc); |
|||
|
|||
/**
|
|||
* Process some data bytes. It is acceptable that <code>len</code> is zero |
|||
* (in which case this function does nothing). |
|||
* |
|||
* @param cc the Groestl-256 context |
|||
* @param data the input data |
|||
* @param len the input data length (in bytes) |
|||
*/ |
|||
void sph_groestl256(void *cc, const void *data, size_t len); |
|||
|
|||
/**
|
|||
* Terminate the current Groestl-256 computation and output the result into |
|||
* the provided buffer. The destination buffer must be wide enough to |
|||
* accomodate the result (32 bytes). The context is automatically |
|||
* reinitialized. |
|||
* |
|||
* @param cc the Groestl-256 context |
|||
* @param dst the destination buffer |
|||
*/ |
|||
void sph_groestl256_close(void *cc, void *dst); |
|||
|
|||
/**
|
|||
* Add a few additional bits (0 to 7) to the current computation, then |
|||
* terminate it and output the result in the provided buffer, which must |
|||
* be wide enough to accomodate the result (32 bytes). If bit number i |
|||
* in <code>ub</code> has value 2^i, then the extra bits are those |
|||
* numbered 7 downto 8-n (this is the big-endian convention at the byte |
|||
* level). The context is automatically reinitialized. |
|||
* |
|||
* @param cc the Groestl-256 context |
|||
* @param ub the extra bits |
|||
* @param n the number of extra bits (0 to 7) |
|||
* @param dst the destination buffer |
|||
*/ |
|||
void sph_groestl256_addbits_and_close( |
|||
void *cc, unsigned ub, unsigned n, void *dst); |
|||
|
|||
/**
|
|||
* Initialize a Groestl-384 context. This process performs no memory allocation. |
|||
* |
|||
* @param cc the Groestl-384 context (pointer to a |
|||
* <code>sph_groestl384_context</code>) |
|||
*/ |
|||
void sph_groestl384_init(void *cc); |
|||
|
|||
/**
|
|||
* Process some data bytes. It is acceptable that <code>len</code> is zero |
|||
* (in which case this function does nothing). |
|||
* |
|||
* @param cc the Groestl-384 context |
|||
* @param data the input data |
|||
* @param len the input data length (in bytes) |
|||
*/ |
|||
void sph_groestl384(void *cc, const void *data, size_t len); |
|||
|
|||
/**
|
|||
* Terminate the current Groestl-384 computation and output the result into |
|||
* the provided buffer. The destination buffer must be wide enough to |
|||
* accomodate the result (48 bytes). The context is automatically |
|||
* reinitialized. |
|||
* |
|||
* @param cc the Groestl-384 context |
|||
* @param dst the destination buffer |
|||
*/ |
|||
void sph_groestl384_close(void *cc, void *dst); |
|||
|
|||
/**
|
|||
* Add a few additional bits (0 to 7) to the current computation, then |
|||
* terminate it and output the result in the provided buffer, which must |
|||
* be wide enough to accomodate the result (48 bytes). If bit number i |
|||
* in <code>ub</code> has value 2^i, then the extra bits are those |
|||
* numbered 7 downto 8-n (this is the big-endian convention at the byte |
|||
* level). The context is automatically reinitialized. |
|||
* |
|||
* @param cc the Groestl-384 context |
|||
* @param ub the extra bits |
|||
* @param n the number of extra bits (0 to 7) |
|||
* @param dst the destination buffer |
|||
*/ |
|||
void sph_groestl384_addbits_and_close( |
|||
void *cc, unsigned ub, unsigned n, void *dst); |
|||
|
|||
/**
|
|||
* Initialize a Groestl-512 context. This process performs no memory allocation. |
|||
* |
|||
* @param cc the Groestl-512 context (pointer to a |
|||
* <code>sph_groestl512_context</code>) |
|||
*/ |
|||
void sph_groestl512_init(void *cc); |
|||
|
|||
/**
|
|||
* Process some data bytes. It is acceptable that <code>len</code> is zero |
|||
* (in which case this function does nothing). |
|||
* |
|||
* @param cc the Groestl-512 context |
|||
* @param data the input data |
|||
* @param len the input data length (in bytes) |
|||
*/ |
|||
void sph_groestl512(void *cc, const void *data, size_t len); |
|||
|
|||
/**
|
|||
* Terminate the current Groestl-512 computation and output the result into |
|||
* the provided buffer. The destination buffer must be wide enough to |
|||
* accomodate the result (64 bytes). The context is automatically |
|||
* reinitialized. |
|||
* |
|||
* @param cc the Groestl-512 context |
|||
* @param dst the destination buffer |
|||
*/ |
|||
void sph_groestl512_close(void *cc, void *dst); |
|||
|
|||
/**
|
|||
* Add a few additional bits (0 to 7) to the current computation, then |
|||
* terminate it and output the result in the provided buffer, which must |
|||
* be wide enough to accomodate the result (64 bytes). If bit number i |
|||
* in <code>ub</code> has value 2^i, then the extra bits are those |
|||
* numbered 7 downto 8-n (this is the big-endian convention at the byte |
|||
* level). The context is automatically reinitialized. |
|||
* |
|||
* @param cc the Groestl-512 context |
|||
* @param ub the extra bits |
|||
* @param n the number of extra bits (0 to 7) |
|||
* @param dst the destination buffer |
|||
*/ |
|||
void sph_groestl512_addbits_and_close( |
|||
void *cc, unsigned ub, unsigned n, void *dst); |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
|
|||
#endif |
@ -1,3 +1,3 @@ |
|||
cd secp256k1; ./m_unix; cd .. |
|||
cd ../crypto777; ./m_LP; cd ../iguana |
|||
gcc -g -o marketmaker -I../crypto777 exchanges/mm.c ../crypto777/cJSON.c mini-gmp.c secp256k1.o ../agents/libcrypto777.a -lnanomsg -lcurl -lpthread -lm |
|||
gcc -g -o marketmaker -I../crypto777 exchanges/mm.c ../crypto777/cJSON.c mini-gmp.c groestl.c segwit_addr.c secp256k1.o ../agents/libcrypto777.a -lnanomsg -lcurl -lpthread -lm |
|||
|
@ -0,0 +1,230 @@ |
|||
/* Copyright (c) 2017 Pieter Wuille
|
|||
* |
|||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
* of this software and associated documentation files (the "Software"), to deal |
|||
* in the Software without restriction, including without limitation the rights |
|||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
* copies of the Software, and to permit persons to whom the Software is |
|||
* furnished to do so, subject to the following conditions: |
|||
* |
|||
* The above copyright notice and this permission notice shall be included in |
|||
* all copies or substantial portions of the Software. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
* THE SOFTWARE. |
|||
*/ |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <stdint.h> |
|||
#include <string.h> |
|||
|
|||
#include "segwit_addr.h" |
|||
#define BECH32_DELIM ':' |
|||
|
|||
/*uint32_t bech32_polymod_step(uint32_t pre) {
|
|||
uint8_t b = pre >> 25; |
|||
return ((pre & 0x1FFFFFF) << 5) ^ |
|||
(-((b >> 0) & 1) & 0x3b6a57b2UL) ^ |
|||
(-((b >> 1) & 1) & 0x26508e6dUL) ^ |
|||
(-((b >> 2) & 1) & 0x1ea119faUL) ^ |
|||
(-((b >> 3) & 1) & 0x3d4233ddUL) ^ |
|||
(-((b >> 4) & 1) & 0x2a1462b3UL); |
|||
}*/ |
|||
|
|||
uint64_t PolyMod_step(uint64_t c,uint8_t d) |
|||
{ |
|||
uint8_t c0 = c >> 35; |
|||
//printf("step (%llx) + %d -> ",(long long)c,d);
|
|||
c = ((c & 0x07ffffffff) << 5) ^ d; |
|||
if (c0 & 0x01) c ^= 0x98f2bc8e61; |
|||
if (c0 & 0x02) c ^= 0x79b76d99e2; |
|||
if (c0 & 0x04) c ^= 0xf33e5fb3c4; |
|||
if (c0 & 0x08) c ^= 0xae2eabe2a8; |
|||
if (c0 & 0x10) c ^= 0x1e4f43e470; |
|||
//printf("%llx\n",(long long)c);
|
|||
return(c); |
|||
} |
|||
|
|||
static const char* charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"; |
|||
|
|||
const int8_t charset_rev[128] = { |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 15, -1, 10, 17, 21, 20, 26, 30, 7, |
|||
5, -1, -1, -1, -1, -1, -1, -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, |
|||
31, 27, 19, -1, 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, |
|||
-1, -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, 1, 0, |
|||
3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1}; |
|||
|
|||
int bech32_encode(char *output,const char *hrp,const uint8_t *data,int32_t data_len) |
|||
{ |
|||
uint64_t chk = 1; size_t i = 0; int32_t ch,chklen = 8; |
|||
while ( hrp[i] != 0 ) |
|||
{ |
|||
ch = hrp[i]; |
|||
if ( ch < 33 || ch > 126 ) |
|||
{ |
|||
printf("bech32_encode illegal ch.%d\n",ch); |
|||
return 0; |
|||
} |
|||
if ( ch >= 'A' && ch <= 'Z' ) |
|||
{ |
|||
printf("bech32_encode illegal uppercase.%c\n",ch); |
|||
return 0; |
|||
} |
|||
i++; |
|||
} |
|||
//printf("bech32_encode after hrp.(%s)\n",hrp);
|
|||
if ( i + chklen + 2 + data_len > 90 ) |
|||
return 0; |
|||
while ( *hrp != 0 ) |
|||
{ |
|||
chk = PolyMod_step(chk,*hrp & 0x1f); |
|||
*(output++) = *(hrp++); |
|||
} |
|||
chk = PolyMod_step(chk,0); |
|||
*(output++) = BECH32_DELIM; |
|||
for (i=0; i<data_len; i++) |
|||
{ |
|||
if ( *data >> 5 ) |
|||
{ |
|||
printf("bech32_encode out of band data.%c\n",*data); |
|||
return 0; |
|||
} |
|||
chk = PolyMod_step(chk,*data); |
|||
*(output++) = charset[*(data++)]; |
|||
} |
|||
for (i = 0; i < chklen; ++i) |
|||
chk = PolyMod_step(chk,0); |
|||
chk ^= 1; |
|||
//printf("bech32_encode emit >>>>>>> ");
|
|||
for (i = 0; i < chklen; ++i) { |
|||
*output = charset[(chk >> ((chklen - 1 - i) * 5)) & 0x1f]; |
|||
//printf("%c",*output);
|
|||
output++; |
|||
} |
|||
*output = 0; |
|||
//printf(" checksum %llx\n",(long long)chk);
|
|||
return 1; |
|||
} |
|||
|
|||
int bech32_decode(char *hrp,uint8_t *data,int32_t *data_len,const char *input) |
|||
{ |
|||
uint64_t chk = 1; int32_t chklen = 8; size_t i,hrp_len,input_len = strlen(input); |
|||
int have_lower = 0, have_upper = 0; |
|||
if ( input_len < 8 || input_len > 90 ) |
|||
{ |
|||
printf("bech32_decode: invalid input_len.%d\n",(int32_t)input_len); |
|||
return 0; |
|||
} |
|||
*data_len = 0; |
|||
while ( *data_len < input_len && input[(input_len - 1) - *data_len] != BECH32_DELIM ) |
|||
++(*data_len); |
|||
hrp_len = input_len - (1 + *data_len); |
|||
if ( hrp_len < 1 || *data_len < chklen ) |
|||
{ |
|||
printf("bech32_decode: invalid hrp_len.%d or datalen.%d\n",(int32_t)hrp_len,(int32_t)*data_len); |
|||
return 0; |
|||
} |
|||
*(data_len) -= chklen; |
|||
for (i=0; i<hrp_len; i++) |
|||
{ |
|||
int ch = input[i]; |
|||
if ( ch < 33 || ch > 126 ) |
|||
{ |
|||
printf("bech32_decode: invalid char.%d\n",ch); |
|||
return 0; |
|||
} |
|||
if ( ch >= 'a' && ch <= 'z' ) |
|||
have_lower = 1; |
|||
else if ( ch >= 'A' && ch <= 'Z' ) |
|||
{ |
|||
have_upper = 1; |
|||
ch = (ch - 'A') + 'a'; |
|||
} |
|||
hrp[i] = ch; |
|||
chk = PolyMod_step(chk,ch & 0x1f); |
|||
} |
|||
hrp[i++] = 0; |
|||
chk = PolyMod_step(chk,0); |
|||
while ( i < input_len ) |
|||
{ |
|||
int v = (input[i] & 0x80) ? -1 : charset_rev[(int)input[i]]; |
|||
if ( input[i] >= 'a' && input[i] <= 'z' ) |
|||
have_lower = 1; |
|||
else if ( input[i] >= 'A' && input[i] <= 'Z' ) |
|||
have_upper = 1; |
|||
if ( v == -1 ) |
|||
{ |
|||
printf("bech32_decode: invalid v.%d from input.[%d] %d\n",(int32_t)v,(int32_t)i,(int32_t)input[i]); |
|||
return 0; |
|||
} |
|||
chk = PolyMod_step(chk,v); |
|||
if (i + chklen < input_len) |
|||
data[i - (1 + hrp_len)] = v; |
|||
++i; |
|||
} |
|||
if ( have_lower && have_upper ) |
|||
{ |
|||
printf("bech32_decode: have_lower.%d have_upper.%d\n",have_lower,have_upper); |
|||
return 0; |
|||
} |
|||
//printf("checksum chk.%llx lower.%d upper.%d inputlen.%d\n",(long long)chk,have_lower,have_upper,(int32_t)input_len);
|
|||
return chk == 1; |
|||
} |
|||
|
|||
int bech32_convert_bits(uint8_t *out,int32_t *outlen,int outbits,const uint8_t *in,int32_t inlen,int inbits,int pad) |
|||
{ |
|||
uint32_t val = 0; |
|||
int bits = 0; |
|||
uint32_t maxv = (((uint32_t)1) << outbits) - 1; |
|||
while (inlen--) { |
|||
val = (val << inbits) | *(in++); |
|||
bits += inbits; |
|||
while (bits >= outbits) { |
|||
bits -= outbits; |
|||
out[(*outlen)++] = (val >> bits) & maxv; |
|||
} |
|||
} |
|||
if (pad) { |
|||
if (bits) { |
|||
out[(*outlen)++] = (val << (outbits - bits)) & maxv; |
|||
} |
|||
} else if (((val << (outbits - bits)) & maxv) || bits >= inbits) { |
|||
return 0; |
|||
} |
|||
return 1; |
|||
} |
|||
|
|||
/*int segwit_addr_encode(char *output, const char *hrp, int witver, const uint8_t *witprog, size_t witprog_len) {
|
|||
uint8_t data[65]; |
|||
size_t datalen = 0; |
|||
if (witver > 16) return 0; |
|||
if (witver == 0 && witprog_len != 20 && witprog_len != 32) return 0; |
|||
if (witprog_len < 2 || witprog_len > 40) return 0; |
|||
data[0] = witver; |
|||
convert_bits(data + 1, &datalen, 5, witprog, witprog_len, 8, 1); |
|||
++datalen; |
|||
return bech32_encode(output, hrp, data, datalen); |
|||
} |
|||
|
|||
int segwit_addr_decode(int* witver, uint8_t* witdata, size_t* witdata_len, const char* hrp, const char* addr) { |
|||
uint8_t data[84]; |
|||
char hrp_actual[84]; |
|||
size_t data_len; |
|||
if (!bech32_decode(hrp_actual, data, &data_len, addr)) return 0; |
|||
if (data_len == 0 || data_len > 65) return 0; |
|||
if (strncmp(hrp, hrp_actual, 84) != 0) return 0; |
|||
if (data[0] > 16) return 0; |
|||
*witdata_len = 0; |
|||
if (!convert_bits(witdata, witdata_len, 8, data + 1, data_len - 1, 5, 0)) return 0; |
|||
if (*witdata_len < 2 || *witdata_len > 40) return 0; |
|||
if (data[0] == 0 && *witdata_len != 20 && *witdata_len != 32) return 0; |
|||
*witver = data[0]; |
|||
return 1; |
|||
}*/ |
@ -0,0 +1,89 @@ |
|||
/* Copyright (c) 2017 Pieter Wuille
|
|||
* |
|||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
* of this software and associated documentation files (the "Software"), to deal |
|||
* in the Software without restriction, including without limitation the rights |
|||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
* copies of the Software, and to permit persons to whom the Software is |
|||
* furnished to do so, subject to the following conditions: |
|||
* |
|||
* The above copyright notice and this permission notice shall be included in |
|||
* all copies or substantial portions of the Software. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
* THE SOFTWARE. |
|||
*/ |
|||
|
|||
#ifndef _SEGWIT_ADDR_H_ |
|||
#define _SEGWIT_ADDR_H_ 1 |
|||
|
|||
#include <stdint.h> |
|||
|
|||
/** Encode a SegWit address
|
|||
* |
|||
* Out: output: Pointer to a buffer of size 73 + strlen(hrp) that will be |
|||
* updated to contain the null-terminated address. |
|||
* In: hrp: Pointer to the null-terminated human readable part to use |
|||
* (chain/network specific). |
|||
* ver: Version of the witness program (between 0 and 16 inclusive). |
|||
* prog: Data bytes for the witness program (between 2 and 40 bytes). |
|||
* prog_len: Number of data bytes in prog. |
|||
* Returns 1 if successful. |
|||
*/ |
|||
int segwit_addr_encode(char *output,const char *hrp,int ver,const uint8_t *prog,int32_t prog_len); |
|||
|
|||
/** Decode a SegWit address
|
|||
* |
|||
* Out: ver: Pointer to an int that will be updated to contain the witness |
|||
* program version (between 0 and 16 inclusive). |
|||
* prog: Pointer to a buffer of size 40 that will be updated to |
|||
* contain the witness program bytes. |
|||
* prog_len: Pointer to a size_t that will be updated to contain the length |
|||
* of bytes in prog. |
|||
* hrp: Pointer to the null-terminated human readable part that is |
|||
* expected (chain/network specific). |
|||
* addr: Pointer to the null-terminated address. |
|||
* Returns 1 if successful. |
|||
*/ |
|||
int segwit_addr_decode(int *ver,uint8_t *prog,int32_t *prog_len,const char *hrp,const char *addr); |
|||
|
|||
/** Encode a Bech32 string
|
|||
* |
|||
* Out: output: Pointer to a buffer of size strlen(hrp) + data_len + 8 that |
|||
* will be updated to contain the null-terminated Bech32 string. |
|||
* In: hrp : Pointer to the null-terminated human readable part. |
|||
* data : Pointer to an array of 5-bit values. |
|||
* data_len: Length of the data array. |
|||
* Returns 1 if successful. |
|||
*/ |
|||
int bech32_encode( |
|||
char *output, |
|||
const char *hrp, |
|||
const uint8_t *data, |
|||
int32_t data_len |
|||
); |
|||
|
|||
/** Decode a Bech32 string
|
|||
* |
|||
* Out: hrp: Pointer to a buffer of size strlen(input) - 6. Will be |
|||
* updated to contain the null-terminated human readable part. |
|||
* data: Pointer to a buffer of size strlen(input) - 8 that will |
|||
* hold the encoded 5-bit data values. |
|||
* data_len: Pointer to a size_t that will be updated to be the number |
|||
* of entries in data. |
|||
* In: input: Pointer to a null-terminated Bech32 string. |
|||
* Returns 1 if succesful. |
|||
*/ |
|||
int bech32_decode( |
|||
char *hrp, |
|||
uint8_t *data, |
|||
int32_t *data_len, |
|||
const char *input |
|||
); |
|||
|
|||
#endif |
File diff suppressed because it is too large
Loading…
Reference in new issue