Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cmd committed Feb 10, 2024
1 parent 7b3e708 commit 9635eaf
Show file tree
Hide file tree
Showing 44 changed files with 1,897 additions and 963 deletions.
3 changes: 2 additions & 1 deletion demo/client/alice/00_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { config } from '@scrow/demo/00_demo_config.js'

/** ========== [ USER CONFIG ] ========== **/

const SECRET_PASS : string = 'test_draft2'
const SECRET_PASS : string = 'test_draft3'

const USER_ALIAS : string = 'alice'

Expand All @@ -36,6 +36,7 @@ const WIT_STATEMENT : WitnessTemplate = {

/** ========== [ MAIN EXPORT ] ========== **/

export const alias = USER_ALIAS
// Compute secret id for nostr session.
export const secret_id = Buff.str(SECRET_PASS).digest.hex
// Derive signing device from the user alias.
Expand Down
89 changes: 89 additions & 0 deletions demo/client/alice/01_draft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { DraftSession, EscrowAccount } from '@scrow/core'

import {
alias,
policy,
signer
} from './00_config.js'
import { print_banner } from '@scrow/test'
import { fund_address } from '@scrow/demo/util.js'
import { sleep } from '@/lib/util.js'
import { config } from '@scrow/demo/00_demo_config.js'

// Create a draft session
const session = new DraftSession(signer)
const account = new EscrowAccount(client)

// Print the store data on update.
session.on('ready', async () => {
console.log('session ready')
if (!session.is_member) {
await session.membership.create(policy)
console.log(`${alias} joined the draft`)
}
})

session.on('update', async (draft) => {
console.log('draft updated')
if (draft.members.length === 3 && !draft.is_endorsed) {
await draft.endorse.sign()
console.log(`${alias} endorsed the draft`)
}
})

account.on('reserved', async (acct) => {
if (config.network !== 'regtest') {
print_banner('make a deposit')
console.log('copy this address :', acct.data.address)
console.log('send this amount :', `${fund_amt} sats`)
console.log('get funds here :', config.faucet, '\n')
} else {
print_banner('sending deposit')
await fund_address(acct.data.address, fund_amt)
await sleep(2000)
}

let ival = 10, retries = 30

await acct.poll(ival, retries)
})

account.on('fetch', () => {
console.log('checking the oracle for new payments...')
})

export { account }


session.on('publish', async (cid : string) => {
console.log('draft published as cid:', cid)

const contract = await EscrowContract.fetch(client, cid)

account.on('update', async (acct) => {
console.log('locking deposit...')
await acct.commit(contract.data, signer)

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

contract.on('update', async (ct) => {
if (ct.status === 'active') {
console.log('sending statement to the vm...')
await ct.vm.sign(signer, wit_tmpl)
}
})

contract.on('update', (ct) => {
if (ct.status === 'settled') {
console.log('contract settled!')
process.exit()
}
})

console.log('fetching account...')
await account.request(signer, 14400)
})

await session.connect('wss://relay.damus.io', secret_id)
32 changes: 32 additions & 0 deletions demo/client/alice/02_fund.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { config } from '@scrow/demo/00_demo_config.js'
import { client } from '@scrow/demo/01_create_client.js'
import { print_banner } from '@scrow/test'
import { EscrowAccount } from '@/index.js'
import { fund_amt } from './00_config.js'

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

const account = new EscrowAccount(client)

account.on('reserved', async (acct) => {
if (config.network !== 'regtest') {
print_banner('make a deposit')
console.log('copy this address :', acct.data.address)
console.log('send this amount :', `${fund_amt} sats`)
console.log('get funds here :', config.faucet, '\n')
} else {
print_banner('sending deposit')
await fund_address(acct.data.address, fund_amt)
await sleep(2000)
}

let ival = 10, retries = 30

await acct.poll(ival, retries)
})

account.on('fetch', () => {
console.log('checking the oracle for new payments...')
})

export { account }
21 changes: 0 additions & 21 deletions demo/client/alice/02_join.ts

This file was deleted.

39 changes: 39 additions & 0 deletions demo/client/alice/03_start.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { EscrowContract } from '@scrow/core'
import { client } from '@scrow/demo/01_create_client.js'
import { session } from './01_draft.js'
import { account } from './02_fund.js'

import { secret_id, signer, wit_tmpl } from './00_config.js'

session.on('publish', async (cid : string) => {
console.log('draft published as cid:', cid)

const contract = await EscrowContract.fetch(client, cid)

account.on('update', async (acct) => {
console.log('locking deposit...')
await acct.commit(contract.data, signer)

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

contract.on('update', async (ct) => {
if (ct.status === 'active') {
console.log('sending statement to the vm...')
await ct.vm.sign(signer, wit_tmpl)
}
})

contract.on('update', (ct) => {
if (ct.status === 'settled') {
console.log('contract settled!')
process.exit()
}
})

console.log('fetching account...')
await account.request(signer, 14400)
})

await session.connect('wss://relay.damus.io', secret_id)
78 changes: 0 additions & 78 deletions demo/client/alice/04_fund.ts

This file was deleted.

14 changes: 0 additions & 14 deletions demo/client/alice/05_submit.ts

This file was deleted.

5 changes: 3 additions & 2 deletions demo/client/bob/00_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { config } from '@scrow/demo/00_demo_config.js'

/** ========== [ USER CONFIG ] ========== **/

const SECRET_PASS : string = 'test_draft2'
const SECRET_PASS : string = 'test_draft3'

const USER_ALIAS : string = 'bob'

Expand All @@ -27,7 +27,7 @@ const ROLE_POLICY : RoleTemplate = {
]
}

const FUND_AMOUNT : number = 0
const FUND_AMOUNT : number = 15_000

const WIT_STATEMENT : WitnessTemplate = {
action : 'close',
Expand All @@ -37,6 +37,7 @@ const WIT_STATEMENT : WitnessTemplate = {

/** ========== [ MAIN EXPORT ] ========== **/

export const alias = USER_ALIAS
// Compute secret id for nostr session.
export const secret_id = Buff.str(SECRET_PASS).digest.hex
// Derive signing device from the user alias.
Expand Down
30 changes: 30 additions & 0 deletions demo/client/bob/01_draft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { DraftSession } from '@scrow/core'
import { alias, policy, signer } from './00_config.js'

// Create a draft session
const session = new DraftSession(signer)

// Print the store data on update.
session.on('ready', async (draft) => {
console.log('session ready')
if (!draft.is_member) {
await draft.membership.create(policy)
console.log(`${alias} joined the draft`)
}
})

session.on('update', async (draft) => {
console.log('draft updated')
if (
draft.members.length === 3 &&
draft.signatures.length >= 1 &&
!draft.is_endorsed
) {
await draft.endorse.sign()
console.log(`${alias} endorsed the draft`)
}
})

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

export { session }
31 changes: 31 additions & 0 deletions demo/client/bob/02_fund.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { config } from '@scrow/demo/00_demo_config.js'
import { client } from '@scrow/demo/01_create_client.js'
import { print_banner } from '@scrow/test'
import { EscrowAccount } from '@/index.js'
import { fund_amt } from './00_config.js'
import { fund_address, sleep } from '@scrow/demo/util.js'

const account = new EscrowAccount(client)

account.on('reserved', async (acct) => {
if (config.network !== 'regtest') {
print_banner('make a deposit')
console.log('copy this address :', acct.data.address)
console.log('send this amount :', `${fund_amt} sats`)
console.log('get funds here :', config.faucet, '\n')
} else {
print_banner('sending deposit')
await fund_address(acct.data.address, fund_amt)
await sleep(2000)
}

let ival = 10, retries = 30

await acct.poll(ival, retries)
})

account.on('fetch', () => {
console.log('checking the oracle for new payments...')
})

export { account }
Loading

0 comments on commit 9635eaf

Please sign in to comment.