Coder Social home page Coder Social logo

furkanahmetk / blockchain-authenticator-contract Goto Github PK

View Code? Open in Web Editor NEW

This project forked from caspercommunityio/blockchain-authenticator-contract

0.0 1.0 0.0 20 KB

Smart contract to stored list of string, written in Rust for the Casper's blockchain

License: MIT License

Rust 96.98% Makefile 3.02%

blockchain-authenticator-contract's Introduction

About Blockchain Authenticator Contract

Provide a simple smart contract written in Rust to store a list of String.
This contract is used by the Blockchain Authenticator App to store the secret keys needed to generate the OTP password.

The contract must be called with 3 parameters :

  • named-key : Name of the property where the data is stored
  • method :
    • add : Add the "keys" to the current list of string
    • del : Remove the "keys" from the current list of string
    • dellall : Remove all elements from the list of string
  • keys : List of strings

How to install

First, be sure that you have the needed tools installed.

You can check the Casper's documentation here

Here are the version of the tools that we've used to build and run the smartcontract :

rustup --version
rustup 1.24.3 (ce5817a94 2021-05-31)

cmake --version
cmake version 3.22.1

You also need Git to clone the repository.

To build the smartcontract :

git clone [email protected]:caspercommunityio/blockchain-authenticator-contract.git
cd blockchain-authenticator-contract
make prepare
make build-contract

How to run the tests

Follow the "how to install" part and then :

cd blockchain-authenticator-contract
make test

The output should be something like this :

make test
cd contract && cargo build --release --target wasm32-unknown-unknown
    Finished release [optimized] target(s) in 0.03s
wasm-strip contract/target/wasm32-unknown-unknown/release/contract.wasm 2>/dev/null | true
mkdir -p tests/wasm
cp contract/target/wasm32-unknown-unknown/release/contract.wasm tests/wasm
cd tests && cargo test
    Finished test [unoptimized + debuginfo] target(s) in 0.06s
     Running unittests (target/debug/deps/integration_tests-5924344271c8fdee)

running 8 tests
test tests::should_create_named_keys ... ok
test tests::should_panic_missing_parameters - should panic ... ok
test tests::should_add_multiple_elements ... ok
test tests::should_add_non_existing_element ... ok
test tests::should_not_del_element_non_existing_element ... ok
test tests::should_del_element ... ok
test tests::should_del_multiple_elements ... ok
test tests::should_update_existing_element ... ok

test result: ok. 8 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.28s

How to test on testnet

Install the "casper-client" using cargo :

cargo +nightly-2022-02-03 install casper-client --locked

Check that the "casper-client" is correctly installed :

casper-client --version
Casper client 1.5.0

Create a new account with Casper Signer and claim tokens using the faucet on https://testnet.cspr.live.

Then download your .pem file using the casper signer.

To deploy it on testnet, we have to use the following command :

casper-client put-deploy --chain-name casper-test -n http://95.216.67.162:7777 -k /path/to/your/secret/keys/private.pem -p 1000000000 -s /path/to/your/project/contract/target/wasm32-unknown-unknown/release/contract.wasm --session-args-complex /path/to/your/args.txt

As you can see, we use a file to store the args because the type CLList is considered complex.

The content of the file should be like this :

[
 {
	"name" : "method",
	"value" : {
		"raw_bytes" : "07000000030000006164640a"
	}
 },{
	"name" : "keys",
	"value" : {
		"raw_bytes" : "1100000001000000090000004944313b56414c55450e0a"
	}
 },{
	"name" : "named-key",
	"value" : {
		"raw_bytes" : "0800000004000000746573740a"
	}
 }
]

The values are encoded like specified in the Casper's documentation : https://docs.casperlabs.io/design/serialization-standard/#clvalue-clvalue

Here are some raw_bytes values for the parameters :

String "test" for the named-key parameter => 0800000004000000746573740a
String "add" => 07000000030000006164640a
String "del" => 070000000300000064656c0a
List of 1 Element "ID1;VALUE" => 1100000001000000090000004944313b56414c55450e0a
List of 2 Elements "ID1;VALUE, ID2;VALUE" => 1e00000002000000090000004944313b56414c5545090000004944323b56414c55450e0a
List of 3 Elements "ID1;VALUE, ID2;VALUE, ID3;VALUE" => 2b00000003000000090000004944313b56414c5545090000004944323b56414c5545090000004944333b56414c55450e0a

With these examples, you can try different tests scenarios on testnet.

The result of the deploy should be like this :

{
  "id": -6078809224594666102,
  "jsonrpc": "2.0",
  "result": {
    "api_version": "1.4.6",
    "deploy_hash": "c60402ece049fb41f60ae67bedeec4c0bd71b7fb0cec0d3267fd030eededa71e"
  }
}

You can check the result of the deploy on https://testnet.cspr.live.

How to write the contract for another blockchain

The smart contract must store an array of string with the following format "ID;VALUE". Then when you call the smartcontract, you must have at least 2 parameters :

  • method :
    • add : Add the "keys" to the current list of string
    • del : Remove the "keys" from the current list of string
    • dellall : Remove all elements from the list of string
  • keys : List of strings
  • named-key (optional) : Name of the property where the data is stored

Then the webapp must be adapted to call and retrieve the data from that blockchain.

License

The Blockchain Authenticator Contract package is an open-sourced software licensed under the MIT license.

blockchain-authenticator-contract's People

Contributors

bilil avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.