Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdruid committed May 8, 2024
2 parents 8012883 + 5beb031 commit 8c25b39
Show file tree
Hide file tree
Showing 16 changed files with 208 additions and 122 deletions.
2 changes: 1 addition & 1 deletion demo/04_finish_proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const DEMO_MODE = process.env.VERBOSE === 'true'
const [ a_signer, b_signer, c_signer ] = signers

// Define our negotiation session.
let draft = DraftUtil.create(proposal, roles),
let draft = DraftUtil.create({ proposal, roles }),
seats = draft.roles.map(e => e.id)

// For each member, add their info to the proposal.
Expand Down
5 changes: 3 additions & 2 deletions demo/09_settle_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ if (DEMO_MODE) {
/** ========== [ Review Settlement ] ========== **/

// Assert that a settled contract exists.
assert.exists(res.data.contract, 'settled contract was not returned')
const settled_res = await client.contract.read(active_contract.cid)
if (!settled_res.ok) throw new Error(res.error)
// Unpack the settled contract from the response.
const settled_contract = res.data.contract
const settled_contract = settled_res.data.contract

if (DEMO_MODE) {
print_banner('settled contract')
Expand Down
10 changes: 5 additions & 5 deletions demo/vm/eval.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { now } from '@scrow/sdk/util'
import { print_banner, VMUtil } from '@scrow/test'

import vector from './vector2.json' assert { type: 'json' }
import vector from './fundraiser.json' assert { type: 'json' }

const active_at = now()
const engine = 'cvm'
Expand All @@ -10,13 +10,13 @@ const vmid = '00'.repeat(32)
// Unpack the mock vector.
const { duration, members, pathnames, programs, schedule, statements } = vector
// Set the closing date.
const closes_at = active_at + duration
const expires_at = active_at + duration
// Find and replace aliases with their relevant pubkeys.
const progs = VMUtil.resolve_aliases(members, programs)
const progs = VMUtil.resolve_aliases(members, programs)
// Configure the init state of the vm.
const config = VMUtil.get_config({ active_at, closes_at, engine, pathnames, programs : progs, schedule, vmid })
const config = VMUtil.get_config({ active_at, expires_at, engine, pathnames, programs : progs, schedule, vmid })
// Run VM with witness and final timestamp.
const vm_state = VMUtil.run_vm_vectors(config, duration, statements)
const vm_state = VMUtil.run_vm_vectors(config, duration, statements)

print_banner('vm state')
console.dir(vm_state, { depth: null })
Expand Down
12 changes: 12 additions & 0 deletions demo/vm/fundraiser.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"comment" : "Basic payout, immediate execution",
"duration" : 9999,
"members" : [],
"pathnames" : [ "payout", "return" ],
"programs" : [],
"schedule" : [
[ 0, "close", "payout" ],
[ 1000, "close", "return" ]
],
"statements" : []
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@scrow/sdk",
"version": "0.11.0",
"version": "0.12.15",
"description": "Development SDK for the BitEscrow API.",
"author": "Christopher Scott",
"license": "MIT",
Expand Down
17 changes: 12 additions & 5 deletions src/client/api/signer/draft.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { verify_proposal } from '@/core/validation/proposal.js'
import { EscrowSigner } from '../../class/signer.js'

import {
get_proposal_id,
verify_endorsement
} from '@/core/lib/proposal.js'

import {
CredentialConfig,
DraftSession
Expand Down Expand Up @@ -70,11 +75,13 @@ export function has_role_api (esigner : EscrowSigner) {
}

export function has_signature_api (esigner : EscrowSigner) {
return (
session : DraftSession
) : boolean => {
const mship = claim_membership(session.members, esigner._signer)
return mship?.sig !== undefined
return (session : DraftSession) : boolean => {
const pub = esigner.pubkey
const sig = session.sigs.find(e => e.slice(0, 64) === pub)
if (sig === undefined) return false
const prop_id = get_proposal_id(session.proposal)
verify_endorsement(prop_id, sig)
return true
}
}

Expand Down
17 changes: 13 additions & 4 deletions src/client/api/signer/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AddressConfig } from '@cmdcode/signer'
import { EscrowSigner } from '../../class/signer.js'

export function has_address_api (esigner : EscrowSigner) {
Expand All @@ -10,18 +11,26 @@ export function has_address_api (esigner : EscrowSigner) {
}
}

export function get_address_api (esigner : EscrowSigner) {
return (account : number, config : AddressConfig = {}) => {
config.format = config.format ?? 'p2tr'
config.network = esigner.network
return esigner._wallet.get_account(account).get_address(config)
}
}

export function new_address_api (esigner : EscrowSigner) {
return (account : number) => {
const format = 'p2tr'
const network = esigner.network
const config = { format, network }
return (account : number, config : AddressConfig = {}) => {
config.format = config.format ?? 'p2tr'
config.network = esigner.network
return esigner._wallet.get_account(account).new_address(config)
}
}

export default function (esigner : EscrowSigner) {
return {
has : has_address_api(esigner),
get : get_address_api(esigner),
new : new_address_api(esigner)
}
}
8 changes: 1 addition & 7 deletions src/client/class/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,16 @@ export class EscrowSigner {
return this._config.server_pk
}

get wallet () {
return {
has : this._wallet.has_address
}
}

get xpub () {
return this._wallet.xpub
}

account = account_api(this)
address = wallet_api(this)
contract = contract_api(this)
deposit = deposit_api(this)
draft = draft_api(this)
vm = vmachine_api(this)
wallet = wallet_api(this)
witness = witness_api(this)

backup (password : string) {
Expand Down
46 changes: 34 additions & 12 deletions src/client/lib/enrollment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {

const GET_ROLE_DEFAULTS = () => {
return {
min_num : 1,
max_num : 1,
paths : [],
programs : []
moderator : false,
paths : [],
programs : [],
seats : 1
}
}

Expand Down Expand Up @@ -45,6 +45,33 @@ export function get_role_policy (
return policy
}

export function get_role_paths_totals (
roles : Array<RoleTemplate | RolePolicy>
) {
const path_totals = new Map<string, number>()

for (const { paths } of roles) {
if (paths !== undefined) {
paths.forEach(([ label, value ]) => {
const curr = path_totals.get(label) ?? 0
path_totals.set(label, curr + value)
})
}
}

return [ ...path_totals ]
}

export function get_role_payment_totals (
roles : Array<RoleTemplate | RolePolicy>
) {
let bal = 0
for (const role of roles) {
bal += role.payment ?? 0
}
return bal
}

export function add_member_data (
cred : CredentialData,
policy : RolePolicy,
Expand Down Expand Up @@ -128,7 +155,6 @@ export function verify_member_data (
const rdata = get_enrollment(cred, proposal)
const rpaths = rdata.paths.map(e => e.toString())
const rprogs = programs.map(e => e.toString())
console.log('pay:', payment, rdata.payment)
if (
typeof payment === 'number' &&
payment > 0 &&
Expand Down Expand Up @@ -174,7 +200,7 @@ export function has_open_roles (
const policy = roles.find(e => e.id === policy_id)
assert.ok(policy !== undefined, 'role does not exists for policy id: ' + policy_id)
const slots = members.filter(e => e.pid === policy_id)
return slots.length < policy.max_num
return slots.length < policy.seats
}

export function has_full_roles (
Expand All @@ -183,12 +209,8 @@ export function has_full_roles (
) {
const tab = tabulate_slots(members, roles)
return roles.every(e => {
const { id, min_num, max_num } = e
const { id, seats } = e
const count = tab.get(id)
return (
count !== undefined &&
count >= min_num &&
count <= max_num
)
return (count !== undefined && count === seats)
})
}
22 changes: 6 additions & 16 deletions src/client/lib/membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,10 @@ export function has_membership (
return exists !== undefined
}

export function has_full_endorsement (members : MemberData[]) {
return members.every(e => typeof e.sig === 'string')
}

export function get_signatures (members : MemberData[]) {
return members.map(mship => {
assert.exists(mship.sig)
return mship.sig
})
}

export function clear_signatures (members : MemberData[]) {
return members.map(mship => {
mship.sig = undefined
return mship
})
export function has_endorsements (
members : MemberData[],
signatures : string[]
) {
const pubkeys = signatures.map(e => e.slice(0, 64))
return members.every(e => pubkeys.includes(e.pub))
}
Loading

0 comments on commit 8c25b39

Please sign in to comment.