Skip to content

Instantly share code, notes, and snippets.

@ccoincash
ccoincash / tokenP2SH.js
Created Dec 9, 2020
deploy tokenP2SH contract
View tokenP2SH.js
const {
bsv,
buildContractClass,
getPreimage,
toHex,
num2bin,
SigHashPreimage,
signTx,
PubKey,
Sig,
View sellContract.scrypt
import "util.scrypt";
contract TokenSellContract {
Ripemd160 bsvRecAddr;
int bsvRecAmount;
public function unlock(
SigHashPreimage txPreimage
) {
View backtrace.scrypt
contract Backtrace {
static int DataLen = 46;
static function unpackunsigned(bytes b): int {
return unpack(b + b'00');
}
static function readOutputScript(bytes tx, int outputIndex): bytes {
// first 4 bytes version
// 1 byte input num, only support max 253
@ccoincash
ccoincash / tokenP2SH.scrypt
Created Dec 9, 2020
a token contract support pay to contract
View tokenP2SH.scrypt
import "util.scrypt";
import "backtrace.scrypt";
/**
* A token protocol based on UTXO model
*/
contract Token {
public function split(
SigHashPreimage txPreimage,
@ccoincash
ccoincash / merkleTree2.scrypt
Last active Nov 28, 2020
merkle tree implementation
View merkleTree2.scrypt
contract MerkleTree {
// TODO second preimage attack
static function calculateMerkleRoot(bytes leaf, bytes merklePath) : bytes {
int i = 0;
int merklePathLength = len(merklePath) / 33;
require(merklePathLength <= 5);
bytes merkleValue = leaf;
loop (5) {
if (i < merklePathLength) {
int left = unpack(merklePath[i * 33 + 32 : i * 33 + 33]);
@ccoincash
ccoincash / tokenSwap.js
Created Nov 24, 2020
deploy tokenSwap contract
View tokenSwap.js
const {
bsv,
buildContractClass,
getPreimage,
toHex,
num2bin,
SigHashPreimage,
signTx,
PubKey,
Sig,
@ccoincash
ccoincash / tokenSwap.scrypt
Last active Apr 8, 2021
a token contract support swap pool
View tokenSwap.scrypt
import "util.scrypt";
import "merkleTree2.scrypt";
// the token swap has two merkle trees to save data. one for token user, one for pool and the pool token balance
contract TokenSwap {
// the contract init bsv amount
int initBsv;
// the swap pool first liquidity require amount
int firstSaveBsvAmount;
@ccoincash
ccoincash / binaryotption.js
Created Nov 6, 2020
deploy the binaryOption contract to the testnet
View binaryotption.js
const { bsv, buildContractClass, getPreimage, toHex, Ripemd160, num2bin, Bytes, SigHashPreimage } = require('scryptlib');
const { DataLen, loadDesc, createUnlockingTx, createLockingTx, sendTx, showError } = require('../helper');
const {
privateKey,
privateKey2
} = require('../privateKey');
(async() => {
@ccoincash
ccoincash / binaryoption.scrypt
Last active Nov 6, 2020
implement binary option contract using oracle data in bsv blockchain
View binaryoption.scrypt
import "util.scrypt";
contract BinaryOption {
int betPrice;
int rabinPubKey;
int timestamp;
Ripemd160 pubKeyHashA;
Ripemd160 pubKeyHashB;
function hash(bytes x): bytes {