Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cmd committed Mar 3, 2024
1 parent b7faf47 commit 66f63e3
Show file tree
Hide file tree
Showing 18 changed files with 161 additions and 357 deletions.
51 changes: 29 additions & 22 deletions demo/draft/alice/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import { alias, fund_amt, role, signer, wit_tmpl } from './config.js'
/** ========== [ Draft Session ] ========== **/

// Create a draft session
const session = new DraftSession(signer, {
socket_config : { verbose : true, debug : false },
store_config : { verbose : true, debug : false },
verbose : true
const session = new DraftSession(secret_id, signer, {
debug : false,
verbose : false
})

// Create an account object.
Expand All @@ -35,9 +34,7 @@ session.on('error', console.log)

// When the session is ready:
session.on('ready', () => {
console.log('updated at :', new Date(session.updated_at * 1000))
console.log('init data :')
console.dir(session.data)
console.log('alice ready')
// If we are not a member:
if (!session.is_member) {
// Grab the buyer policy from the roles list.
Expand All @@ -53,32 +50,42 @@ session.on('ready', () => {

// Each time the session updates:
session.on('update', async () => {
console.log('draft updated')
console.log('draft:', session.data)
console.log('alice draft updated')
console.log('is full :', session.is_full)
console.log('is approved :', session.is_approved)
console.log('is confirmed :', session.is_confirmed)
})

session.on('join', (msg) => {
console.log('pubkey joined draft :', msg.pub)
console.log('pubkey joined as role :', msg.pol)
})

session.on('approve', (proof) => {
const pub = proof.slice(0, 64)
console.log('pubkey approved:', pub)
})

session.on('endorse', (proof) => {
const pub = proof.slice(0, 64)
console.log('pubkey endorsed:', pub)
})

session.on('full', () => {
console.log('draft is full')
// If all roles have been assigned:
if (session.is_full) {
// If we have not yet endorsed the draft:
if (!session.is_approved) {
// Endorse the draft.
session.approve()
console.log(`${alias} approved the draft`)
} else if (session.is_confirmed) {
// console.log('publishing the contract...')
try {
session.publish(client)
} catch (err) {
console.log('error publishing contract:', err)
}
}
}
})

session.on('full', () => {
console.log('all roles have been filled')
})

session.on('confirmed', () => {
console.log('draft has enough signatures')
session.publish(client)
})

session.on('error', console.log)
Expand Down Expand Up @@ -169,4 +176,4 @@ session.on('error', console.log)

console.log('draft:', agent_draft)

await session.init('wss://relay.damus.io', secret_id, agent_draft)
await session.init('wss://relay.damus.io', agent_draft)
76 changes: 46 additions & 30 deletions demo/draft/bob/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,29 @@ import {
EscrowContract
} from '@scrow/core'

import { client } from '@scrow/demo/01_create_client.js'
import { config } from '@scrow/demo/00_demo_config.js'
import { print_banner } from '@scrow/test'
import { secret_id } from '../terms.js'
import { client } from '@scrow/demo/01_create_client.js'
import { config } from '@scrow/demo/00_demo_config.js'
import { print_banner } from '@scrow/test'
import { secret_id } from '../terms.js'

import { fund_regtest_address, sleep } from '@scrow/demo/util.js'
import { fund_mutiny_address, fund_regtest_address, sleep } from '@scrow/demo/util.js'

import { alias, fund_amt, role, signer, wit_tmpl } from './config.js'

/** ========== [ Draft Session ] ========== **/

// Create a draft session
const session = new DraftSession(signer, {
socket_config : { verbose : true, debug : false },
store_config : { verbose : true, debug : false },
verbose : true
const session = new DraftSession(secret_id, signer, {
debug : false,
verbose : false
})

// Create an account object.
const account = new EscrowAccount(client, signer)

// When the session is ready:
session.on('ready', async () => {
console.log('session ready')
console.dir(session.data)
console.log('bob draft ready')
// If we are not a member:
if (!session.is_member) {
// Grab the policy from the roles list.
Expand All @@ -46,23 +45,39 @@ session.on('error', console.log)

// Each time the session updates:
session.on('update', async () => {
console.log('session update')
console.log('draft:', session.data)
console.log('bob draft updated')
console.log('is full :', session.is_full)
console.log('is approved :', session.is_approved)
console.log('is confirmed :', session.is_confirmed)
})

session.on('join', (msg) => {
console.log('pubkey joined draft :', msg.pub)
console.log('pubkey joined as role :', msg.pol)
})

session.on('approve', (proof) => {
const pub = proof.slice(0, 64)
console.log('pubkey approved:', pub)
})

session.on('endorse', (proof) => {
const pub = proof.slice(0, 64)
console.log('pubkey endorsed:', pub)
})

session.on('full', () => {
console.log('draft is full')
// If all roles have been assigned:
if (session.is_full) {
// If we have not yet endorsed the draft:
if (!session.is_approved) {
// Endorse the draft.
session.approve()
console.log(`${alias} endorsed the draft`)
}
}
})

session.on('full', () => {
console.log('all roles have been filled')
})

session.on('confirmed', () => {
console.log('draft has enough signatures')
})
Expand All @@ -72,18 +87,19 @@ session.on('confirmed', () => {
function fund_contract (contract : ContractData) {
// When an account is received from the server:
account.on('reserved', async () => {
// If we are not on regtest:
if (config.network !== 'regtest') {
// Make a deposit to the specified address:

switch (config.network) {
case 'mutiny':
fund_mutiny_address(account.address, fund_amt)
break
case 'regtest':
fund_regtest_address(account.address, fund_amt)
break
default:
print_banner('make a deposit')
console.log('copy this address :', account.data.address)
console.log('copy this address :', account.address)
console.log('send this amount :', `${fund_amt} sats`)
console.log('get funds here :', config.faucet, '\n')
} else {
// Use the automated payment for regtest testing.
print_banner('sending deposit')
await fund_regtest_address(account.data.address, fund_amt)
await sleep(2000)
console.log('get funds here :', config.faucet, '\n')
}

// Define our polling config.
Expand Down Expand Up @@ -130,7 +146,7 @@ function settle_contract (contract : EscrowContract) {
})

console.log('polling contract...')
return contract.poll('settled', 10, 30)
return contract.poll('settled', 30)
}

/** ========== [ Flow Control ] ========== **/
Expand All @@ -146,4 +162,4 @@ session.on('published', async (cid) => {

session.on('error', console.log)

await session.connect('wss://relay.damus.io', secret_id)
await session.connect('wss://relay.damus.io')
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@
"@cmdcode/buff": "2.2.5",
"@cmdcode/crypto-tools": "2.7.6",
"@cmdcode/musig2": "2.4.3",
"@cmdcode/signer": "1.4.1",
"@cmdcode/nostr-sdk": "^0.0.3",
"@cmdcode/signer": "1.4.2",
"@scrow/tapscript": "2.2.2",
"websocket-polyfill": "^0.0.3",
"zod": "^3.22.4"
}
}
Empty file added src/client/api/draft/endorse.ts
Empty file.
Empty file added src/client/api/draft/mship.ts
Empty file.
Empty file added src/client/api/draft/role.ts
Empty file.
Empty file added src/client/api/draft/terms.ts
Empty file.
Loading

0 comments on commit 66f63e3

Please sign in to comment.