Skip to content

Commit

Permalink
pump scrypt-ts-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
zhfnjust committed Feb 24, 2024
1 parent 4e0f3b5 commit 163b053
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 37 deletions.
78 changes: 71 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"paillier-bigint": "^3.4.3",
"scrypt-ord": "^1.0.16",
"scrypt-ts": "^1.3.25",
"scrypt-ts-lib": "^0.1.24"
"scrypt-ts-lib": "^0.1.27"
},
"devDependencies": {
"@types/chai": "^4.3.4",
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/blockPRNG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class BlockchainPRNG extends SmartContract {
assert(Blockchain.isValidBlockHeader(bh, this.blockchainTarget))

// verify previous transaction in the block
assert(Blockchain.txInBlock(prevTxid, bh, merkleproof))
assert(Blockchain.txInBlock(prevTxid, bh, merkleproof, 32))

// use block header's nonce last bit as apseudo-random number
const winner: PubKey = bh.nonce % 2n ? this.alice : this.bob
Expand Down
4 changes: 2 additions & 2 deletions src/contracts/bsv20LendingPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class Bsv20LendingPool extends BSV20V2 {
'BH does not meet min target'
)
assert(
Blockchain.txInBlock(prevTxid, blockHeader, merkleProof),
Blockchain.txInBlock(prevTxid, blockHeader, merkleProof, 32),
'invalid Merkle proof'
)

Expand Down Expand Up @@ -259,7 +259,7 @@ export class Bsv20LendingPool extends BSV20V2 {
'BH does not meet min target'
)
assert(
Blockchain.txInBlock(prevTxid, blockHeader, merkleProof),
Blockchain.txInBlock(prevTxid, blockHeader, merkleProof, 32),
'invalid Merkle proof'
)

Expand Down
2 changes: 1 addition & 1 deletion src/contracts/bsv20LoanMultiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export class Bsv20LoanMultiple extends BSV20V2 {
'BH does not meet min target'
)
assert(
Blockchain.txInBlock(prevTxid, blockHeader, merkleProof),
Blockchain.txInBlock(prevTxid, blockHeader, merkleProof, 32),
'invalid Merkle proof'
)

Expand Down
2 changes: 1 addition & 1 deletion src/contracts/bsv20LockBtcToMint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class Bsv20LockBtcToMint extends BSV20V2 {

// Calc merkle root.
const txID = hash256(btcTx)
const merkleRoot = MerklePath.calcMerkleRoot(txID, merkleProof)
const merkleRoot = MerklePath.calcMerkleRoot(txID, merkleProof, 32)

// Check if merkle root is included in the first BH.
assert(
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/btcSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export class BTCSwap extends SmartContract {

// Calc merkle root.
const txID = hash256(btcTx)
const merkleRoot = MerklePath.calcMerkleRoot(txID, merkleProof)
const merkleRoot = MerklePath.calcMerkleRoot(txID, merkleProof, 32)

// Check if merkle root is included in the first BH.
assert(
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/mast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class MAST extends SmartContract {
public main(branchScript: ByteString, merklePath: MerkleProof) {
// validate branchScript is from the merkle tree
assert(
MerklePath.calcMerkleRoot(sha256(branchScript), merklePath) ==
MerklePath.calcMerkleRoot(sha256(branchScript), merklePath, 32) ==
this.merkleRoot
)

Expand Down
4 changes: 2 additions & 2 deletions src/contracts/priceBet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
PubKey,
slice,
} from 'scrypt-ts'
import { RabinSig, RabinPubKey, RabinVerifierWOC } from 'scrypt-ts-lib'
import { RabinSig, RabinPubKey, WitnessOnChainVerifier } from 'scrypt-ts-lib'

export type ExchangeRate = {
timestamp: bigint
Expand Down Expand Up @@ -82,7 +82,7 @@ export class PriceBet extends SmartContract {
public unlock(msg: ByteString, sig: RabinSig, winnerSig: Sig) {
// Verify oracle signature.
assert(
RabinVerifierWOC.verifySig(msg, sig, this.oraclePubKey),
WitnessOnChainVerifier.verifySig(msg, sig, this.oraclePubKey),
'Oracle sig verify failed.'
)

Expand Down
60 changes: 40 additions & 20 deletions src/contracts/treeSig.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,62 @@
import { ByteString, FixedArray, PubKey, Sha256, Sig, SmartContract, assert, method, prop, sha256, toByteString } from "scrypt-ts";
import { MerklePath, MerkleProof } from 'scrypt-ts-lib';
import {
ByteString,
FixedArray,
PubKey,
Sha256,
Sig,
SmartContract,
assert,
method,
prop,
sha256,
toByteString,
} from 'scrypt-ts'
import { MerklePath, MerkleProof } from 'scrypt-ts-lib'

// tree signatures: Merkle tree-based multisig
export class TreeSig extends SmartContract{


export class TreeSig extends SmartContract {
// M out of N multisig
static readonly M : bigint = 3n;
static readonly M: bigint = 3n

@prop()
readonly merkleRoot : Sha256;
readonly merkleRoot: Sha256

constructor(merkleRoot : Sha256){
constructor(merkleRoot: Sha256) {
super(...arguments)
this.merkleRoot = merkleRoot
}

@method()
public main(pubKeys : FixedArray<PubKey, 3>, sigs : FixedArray<Sig, 3> , merkleproof : MerkleProof) {
public main(
pubKeys: FixedArray<PubKey, 3>,
sigs: FixedArray<Sig, 3>,
merkleproof: MerkleProof
) {
// validate public keys are from the merkle tree
assert(MerklePath.calcMerkleRoot(TreeSig.pubKeys2Leaf(pubKeys), merkleproof) == this.merkleRoot);
assert(
MerklePath.calcMerkleRoot(
TreeSig.pubKeys2Leaf(pubKeys),
merkleproof,
32
) == this.merkleRoot
)

// check if all M signatures are valid
let allMatch :boolean = true;
for (let i = 0; i < 3; i ++) {
allMatch = allMatch && this.checkMultiSig(sigs, pubKeys);
let allMatch: boolean = true
for (let i = 0; i < 3; i++) {
allMatch = allMatch && this.checkMultiSig(sigs, pubKeys)
}
assert(allMatch);
assert(allMatch)
}

// map public keys to a leaf
@method()
static pubKeys2Leaf(pubKeys : FixedArray<PubKey, 3>) : Sha256 {
let leaf : ByteString = toByteString('');
static pubKeys2Leaf(pubKeys: FixedArray<PubKey, 3>): Sha256 {
let leaf: ByteString = toByteString('')

for (let i = 0; i < 3; i ++) {
leaf += pubKeys[i];
for (let i = 0; i < 3; i++) {
leaf += pubKeys[i]
}
return sha256(leaf);
return sha256(leaf)
}
}

0 comments on commit 163b053

Please sign in to comment.