Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cmd committed Mar 9, 2024
1 parent 117941a commit cb0c93f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 16 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
contrib
coredata
dist
node_modules
src/**/_*
Expand Down
6 changes: 4 additions & 2 deletions demo/draft/alice/connect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DraftSession } from '@scrow/core'
import { signer } from './config.js'

import { address, secret_id } from '../terms.js'
import { address, secret_id } from '../terms.js'

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

Expand Down Expand Up @@ -33,4 +33,6 @@ session.on('leave', (mship) => {
console.log('member left:', mship.pub)
})

await session.connect(address, secret_id)


await session.connect('wss://nos.lol', '9112933aec51b7b79da16c64ce17e34fa892dd34acd6a005d7ea8db45fffa38f')
44 changes: 34 additions & 10 deletions src/client/class/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ export class DraftSession extends EventEmitter <{
}

get is_confirmed () {
const pubkeys = this.members.map(e => e.pub)
return this.is_full && this.approvals.every(e => {
const included = pubkeys.includes(e.slice(0 ,64))
const verified = verify_endorsement(this.prop_id, e)
return included && verified
const acks = this.approvals
return this.is_full && this.members.every(e => {
const ack = acks.find(x => x.slice(0, 64) === e.pub)
if (ack === undefined) return false
return verify_endorsement(this.prop_id, ack)
})
}

Expand Down Expand Up @@ -335,6 +335,7 @@ export class DraftSession extends EventEmitter <{
created_at ?: number
) {
const new_draft = join_role(mship, policy, this.data)

this._update(new_draft, created_at)
this.log.info('member joined :', mship.pub)
this.log.info('role joined :', policy.id)
Expand Down Expand Up @@ -395,7 +396,13 @@ export class DraftSession extends EventEmitter <{
schedule : [ ...schedule, ...terms.schedule ?? [] ],
}

const session = { ...this.data, proposal }
const session = {
...this.data,
proposal,
approvals : [],
signatures : []
}

this._update(session, created_at)
this.emit('terms', terms)
}
Expand All @@ -416,10 +423,16 @@ export class DraftSession extends EventEmitter <{
}

approve () {
const sig = this.signer.draft.approve(this.data)
this._approve(sig)
this._room.send('approve', sig)
this.log.info('send approve :', this.signer.pubkey)
const sig = this.signer.draft.approve(this.data)
const pub = this.mship.pub
const acks = this.approvals.map(e => e.slice(0, 64))
if (acks.includes(pub)) {
throw new Error('draft is already approved by member: ' + pub)
} else {
this._approve(sig)
this._room.send('approve', sig)
this.log.info('send approve :', this.signer.pubkey)
}
}

check_member (pubkey : string) {
Expand Down Expand Up @@ -569,6 +582,13 @@ export class DraftSession extends EventEmitter <{
return NostrRoom.list(address, signer, filter)
}

on_topic (
topic : string,
fn : (msg: EventMessage<string>) => void
) {
return this._room.on_topic(topic, fn)
}

async publish (client : EscrowClient) {
verify_proposal(this.data.proposal)
const contract = await EscrowContract.create(client, this.data)
Expand All @@ -581,6 +601,10 @@ export class DraftSession extends EventEmitter <{
return this._room.refresh()
}

send (subject: string, body: string) {
return this._room.send(subject, body)
}

update_terms (terms : Partial<ProposalData>) {
if (!this.check_terms(terms)) {
throw new Error('invalid terms: ' + terms.toString())
Expand Down
16 changes: 14 additions & 2 deletions src/lib/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,13 @@ export function join_role (
const tmpl = { ...proposal, paths, payments, programs }
const mbrs = upsert_member_data(members, mship)

return { ...session, members : mbrs, proposal : tmpl, signatures : [] }
return {
...session,
approvals : [],
members : mbrs,
proposal : tmpl,
signatures : []
}
}

export function rem_enrollment (
Expand All @@ -197,5 +203,11 @@ export function rem_enrollment (
const tmpl = { ...proposal, paths, payments, programs }
const mbrs = rem_member_data(members, membership)

return { ...session, members : mbrs, proposal : tmpl, signatures : [] }
return {
...session,
approvals : [],
members : mbrs,
proposal : tmpl,
signatures : []
}
}
11 changes: 10 additions & 1 deletion test/scratch.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import { EscrowClient } from "@/index.js";
import { Buff } from "@cmdcode/buff";

console.log(Buff.random(32).hex)
const client = new EscrowClient({
network : 'mutiny',
hostname : 'https://bitescrow-mutiny.vercel.app',
oracle : 'https://mutinynet.com'
})

const est = await client.oracle.fee_estimates()

console.log(est)

0 comments on commit cb0c93f

Please sign in to comment.