Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

St fixes #183

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .circleci/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build:
docker build . -f images/ubuntu_1804/Dockerfile -t anyledger/base
docker build . -f images/develop/Dockerfile -t anyledger/develop
25 changes: 20 additions & 5 deletions .circleci/build_zephyr_nrf52840_pca10056.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/bin/bash
set -e
set -x

update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives --install /usr/bin/python python /usr/bin/python3 2
pip install protobuf

Zephyr_RTOS=zephyr_rtos
Zephyr_SDK=zephyr_sdk
PROJECT_DIR=/home/circleci/project

#Reset to the home directoryy
cd ~
Expand All @@ -16,12 +18,25 @@ source ${Zephyr_RTOS}/zephyr/zephyr-env.sh
export ZEPHYR_TOOLCHAIN_VARIANT=zephyr
export ZEPHYR_SDK_INSTALL_DIR=/opt/sdk/zephyr-sdk

# cd to the samples directory
cd ${Zephyr_RTOS}/zephyr/samples/

# clone the repo
git clone /home/circleci/project anyledger-wallet
cd anyledger-wallet
# clone the anyledger repo
if [ -d $PROJECT_DIR ];then
# for CI use directory which contains already cloned source
git clone /home/circleci/project anyledger-wallet
else
git clone -b st_fixes https://github.com/pcppcp/anyledger-wallet.git anyledger-wallet
fi
WALLET_DIR=`pwd`/anyledger-wallet/examples/wallet
BUILD_DIR=$WALLET_DIR/build
mkdir -p $BUILD_DIR

cd examples/wallet/ && mkdir build && cd build/
cmake -DCMAKE_CXX_FLAGS="-Werror $CMAKE_CXX_FLAGS" -DCMAKE_C_FLAGS="-Werror $CMAKE_C_FLAGS" -GNinja -DBOARD=nrf52840_pca10056 -DBUILD_XCOMPILE=1 ../
# build the wallet for x86
cd $BUILD_DIR && mkdir x86 && cd x86
cmake -DCMAKE_CXX_FLAGS="-Werror $CMAKE_CXX_FLAGS" -DCMAKE_C_FLAGS="-Werror $CMAKE_C_FLAGS" -GNinja -DBOARD=qemu_x86 -DBUILD_XCOMPILE=1 $WALLET_DIR
ninja
# build the wallet for nrf52
cd $BUILD_DIR && mkdir nrf52 && cd nrf52
cmake -DCMAKE_CXX_FLAGS="-Werror $CMAKE_CXX_FLAGS" -DCMAKE_C_FLAGS="-Werror $CMAKE_C_FLAGS" -GNinja -DBOARD=nrf52840_pca10056 -DBUILD_XCOMPILE=1 $WALLET_DIR
ninja
4 changes: 2 additions & 2 deletions .circleci/download_solc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

set -e
set -x
SOLC_URL_LINUX='https://github.com/ethereum/solidity/releases/download/v0.4.23/solc-static-linux'
SOLC_VERSION='v0.4.23'
SOLC_VERSION='v0.5.10'
SOLC_URL_LINUX="https://github.com/ethereum/solidity/releases/download/${SOLC_VERSION}/solc-static-linux"

fail() {
if [[ $- == *i* ]]; then
Expand Down
11 changes: 11 additions & 0 deletions .circleci/images/develop/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#
# dockerfile for "developer" image
#
FROM anyledger/base:latest

WORKDIR /home/circleci
ENV HOME /home/circleci

COPY build_zephyr_nrf52840_pca10056.sh /home/circleci
RUN chmod +x build_zephyr_nrf52840_pca10056.sh
RUN ./build_zephyr_nrf52840_pca10056.sh
25 changes: 0 additions & 25 deletions .circleci/images/primary/Dockerfile

This file was deleted.

14 changes: 7 additions & 7 deletions .circleci/images/ubuntu_1804/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#
# this image installs all necessary requirements to build zephyr
#
FROM phusion/baseimage:0.11

MAINTAINER Mohit Agnihotri "[email protected]"

WORKDIR /home/circleci
ENV HOME /home/circleci

Expand All @@ -22,16 +23,15 @@ RUN cd /usr/src/gtest && \

RUN pip3 install --no-cache-dir --upgrade pip

ARG SOLC_URL=https://github.com/ethereum/solidity/releases/download/v0.4.23/solc-static-linux
ARG SOLC_VERSION=v0.4.23
ARG SOLC_VERSION=v0.5.10
ARG SOLC_URL=https://github.com/ethereum/solidity/releases/download/${SOLC_VERSION}/solc-static-linux
RUN curl -L ${SOLC_URL} > /tmp/solc-${SOLC_VERSION} && \
chmod 775 /tmp/solc-${SOLC_VERSION} && \
mv /tmp/solc-${SOLC_VERSION} /usr/local/bin/ && \
ln -s /usr/local/bin/solc-${SOLC_VERSION} /usr/local/bin/solc

#RUN wget http://pyyaml.org/download/pyyaml/PyYAML-3.13.tar.gz && tar -xvf PyYAML-3.13.tar.gz && cd PyYAML-3.13 && python setup.py install

RUN pip3 install --no-cache-dir web3 eth_abi eth_utils click pyaml protobuf
# TODO: this should use real requirements.txt
RUN pip3 install --no-cache-dir web3 eth_utils click pyaml protobuf
RUN ssh-keyscan -H github.com >> /root/.ssh/known_hosts

RUN ls -laF
Expand Down
14 changes: 11 additions & 3 deletions .circleci/install_zephyr_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ set -e
Zephyr_RTOS=zephyr_rtos
Zephyr_SDK=zephyr_sdk

# Clone the Zephyr and install required dependencies
# Clone the Zephyr
mkdir ${Zephyr_RTOS} && cd ${Zephyr_RTOS}
git clone https://github.com/zephyrproject-rtos/zephyr.git
pip3 install -r zephyr/scripts/requirements.txt
cd ../

# until Zephyr fixes http client, we work with this revision
cd zephyr
git checkout bec74121f6

# install requirements
pip3 install -r scripts/requirements.txt

# go back to the source root
cd ../../

# Get the Zephyr SDK
mkdir ${Zephyr_SDK} && cd ${Zephyr_SDK}
Expand Down
2 changes: 1 addition & 1 deletion BUILD-qemu.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ This document describes how to start `anyledger-wallet` project using a QEMU vir

2) build the wallet

`$ mkdir build && cd build && cmake -GNinja -DBOARD=qemu_x86 -DBUILD_XCOMPILE=1 ../ && ninja`
`$ mkdir build && cd build && cmake -GNinja -DBOARD=qemu_x86 -DBUILD_XCOMPILE=1 ../examples/wallet && ninja`

## part three: play with the wallet
1) run the emulator (the wallet prompt should appear immediately)
Expand Down
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ A working `solc` should be available in your `$PATH`. See the [Installation inst

### x86 build

This build is used to develop and test the wallet library on your desktop.
This build is used to develop and test the wallet library on your desktop. It does not include any working application - its purpose is to provide a compileable framework
for unit testing of the individual components on POSIX system. You should always start with this build if you plan to do any changes to the core components.

1) clone wallet repo
`git clone [email protected]:AnyLedger/anyledger-wallet.git && cd anyledger-wallet`

2) create build directory and generate build targets
`mkdir build && cd build && cmake -GNinja ../`
`mkdir build && cd build && cmake -GNinja ../examples/tests`

3) compile the library & run the tests
`ninja && ctest`
Expand All @@ -69,15 +70,29 @@ This build is used to develop and test the wallet library on your desktop.
Prerequisites: a working zephyr SDK (at least v0.9.5), see [here](https://docs.zephyrproject.org/latest/getting_started/getting_started.html) for instructions.
Please make sure you can compile and flash at least blinky example before building the wallet.

Before using the actual hardware, you will probably want to test the functionality using the zephyr's QEMU image. (Use `-DBOARD=x86_qemu` when running `cmake`)

1) clone wallet repo to the zephyr's `samples` directory
`mkdir samples/anyledger/ && git clone [email protected]:AnyLedger/anyledger-wallet.git`

2) create build directory and generate build targets
`cd samples/anyledger/anyledger-wallet/build && cmake -GNinja -DBOARD=nrf52840_pca10056 -DBUILD_XCOMPILE=1 ../`
`mkdir -p samples/anyledger/anyledger-wallet/build && cd samples/anyledger/anyledger-wallet/build && cmake -GNinja -DBOARD=nrf52840_pca10056 -DBUILD_XCOMPILE=1 ../examples/wallet`

3) compile & flash the firmware
3) for hardware builds, compile & flash the firmware
`ninja && ninja flash`

## Using AnyLedger wallet

To start AnyLedger wallet using a QEMU virtual machine, [see here](https://github.com/AnyLedger/anyledger-wallet/blob/master/BUILD-qemu.md).

## Project structure

- `abi_compiler` - python tool for compiling Ethereum ABI definition into a C code
- `eth` - ethereum transaction utilities
- `utils` - string manipulation, JSON en+de-coding, run-length encoding, uint256 manipulators
- `examples` - examples of the actual apps
- `sawtooth` - Hyperledger sawtooth utilities
- `thirdparty` - CMakeFiles for 3rd party modules
- `tools` - random helper scripts
- `wallet` - MacOs compatibility hack
- `zephyr-wallet` - Zephyr-specific code for the AnyLedger wallet
2 changes: 2 additions & 0 deletions cmake/nanopb.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ include(ExternalProject)

set(mylib_build_dir ${CMAKE_CURRENT_BINARY_DIR}/nanopb)
set(mylib_source_dir ${CMAKE_CURRENT_BINARY_DIR}/thirdparty/nanopb)
#set(NANOPB_C_FLAGS "${CMAKE_C_FLAGS} -DPB_SYSTEM_HEADER=\\\"pb_syshdr.h\\\" -I${mylib_source_dir}/extra")
#list(REMOVE_ITEM ${NANOPB_C_FLAGS} "-Werror")

ExternalProject_Add(
nanopb
Expand Down
1 change: 1 addition & 0 deletions cmake/zephyr.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# https://github.com/zephyrproject-rtos/zephyr/blob/master/samples/application_development/external_lib/CMakeLists.txt#L17
set_property(GLOBAL PROPERTY CSTD c11)
include(ExternalProject)
#include($ENV{ZEPHYR_BASE}/cmake/extensions.cmake NO_POLICY_SCOPE)

zephyr_get_include_directories_for_lang_as_string( C includes)
zephyr_get_system_include_directories_for_lang_as_string(C system_includes)
Expand Down
4 changes: 4 additions & 0 deletions eth/address.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ extern "C" {
typedef struct {
uint8_t a[20];
} address_t;

// ETH privkey to address conversion
int privkey_to_ethereum_address(const uint8_t *privkey, address_t *addr_out);

// derive address from a signature (ecrecover)
int address_from_signature(const uint8_t *data, size_t data_len, const signature_t *signature, address_t *addr_out);

#ifdef __cplusplus
Expand Down
13 changes: 12 additions & 1 deletion eth/sign.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,21 @@ typedef struct {
uint8_t v;
} signature_t;

// signature is 32 bytes
// sign a data using provided @privkey
// @param privkey: privkey to use
// @param data: data to sign
// @param data_len: length of the data
// @param signature: signature is stored here. The parameter must have size of at least 65 bytes
int eth_sign(const uint8_t *privkey, const uint8_t *data, size_t data_len, uint8_t *signature);
// create a keccak256 digest of a data
// @param data: data to digest
// @param data_len: length of the data
// @param digest: digest is stored here. The parameter must have size of at least 32 bytes.
int eth_digest_message(const uint8_t *data, size_t data_len, uint8_t *digest);

// sign keccak256(data). the signature parameter must have size of at least 65 bytes.
int eth_sign_data_rawsig(const uint8_t *privkey, const uint8_t *data, size_t data_len, uint8_t *signature);
// sign keccak256(data) and store the signature as r, v, s values
int eth_sign_data(const uint8_t *privkey, const uint8_t *data, size_t data_len, signature_t *out);

int privkey_to_pubkey(const uint8_t *privkey, uint8_t *pubkey);
Expand Down
10 changes: 9 additions & 1 deletion eth/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extern "C" {
#endif
#define SIGNATURE_LENGTH 64

// a generic ethereum transaction
typedef struct {
uint32_t nonce;
uint32_t gas_price;
Expand All @@ -26,20 +27,27 @@ typedef struct {
size_t data_len;
} transaction_t;

// ethereum transaction hash
typedef struct {
uint8_t h[32];
} tx_hash_t;

// basic transaction receipt
typedef struct {
uint64_t blockNumber;
uint8_t status;
} tx_receipt_t;

// set 'to' parameter of the transaction
// @param to_hex: ASCII-encoded address
int tx_set_to(transaction_t *tx, const char *to_hex);
// set 'value' parameter of the transaction (64bit)
// @param val: value
void tx_set_value_u64(transaction_t *tx, uint64_t val);

// encode an ethereum transaction and store result in the buffer
int tx_encode(const transaction_t *tx, const signature_t *sig, uint8_t *buf, size_t buf_size);
// create pre-EIP155 tx
// create pre-EIP155 signed tx
int tx_encode_sign(const transaction_t *tx, const uint8_t *privkey, uint8_t *buf, size_t buf_size);


Expand Down
8 changes: 7 additions & 1 deletion eth/web3.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@
extern "C" {
#endif

// Module that generates JSON-RPC strings for querying the state of an ethereum network

typedef struct {
uint8_t *buf;
size_t buf_size;
size_t buf_used;
uint32_t req_id;
} web3_ctx_t;

// initialize the web3 module
// @param web3: web3 context to use
// @param buf: buffer to use for the outgoing requests
// @param buf_size: buffer size
void web3_init(web3_ctx_t *web3, uint8_t *buf, size_t buf_size);

// what to filter out when assembling tx JSON
// tx flags: what to filter out when assembling tx JSON
#define TX_NO_FROM 1 << 0
#define TX_NO_GAS 1 << 1
#define TX_NO_GASPRICE 1 << 2
Expand Down
4 changes: 4 additions & 0 deletions eth/web3_jsonp.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
extern "C" {
#endif

// JSON-RPC reply decoder

typedef struct {
long code;
const char *message;
Expand All @@ -22,7 +24,9 @@ typedef struct {
jsonrpc_error_t error;
} jsonrpc_result_t;

// decode 'hexencoded' result (i.e. result of a eth_blockNumber call)
int jsonrpc_decode_hexencoded(uint8_t *buf, size_t buf_size, jsonrpc_result_t *result, uint256_t *out);
// decode transaction receipt
int jsonrpc_decode_txreceipt(uint8_t *buf, size_t buf_size, jsonrpc_result_t *result, tx_receipt_t *out);

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion examples/sawtooth-fish-supply/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if(BUILD_XCOMPILE)
file(GLOB APP_SOURCES "app/*.c")
add_subdirectory(payload/)
target_sources(app PRIVATE ${APP_SOURCES})
target_link_libraries(app PUBLIC fish-demo al-zephyr)
target_link_libraries(app PUBLIC fish-demo al-zephyr gcc)
target_include_directories(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
else()
message(STATUS "x86 build")
Expand Down
Loading