Skip to content

Instantly share code, notes, and snippets.

@ccoincash
ccoincash / tokenP2SH.js
Created December 9, 2020 09:13
deploy tokenP2SH contract
const {
bsv,
buildContractClass,
getPreimage,
toHex,
num2bin,
SigHashPreimage,
signTx,
PubKey,
Sig,
@ccoincash
ccoincash / sellContract.scrypt
Created December 9, 2020 09:10
sell token for bsv
import "util.scrypt";
contract TokenSellContract {
Ripemd160 bsvRecAddr;
int bsvRecAmount;
public function unlock(
SigHashPreimage txPreimage
) {
@ccoincash
ccoincash / backtrace.scrypt
Created December 9, 2020 09:06
a helper 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 December 9, 2020 09:03
a token contract support pay to contract
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 November 28, 2020 12:02
merkle tree implementation
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 November 24, 2020 09:55
deploy tokenSwap contract
const {
bsv,
buildContractClass,
getPreimage,
toHex,
num2bin,
SigHashPreimage,
signTx,
PubKey,
Sig,
@ccoincash
ccoincash / tokenSwap.scrypt
Last active April 8, 2021 02:45
a token contract support swap pool
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 November 6, 2020 07:34
deploy the binaryOption contract to the testnet
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 November 6, 2020 07:41
implement binary option contract using oracle data in bsv blockchain
import "util.scrypt";
contract BinaryOption {
int betPrice;
int rabinPubKey;
int timestamp;
Ripemd160 pubKeyHashA;
Ripemd160 pubKeyHashB;
function hash(bytes x): bytes {