Skip to content

Instantly share code, notes, and snippets.

View chiro-hiro's full-sized avatar
🤖
Bleep bloop, I am a robot. Eh, just kidding.

Chiro Hiro chiro-hiro

🤖
Bleep bloop, I am a robot. Eh, just kidding.
View GitHub Profile
@chiro-hiro
chiro-hiro / xorshift.md
Last active September 13, 2017 06:23
XorShift based PRNG
//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 September 13, 2017 06:27
Bip39 triple duplicate

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 October 18, 2017 05:24
Basic ERC20 Token
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 October 18, 2017 05:24
Create and sign raw transaction
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 October 18, 2017 05:31
PoC: Send ERC20 token
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 August 20, 2023 16:41
Unsigned 32 bits IP address to string
/*
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 March 14, 2018 12:21
PartTime smart contract

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 March 21, 2018 05:29
Partime smart contract and test case

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 11:07
Part time part 3/chap 6
    //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 12:30
Part time part 4/chap 6

Đ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