Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cmd committed Mar 5, 2024
1 parent 956a351 commit ac094c8
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 47 deletions.
44 changes: 44 additions & 0 deletions demo/draft/alice/join.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { DraftSession } from '@scrow/core'
import { signer } from './config.js'
import { secret_id } from '../terms.js'

const role = 'buyer'

const session = new DraftSession(signer, {
debug : false,
verbose : false
})

session.on('ready', () => {
console.log('session ready')
console.log('session:', session.data)
if (!session.is_member) {
if (session.has_role(role)) {
console.log('joining session as:', role)
const pol = session.get_role(role)
session.join(pol.id)
}
} else if (session.is_member) {
console.log('member leaving session:', session.mship.pub)
session.leave()
}
})

session.on('update', () => {
console.log('session updated')
if (session.is_member) {
console.log('member leaving session:', session.mship.pub)
session.leave()
}
})

session.on('join', (mship) => {
console.log('member joined:', mship.pub)
})

session.on('leave', (mship) => {
console.log('member left:', mship.pub)
session.close()
})

await session.connect('wss://relay.damus.io', secret_id)
21 changes: 0 additions & 21 deletions demo/draft/alice/leave.ts

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"@cmdcode/buff": "2.2.5",
"@cmdcode/crypto-tools": "2.7.6",
"@cmdcode/musig2": "2.4.3",
"@cmdcode/nostr-sdk": "^0.1.0",
"@cmdcode/nostr-sdk": "^0.1.2",
"@cmdcode/signer": "1.4.2",
"@scrow/tapscript": "2.2.2",
"zod": "^3.22.4"
Expand Down
61 changes: 40 additions & 21 deletions src/client/class/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ interface SessionConfig {

export class DraftSession extends EventEmitter <{
'approve' : string
'close' : DraftSession
'commit' : string
'confirmed' : DraftSession
'debug' : unknown[]
Expand Down Expand Up @@ -104,6 +105,8 @@ export class DraftSession extends EventEmitter <{
this._full = false
this._init = false


this._room.on('close', () => { void this.emit('close', this) })
this._room.on('fetch', () => { void this.emit('fetch', this) })

this._room.once('ready', () => {
Expand Down Expand Up @@ -135,14 +138,6 @@ export class DraftSession extends EventEmitter <{
return this.data.approvals
}

get available () {
const map = tabulate_enrollment(this.members, this.roles)
return this.roles.filter(e => {
const score = map.get(e.id) ?? e.max_slots
return score >= e.max_slots
})
}

get data () {
return this._room.data
}
Expand All @@ -153,7 +148,7 @@ export class DraftSession extends EventEmitter <{

get is_approved () {
if (!this.is_member) return false
const mship = this.membership.data
const mship = this.mship
const sig = this.approvals.find(e => {
return e.slice(0, 64) === mship.pub
})
Expand Down Expand Up @@ -209,18 +204,15 @@ export class DraftSession extends EventEmitter <{
return this.data.members
}

get member_idx () {
const idx = this.members.findIndex(e => {
return this.signer.credential.claimable(e)
})
return idx !== -1 ? idx : null
}
// get member_idx () {
// const idx = this.members.findIndex(e => {
// return this.signer.credential.claimable(e)
// })
// return idx !== -1 ? idx : null
// }

get membership () {
if (!this.is_member) {
throw new Error('signer is not a member of the draft')
}
return this.signer.credential.claim(this.members)
get mship () {
return this.get_mship().data
}

get opt () {
Expand Down Expand Up @@ -373,7 +365,6 @@ export class DraftSession extends EventEmitter <{
const mship = JSON.parse(msg.body)
const cat = msg.envelope.created_at
this._leave(mship, cat)
this.log.info('member left :', mship.pub)
}

_terms_handler (msg : EventMessage) {
Expand Down Expand Up @@ -444,6 +435,11 @@ export class DraftSession extends EventEmitter <{
}
return true
}

close () {
this._room.close()
return this
}

async connect (address : string, secret : string) {
await this._room.connect(address, secret)
Expand All @@ -468,6 +464,13 @@ export class DraftSession extends EventEmitter <{
return this._room.fetch()
}

get_mship () {
if (!this.is_member) {
throw new Error('you are not a member of the draft')
}
return this.signer.credential.claim(this.members)
}

get_policy (pol_id : string) {
const pol = this.roles.find(e => e.id === pol_id)
if (pol === undefined) throw new Error('policy does not exist: ' + pol_id)
Expand All @@ -486,11 +489,23 @@ export class DraftSession extends EventEmitter <{
return pol
}

get_seats () {
const map = tabulate_enrollment(this.members, this.roles)
return this.roles.filter(e => {
const score = map.get(e.id) ?? e.max_slots
return score < e.max_slots
})
}

get_term <K extends keyof ProposalData> (key : K) {
if (!this.terms.includes(key)) throw new Error('term is not negotiable: ' + key)
return this.proposal[key]
}

has_mship () {
return this.signer.credential.exists(this.members)
}

has_policy (pol_id : string) {
return this.roles.find(e => e.id === pol_id) !== undefined
}
Expand All @@ -503,6 +518,10 @@ export class DraftSession extends EventEmitter <{
return this.roles.find(e => e.title === title) !== undefined
}

has_seats () {
return this.get_seats().length > 0
}

has_term <K extends keyof ProposalData> (key : K) {
return this.terms.includes(key)
}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
"@cmdcode/buff" "^2.2.5"
"@cmdcode/crypto-tools" "^2.7.6"

"@cmdcode/nostr-sdk@^0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@cmdcode/nostr-sdk/-/nostr-sdk-0.1.0.tgz#4ca253cddf3d9505d86bda38bc864699f41e10b8"
integrity sha512-ykQmLbgQxoF56473jjQAC5y6KdsAFTAPDPwhR1YpbCXoDBGghwuLAMhoGHysAh1PbKYccVY6Re69wKlHgZQMJA==
"@cmdcode/nostr-sdk@^0.1.2":
version "0.1.2"
resolved "https://registry.yarnpkg.com/@cmdcode/nostr-sdk/-/nostr-sdk-0.1.2.tgz#b762d1663e0da279dc33ac3b7ee92457b187170f"
integrity sha512-/G8FYtpEAJiDHhxl1gKZpVmwyySEsok/bkud2z6Sb/CrZnBFK7uZ51CcJYiAOKLOKwtWrdJhlL0c4XGOXSPm7A==
dependencies:
"@cmdcode/buff" "2.2.5"
"@cmdcode/crypto-tools" "2.7.6"
Expand Down

0 comments on commit ac094c8

Please sign in to comment.