Skip to content

Instantly share code, notes, and snippets.

@sogoiii
sogoiii / extract_signature_params_snippet.js
Created June 9, 2017 06:04
Given a signature, extract the r, s, and v values.
signature = signature.substr(2); //remove 0x
const r = '0x' + signature.slice(0, 64)
const s = '0x' + signature.slice(64, 128)
const v = '0x' + signature.slice(128, 130)
const v_decimal = web3.toDecimal(v)
@sogoiii
sogoiii / genSignature.js
Created June 9, 2017 05:55
Create Ethereum Signatures
const Web3 = require('web3')
const provider = new Web3.providers.HttpProvider('http://localhost:8545')
const web3 = new Web3(provider)
function toHex(str) {
var hex = ''
for(var i=0;i<str.length;i++) {
hex += ''+str.charCodeAt(i).toString(16)
}
return hex
@sogoiii
sogoiii / ListenToEventsWeb3-1-0-0-beta.js
Last active December 26, 2023 23:00
Listening to Solidity events using web3 1.0.0 beta
const Web3 = require('web3') // Works with web3 1.0.0-beta27
const contractArtifact = require('./build/contracts/TutorialToken.json')
const web3 = new Web3()
const providerUrl = 'ws://localhost:8545' // requires # https://github.com/trufflesuite/ganache-cli/releases/tag/v7.0.0-beta.0 or https://github.com/trufflesuite/ganache/releases/tag/v1.1.0-beta.0
const provider = new Web3.providers.WebsocketProvider(providerUrl)
web3.setProvider(provider)
web3.eth.net.getId()
.then(networkId => {
@sogoiii
sogoiii / Verifier.sol
Last active December 26, 2023 22:53
A Solidity contract that verifies signatures
pragma solidity ^0.4.8;
contract Verifier {
function recoverAddr(bytes32 msgHash, uint8 v, bytes32 r, bytes32 s) returns (address) {
return ecrecover(msgHash, v, r, s);
}
function isSigned(address _addr, bytes32 msgHash, uint8 v, bytes32 r, bytes32 s) returns (bool) {
return ecrecover(msgHash, v, r, s) == _addr;
}
}
@sogoiii
sogoiii / verify_signature.js
Last active December 26, 2023 22:52
Validate a signature in javascript using solidity
//...
const SignVerifyArtifact = require('./contracts/SignAndVerifyExample')
const SignVerify = contract(SignVerifyArtifact)
SignVerify.setProvider(provider)
//...
SignVerify
.deployed()
.then(instance => {
let fixed_msg = `\x19Ethereum Signed Message:\n${msg.length}${msg}`
let fixed_msg_sha = web3.sha3(fixed_msg)
@sogoiii
sogoiii / ListenToEventsWeb3-0-20-4.js
Last active July 31, 2023 06:53
Listening to web3 events in version 0.20.4 and below
const Web3 = require('web3') // Works with web3 0.20.4
const contractArtifact = require('./build/contracts/TutorialToken.json')
const web3 = new Web3()
const providerUrl = 'http://localhost:8545'
const provider = new Web3.providers.HttpProvider(providerUrl)
web3.setProvider(provider)
const networkId = web3.version.network
const contractAddr = contractArtifact.networks[networkId].address
@sogoiii
sogoiii / ListenToEventsTruffleAnyWeb3.js
Last active July 31, 2023 06:52
Example on how to use truffle to listen to events in your smart contracts
const Web3 = require('web3') // Web3 0.20.4 or web3 1 beta
const truffleContract = require("truffle-contract")
const contractArtifact = require('./build/contracts/TutorialToken.json')
const providerUrl = 'http://localhost:8545'
const provider = new Web3.providers.HttpProvider(providerUrl)
const contract = truffleContract(contractArtifact)
contract.setProvider(provider)
@sogoiii
sogoiii / index.html
Created July 18, 2017 21:07
Browser to IPFS example
<!DOCTYPE html>
<html>
<head>
<title>JavaScript file upload</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="https://wzrd.in/standalone/buffer"></script>
<script src="https://unpkg.com/ipfs-api@9.0.0/dist/index.js"
integrity="sha384-5bXRcW9kyxxnSMbOoHzraqa7Z0PQWIao+cgeg327zit1hz5LZCEbIMx/LWKPReuB"
crossorigin="anonymous"></script>
</head>
pragma solidity ^0.4.18;
contract D {
uint public n;
address public sender;
function callSetN(address _e, uint _n) public {
_e.call(bytes4(keccak256("setN(uint256)")), _n); // E's storage is set, D is not modified
}
function callcodeSetN(address _e, uint _n) public {
@sogoiii
sogoiii / ethereum-to-graphql-server.js
Last active July 23, 2018 21:16
Server that can be used with the ethereum-to-graphql package
const express = require('express')
const graphqlHTTP = require('express-graphql')
const Web3 = require('web3')
const TFcontract = require('truffle-contract')
const MetaCoinArtifact = require('./build/contracts/Metacoin')
const MetCoinContract = TFcontract(MetaCoinArtifact)
MetCoinContract.setProvider(new Web3.providers.HttpProvider('http://localhost:8545'))
const { genGraphQlProperties } = require('ethereum-to-graphql')