Skip to content

Commit

Permalink
Merge pull request #344 from yusufidimaina9989/demo
Browse files Browse the repository at this point in the history
Add enum to blindEscrow contract
  • Loading branch information
msinkec authored Jul 19, 2024
2 parents 81814e1 + 4b17145 commit 1004682
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
24 changes: 13 additions & 11 deletions src/contracts/blindEscrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ import { SECP256K1, Signature } from 'scrypt-ts-lib'
// All public keys must be in uncompressed form. This also affects
// the values of the pub key hashes i.e. addresses.

export class BlindEscrow extends SmartContract {
// 4 possible actions:
// 4 possible actions:
// - buyer signs and uses sellers stamp (releaseBySeller)
// - buyer signs and uses arbiters stamp (releaseByArbiter)
// - seller signs and uses buyers stamp (returnByBuyer)
// - seller signs and uses arbiters stamp (returnByArbiter)
static readonly RELEASE_BY_SELLER = 0n
static readonly RELEASE_BY_ARBITER = 1n
static readonly RETURN_BY_BUYER = 2n
static readonly RETURN_BY_ARBITER = 3n

export enum Actions{
RELEASE_BY_SELLER,
RELEASE_BY_ARBITER,
RETURN_BY_BUYER,
RETURN_BY_ARBITER
}
export class BlindEscrow extends SmartContract {

@prop()
seller: Addr

Expand Down Expand Up @@ -73,16 +75,16 @@ export class BlindEscrow extends SmartContract {
)

// Load correct addresses.
if (action == BlindEscrow.RELEASE_BY_SELLER) {
if (action == BigInt(Actions.RELEASE_BY_SELLER)) {
spender = this.buyer
oracle = this.seller
} else if (action == BlindEscrow.RELEASE_BY_ARBITER) {
} else if (action == BigInt(Actions.RELEASE_BY_ARBITER)) {
spender = this.buyer
oracle = this.arbiter
} else if (action == BlindEscrow.RETURN_BY_BUYER) {
} else if (action == BigInt(Actions.RETURN_BY_BUYER)) {
spender = this.seller
oracle = this.buyer
} else if (action == BlindEscrow.RETURN_BY_ARBITER) {
} else if (action == BigInt(Actions.RETURN_BY_ARBITER)) {
spender = this.seller
oracle = this.arbiter
} else {
Expand Down
18 changes: 9 additions & 9 deletions tests/blindEscrow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
pubKey2Addr,
} from 'scrypt-ts'
import { Signature } from 'scrypt-ts-lib'
import { BlindEscrow } from '../src/contracts/blindEscrow'
import { BlindEscrow, Actions } from '../src/contracts/blindEscrow'
import { getDefaultSigner } from './utils/helper'

use(chaiAsPromised)
Expand Down Expand Up @@ -72,7 +72,7 @@ describe('Heavy: Test SmartContract `BlindEscrow`', () => {

// Create "stamp", i.e. seller signature of the escrowNonce.
const oracleMsg: ByteString =
escrowNonce + int2ByteString(BlindEscrow.RELEASE_BY_SELLER)
escrowNonce + int2ByteString(BigInt(Actions.RELEASE_BY_SELLER))
const hashBuff = Buffer.from(hash256(oracleMsg), 'hex')
const oracleSigObj = bsv.crypto.ECDSA.sign(hashBuff, seller)
const oracleSig: Signature = {
Expand All @@ -89,7 +89,7 @@ describe('Heavy: Test SmartContract `BlindEscrow`', () => {
PubKey(buyerPubKey.toByteString()),
oracleSig,
PubKey(sellerPubKey.toByteString()),
BlindEscrow.RELEASE_BY_SELLER,
Actions.RELEASE_BY_SELLER,
{
pubKeyOrAddrToSign: buyer.publicKey,
} as MethodCallOptions<BlindEscrow>
Expand All @@ -102,7 +102,7 @@ describe('Heavy: Test SmartContract `BlindEscrow`', () => {
//// Sig by buyer, stamp by arbiter.

const oracleMsg: ByteString =
escrowNonce + int2ByteString(BlindEscrow.RELEASE_BY_ARBITER)
escrowNonce + int2ByteString(BigInt(Actions.RELEASE_BY_ARBITER))
const hashBuff = Buffer.from(hash256(oracleMsg), 'hex')
const oracleSigObj = bsv.crypto.ECDSA.sign(hashBuff, arbiter)
const oracleSig: Signature = {
Expand All @@ -120,7 +120,7 @@ describe('Heavy: Test SmartContract `BlindEscrow`', () => {
PubKey(buyerPubKey.toByteString()),
oracleSig,
PubKey(arbiterPubKey.toByteString()),
BlindEscrow.RELEASE_BY_ARBITER,
Actions.RELEASE_BY_ARBITER,
{
pubKeyOrAddrToSign: buyer.publicKey,
} as MethodCallOptions<BlindEscrow>
Expand All @@ -133,7 +133,7 @@ describe('Heavy: Test SmartContract `BlindEscrow`', () => {
//// Sig by seller, stamp by buyer.

const oracleMsg: ByteString =
escrowNonce + int2ByteString(BlindEscrow.RETURN_BY_BUYER)
escrowNonce + int2ByteString(BigInt(Actions.RETURN_BY_BUYER))
const hashBuff = Buffer.from(hash256(oracleMsg), 'hex')
const oracleSigObj = bsv.crypto.ECDSA.sign(hashBuff, buyer)
const oracleSig: Signature = {
Expand All @@ -149,7 +149,7 @@ describe('Heavy: Test SmartContract `BlindEscrow`', () => {
PubKey(sellerPubKey.toByteString()),
oracleSig,
PubKey(buyerPubKey.toByteString()),
BlindEscrow.RETURN_BY_BUYER,
Actions.RETURN_BY_BUYER,
{
pubKeyOrAddrToSign: seller.publicKey,
} as MethodCallOptions<BlindEscrow>
Expand All @@ -162,7 +162,7 @@ describe('Heavy: Test SmartContract `BlindEscrow`', () => {
//// Sig by seller, stamp by arbiter.

const oracleMsg: ByteString =
escrowNonce + int2ByteString(BlindEscrow.RETURN_BY_ARBITER)
escrowNonce + int2ByteString(BigInt(Actions.RETURN_BY_ARBITER))
const hashBuff = Buffer.from(hash256(oracleMsg), 'hex')
const oracleSigObj = bsv.crypto.ECDSA.sign(hashBuff, arbiter)
const oracleSig: Signature = {
Expand All @@ -179,7 +179,7 @@ describe('Heavy: Test SmartContract `BlindEscrow`', () => {
PubKey(sellerPubKey.toByteString()),
oracleSig,
PubKey(arbiterPubKey.toByteString()),
BlindEscrow.RETURN_BY_ARBITER,
Actions.RETURN_BY_ARBITER,
{
pubKeyOrAddrToSign: seller.publicKey,
} as MethodCallOptions<BlindEscrow>
Expand Down

0 comments on commit 1004682

Please sign in to comment.