Skip to content

Instantly share code, notes, and snippets.

@rjl493456442
Last active December 3, 2019 06:36
Show Gist options
  • Select an option

  • Save rjl493456442/7a1127de974a49792588b6f42fb77bc0 to your computer and use it in GitHub Desktop.

Select an option

Save rjl493456442/7a1127de974a49792588b6f42fb77bc0 to your computer and use it in GitHub Desktop.

Payment channel instructions

Initiailize your clef configurations

Initialization

note: you need to create 2 separate config directories for both client and server.

clef init --configdir <config_path>

generate accounts for payment

The generated key file is located at your default keystore path.

geth account new

allocate some test ethers

https://goerli-faucet.slock.it/

attest scripts for automatic signing

  • Script for server
// The rules for listing accounts
function ApproveListing(req){
    return "Approve"
}

// The rules for signing transactions
function ApproveTx(req) {
    return "Approve"
}

// The rules for printing banner.
function OnSignerStartup(req) {
    return "Approve"
}

calculate sha256 hash

$ shasum -a 256 server.js
12762fc75de9c88ec6a87f61def2923eaf5164539992df4f2101ac82f079c923  server.js

attest it

clef attest <hash> --configdir <config_dir_server>
  • Script for client
// The rules for listing accounts
function ApproveListing(req) {
    return "Approve"
}

function big(str) {
	if (str.slice(0, 2) == "0x") {
		return new BigNumber(str.slice(2), 16)
	}
	return new BigNumber(str)
}

// Only 5e-2 ethers are allowed to spend in 1 week time.
var window = 1000*3600*7;
var limit = new BigNumber("5e16");

function isLimitOk(transaction) {
	var value = big(transaction.value)
	var windowstart = new Date().getTime() - window;

	var txs = [];
	var stored = storage.get('txs');

	if (stored != "") {
		txs = JSON.parse(stored)
	}
	// First, remove all that have passed out of the time-window
	var newtxs = txs.filter(function(tx){return tx.tstamp > windowstart});
	// Secondly, aggregate the current sum
	sum = new BigNumber(0)
	sum = newtxs.reduce(function(agg, tx){ return big(tx.value).plus(agg)}, sum);
	// Would we exceed weekly limit ?
	return sum.plus(value).lt(limit)
}

// The rules for sigining transactions
function ApproveTx(r) {
    if (isLimitOk(r.transaction)) {
        return "Approve"
    }
    return "Reject"
}

// OnApprovedTx(str) is called when a transaction has been approved and signed.
function OnApprovedTx(resp) {
    var value = big(resp.tx.value)
	var txs = []
	// Load stored transactions
	var stored = storage.get('txs');
	if (stored != "") {
		txs = JSON.parse(stored)
	}
	// Add this to the storage
	txs.push({tstamp: new Date().getTime(), value: value});
	storage.put("txs", JSON.stringify(txs));
}

// The rules for sigining cheques
function ApproveSignData(r) {
    return "Approve"
}

// The rules for printing banner.
function OnSignerStartup(i) {
    return "Approve"
}

calculate sha256 hash

$ shasum -a 256 client.js
1c43a4e14f7999079f0e04d636a37aebfb8b520ebf281e19e213cdd4d371439b  client.js

attest it

clef attest <hash> --configdir <config_dir_client>

SetPassword

clef setpw --configdir ~/Library/Ethereum/signers/client_config <account>

clef setpw --configdir ~/Library/Ethereum/signers/client_config <account>

Start clef daemon for both server and client

clef --configdir ~/Library/Ethereum/signers/server_config --rpc --chainid 5 --rules ~/Library/Ethereum/signers/rules/server.js

clef --configdir ~/Library/Ethereum/signers/client_config --rpc --chainid 5 --rpcport 8552 --rules ~/Library/Ethereum/signers/rules/client.js

Run geth

geth --goerli --light.charge --lightserv 100 --light.address 0x43c4c8759c66890688fe8321e47dca9266e36b39 --signer http://localhost:8550

geth --goerli --syncmode light --datadir ~/Library/Ethereum/goerli-light --port 30304 --light.pay --light.address 0x2F3B6B4c1c9A8ed152A5c48C65Cd044B7e10468d --signer http://localhost:8552

What's more

  • les.openedChannel: returns all opened channel contract addresses
  • les.channelInfo(<channelAddress>): return info of the specified channel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment