Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:
Chiro ᴴᶦʳᵒ

Chiro Hiro chiro-hiro

:octocat:
Chiro ᴴᶦʳᵒ
View GitHub Profile
@chiro-hiro
chiro-hiro / xorshift.md
Last active Sep 13, 2017
XorShift based PRNG
View xorshift.md
//Generate seed from Math.random()
var seed = Math.random()*Math.pow(2,32);

//XorShift based PRNG
function rnd(){
    var x = 0, i;
    for(i = 0; i < 32; i++){
        //Shift right if [i th] bit is equal to 1, otherwise shift letf
        x ^= ((seed >> i) & 1) ? seed >> i : seed << i;
@chiro-hiro
chiro-hiro / bip39.md
Last active Sep 13, 2017
Bip39 triple duplicate
View bip39.md

Our testor (Ms. Phương) talk to me about, duplication of passphrase will become invalid passphrase in our wallet.

Look at BIP39 there are no duplication check. So I give it a try.

Create bip39.js

var bip39 = require('bip39')

function scanDuplicated(value){
    let buff = value.split(' ');
@chiro-hiro
chiro-hiro / basic-token.md
Last active Oct 18, 2017
Basic ERC20 Token
View basic-token.md
pragma solidity ^0.4.11;

contract BasicToken {

  mapping(address => uint256) balances;

  function BasicToken(){
    balances[address(0x005239d2bec5c8ee929ec27944cffc7b2667710b51)] = 1000;
  }
@chiro-hiro
chiro-hiro / raw-tranasction.md
Created Oct 18, 2017
Create and sign raw transaction
View raw-tranasction.md
var BasicToken = artifacts.require('./BasicToken.sol');
const EthereumTx = require('../node_modules/ethereumjs-tx')
const userAccount = '0x5239d2bec5c8ee929ec27944cffc7b2667710b51';

//Create transaction
const txParams = {
    nonce: web3.eth.getTransactionCount(userAccount),
    from: userAccount,
    gasPrice: '0x6FC23AC00',
@chiro-hiro
chiro-hiro / erc20-poc.md
Last active Oct 18, 2017
PoC: Send ERC20 token
View erc20-poc.md
var BasicToken = artifacts.require('./BasicToken.sol');
const EthereumTx = require('../node_modules/ethereumjs-tx')
const userAccount = '0x5239d2bec5c8ee929ec27944cffc7b2667710b51';

contract('BasicToken', function (accounts) {

Constructor will put 1000 token in account 0x5239d2bec5c8ee929ec27944cffc7b2667710b51 which was shown balances[address(0x005239d2bec5c8ee929ec27944cffc7b2667710b51)] = 1000;

@chiro-hiro
chiro-hiro / ip2str.cpp
Last active Feb 14, 2018
Unsigned 32 bits IP address to string
View ip2str.cpp
/*
g++ -std=c++11 ip2str.cpp -o ip2str
ip2str 0xb01a8c0 0x100007f
*/
#include <sstream>
#include <iostream>
using namespace std;
/* Simplest version */
@chiro-hiro
chiro-hiro / partime.md
Last active Mar 14, 2018
PartTime smart contract
View partime.md

Toàn bộ smart contract như sau

pragma solidity ^0.4.17;

contract PartTime {
  
  struct Job {
    uint256 id;
    address creator;
    uint256 salary;
@chiro-hiro
chiro-hiro / partime-2.md
Created Mar 21, 2018
Partime smart contract and test case
View partime-2.md

Mình viết lại smart contract, thêm hai function mới takeJob()viewJob():

    //Take job
    function
    takeJob (uint256 jobId)
    public onlyValidMortgage(jobId) onlyValidId(jobId) onlyValidJob(jobId)
    {
        //Trigger event to log labor
        TakeJob(jobId, msg.sender);
@chiro-hiro
chiro-hiro / part-time-3.md
Created May 26, 2018
Part time part 3/chap 6
View part-time-3.md
    //Mapped data
    mapping (uint256 => Job) public jobData;
    
    //Transaction must contant Ethereum
    modifier onlyHaveFund {
        require(msg.value > MINIUM_SALARY);
        _;
    }
@chiro-hiro
chiro-hiro / part-time-4.md
Last active May 26, 2018
Part time part 4/chap 6
View part-time-4.md

Đoạn code bên dưới sẽ giúp Ethereum không bị trapped trong smart contract khi có người dùng vô tình gửi Ethereum vào.

    //We don't let any trapped in this contract
    function () public payable {
        revert();
    }

Từ Solidity 0.4.21 thì việc dùng constructor sẽ thay thế cho việc sử dụng function có cùng tên với smart contract.

    //Empty constructor
You can’t perform that action at this time.