Skip to content

Instantly share code, notes, and snippets.

const webcrypto = require('crypto').webcrypto.subtle;
const enc = new TextEncoder();
function bytesToBase64url(bytes) {
return Buffer.from(bytes).toString('base64url');
}
function jsonToBase64url(obj) {
const bytes = enc.encode(JSON.stringify(obj));
@Lohann
Lohann / UnidirecionalPaymentChannel.sol
Last active July 24, 2022 14:57
Example of Unidirecional Payment Channel in solidty
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
library ECDSA {
/**
* @notice Recovers the address for an ECDSA signature and message hash,
* note that the hash must be prefixed with "\x19Ethereum Signed Message:\n{msg.length}"
* @return address The address that was used to sign the message
*/
@Lohann
Lohann / Lottery.sol
Last active July 10, 2022 15:15
Example of a simple lottery smart-contract written in solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Lottery {
// PRICE PER TICKET
uint256 constant TICKET_PRICE = (10 ** 18) / 100; // 0.01eth
uint256 constant TICKET_OPTIONS = 30;
uint256 constant OPTIONS_PER_TICKET = 3;
@Lohann
Lohann / Serialization.sol
Created June 30, 2022 14:29
Solidity encode/decode
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
library Vote {
uint256 constant BITS_PER_CANDIDATE = 5;
uint256 constant BITMASK = (2**BITS_PER_CANDIDATE) - 1;
// Storage Layout:
// Voter (160bit) | size (5bit) | votes (5~90bit)