Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdruid committed May 22, 2024
1 parent 5cd7951 commit 013358d
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 102 deletions.
28 changes: 14 additions & 14 deletions demo/09_submit_statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ const [ a_signer, b_signer ] = signers

/**
* Configure and initialize a machine instance. We'll use this
* to validate our own statement, and to verify it was executed
* fairly by the escrow server.
* to verify our statements were executed fairly by the server.
*/
const config = get_machine_config(active_contract)
// Define our script interpreter.
const engine = CVM
// Create the initial state of the machine.
const vmdata = CVM.init(config)
// The machine id is derived from the contract.
const vmid = vmdata.vmid
let vmstate = engine.init(config)

/**
* Configure a template for our statement. This template is used to check
Expand All @@ -42,9 +41,9 @@ const template = {
*/
let witness : WitnessData
// Alice signs the initial statement.
witness = a_signer.witness.create(vmdata, template)
witness = a_signer.witness.create(vmstate, template)
// Bob endoreses the statement from Alice.
witness = b_signer.witness.endorse(vmdata, witness)
witness = b_signer.witness.endorse(vmstate, witness)

if (DEMO_MODE) {
print_banner('witness statement')
Expand All @@ -54,25 +53,26 @@ if (DEMO_MODE) {
/**
* Submit the signed witness statement to the escrow server.
*/
const res = await client.machine.submit(vmid, witness)
const res = await client.machine.submit(witness)
// Check the response is valid.
if (!res.ok) throw new Error(res.error)

/**
* The server will respond with a signed receipt. This receipt
* commits to our statement being evaluated by the machine.
*/
const vm_commit = res.data.commit
const { commit, vmdata } = res.data

/**
* We can use the receipt to verify that our witness statement was processed
* correctly by the escrow server.
* We can use our local copy of the vmstate to verify that our
* witness statement was handled correctly by the escrow server.
*/
client.witness.verify(vm_commit, vmdata, witness)
vmstate = engine.eval(vmstate, witness)
client.witness.verify(commit, vmstate, witness)

if (DEMO_MODE) {
print_banner('witness receipt')
console.dir(vm_commit, { depth : null })
console.dir(commit, { depth : null })
}

export { vmdata, witness }
export { commit, vmdata, witness }
2 changes: 1 addition & 1 deletion demo/api/machine/submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ print_banner('witness statement')
console.dir(witness, { depth : null })

// Submit the signed statement to the server.
const res = await client.machine.submit(vmid, witness)
const res = await client.machine.submit(witness)
// Check the response is valid.
if (!res.ok) throw new Error(res.error)
// Unpack the data from the response.
Expand Down
3 changes: 1 addition & 2 deletions docs/api/machine.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ body : JSON.stringify(submit_request)
**Request Body**

```ts
export interface VMSubmitRequest {
vmid : string
interface VMSubmitRequest {
witness : WitnessData
}
```
Expand Down
7 changes: 2 additions & 5 deletions src/client/api/client/deposit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert } from '@/core/util/index.js'
import { assert } from '@/core/util/index.js'
import { EscrowClient } from '@/client/class/client.js'

import {
validate_close_req,
Expand All @@ -17,10 +18,6 @@ import {
FundingDataResponse
} from '@/client/types/index.js'

/* Module Imports */

import { EscrowClient } from '../../class/client.js'

/**
* Fetch a deposit from the server by Id.
*/
Expand Down
47 changes: 23 additions & 24 deletions src/client/api/client/machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,6 @@ function list_machines_api (client : EscrowClient) {
}
}

/**
* Submit a signed statement to the contract.
*/
function submit_witness_api (client : EscrowClient) {
return async (
vmid : string,
witness : WitnessData
) : Promise<ApiResponse<VMSubmitResponse>> => {
// Validate the request.
validate_submit_req({ vmid, witness })
// Formulate the request url.
const host = client.server_url
const url = `${host}/api/machine/submit`
// Formulate the request body.
const init = {
method : 'POST',
body : JSON.stringify({ vmid, witness }),
headers : { 'content-type': 'application/json' }
}
// Return the response.
return client.fetcher<VMSubmitResponse>({ url, init })
}
}

/**
* Return a list of committed deposits
* that are associated with the contract.
Expand Down Expand Up @@ -84,6 +60,29 @@ function list_commits_api (client : EscrowClient) {
}
}

/**
* Submit a signed statement to the contract.
*/
function submit_witness_api (client : EscrowClient) {
return async (
witness : WitnessData
) : Promise<ApiResponse<VMSubmitResponse>> => {
// Validate the request.
validate_submit_req({ witness })
// Formulate the request url.
const host = client.server_url
const url = `${host}/api/machine/${witness.vmid}/submit`
// Formulate the request body.
const init = {
method : 'POST',
body : JSON.stringify({ witness }),
headers : { 'content-type': 'application/json' }
}
// Return the response.
return client.fetcher<VMSubmitResponse>({ url, init })
}
}

export default function (client : EscrowClient) {
return {
commits : list_commits_api(client),
Expand Down
4 changes: 2 additions & 2 deletions src/client/api/client/witness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ function read_witness_api (client : EscrowClient) {
function verify_commit_api (client : EscrowClient) {
return (
commit : WitnessCommit,
vmdata : MachineData,
vmstate : MachineData,
witness : WitnessData
) => {
// Verify the server pubkey.
client.verify_pk(commit.agent_pk)
// Verify the witness statement.
verify_witness_commit(commit, vmdata, witness)
verify_witness_commit(commit, vmstate, witness)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/client/api/signer/witness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
can_endorse,
create_witness,
endorse_witness
} from '@/core/module/witness/api.js'
} from '@/core/module/witness/index.js'

import {
ContractData,
Expand Down
4 changes: 2 additions & 2 deletions src/core/module/deposit/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function confirm_deposit (
assert.ok(!deposit.confirmed, 'deposit is already confirmed')
assert.ok(txstate.confirmed, 'transaction is not confirmed')
const status = (!deposit.locked)
? 'confirmed' as DepositStatus
? 'open' as DepositStatus
: 'locked' as DepositStatus
const updated = { ...deposit, ...txstate, status, updated_at: txstate.block_time }
return sort_record(updated)
Expand Down Expand Up @@ -111,7 +111,7 @@ export function release_deposit (
assert.ok(!deposit.closed, 'deposit is already closed')
assert.ok(!deposit.spent, 'deposit is already spent')
const status = (deposit.confirmed)
? 'confirmed' as DepositStatus
? 'open' as DepositStatus
: 'registered' as DepositStatus
const changes = INIT_LOCK_STATE()
return sort_record({ ...deposit, ...changes, status, updated_at })
Expand Down
2 changes: 1 addition & 1 deletion src/core/module/deposit/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const GET_CONFIRMED_STATE = () => {
return {
...INIT_SPEND_STATE(),
...INIT_LOCK_STATE(),
status : 'confirmed' as DepositStatus
status : 'open' as DepositStatus
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/core/module/machine/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import {
* Create a submit request object.
*/
export function create_submit_req (
vmid : string,
witness : WitnessData
) : VMSubmitRequest {
// Parse and return a valid submit request object.
return MachineSchema.submit_req.parse({ vmid, witness })
return MachineSchema.submit_req.parse({ witness })
}
42 changes: 8 additions & 34 deletions src/core/module/witness/api.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,17 @@
import { Buff } from '@cmdcode/buff'
import { get_program } from '@/core/lib/program.js'
import { get_witness_id } from './util.js'

import {
now,
sort_record
} from '@/core/util/index.js'
import { Buff } from '@cmdcode/buff'
import { get_program } from '@/core/lib/program.js'
import { sort_record } from '@/core/util/index.js'

import {
ProgramEntry,
SignerAPI,
MachineConfig,
MachineData,
WitnessData,
WitnessTemplate
WitnessData
} from '@/core/types/index.js'

export function create_witness (
config : MachineConfig | MachineData,
pubkeys : string | string[],
template : WitnessTemplate
) : WitnessData {
const { args = [], action, content = '', method, path, stamp = now() } = template

const keys = (Array.isArray(pubkeys)) ? pubkeys : [ pubkeys ]
const query = { method, action, path, includes: keys }
const pdata = get_program(query, config.programs)

if (pdata === undefined) {
throw new Error('matching program not found')
}

const prog_id = pdata.prog_id
const vmid = config.vmid
const tmpl = { ...template, args, content, prog_id, stamp, vmid }
const wid = get_witness_id(tmpl)
return sort_record({ ...tmpl, sigs: [], wid })
}

/**
* Checks if a given signing device
* can endorse a witness statement.
*/
export function can_endorse (
programs : ProgramEntry[],
signer : SignerAPI,
Expand Down
39 changes: 34 additions & 5 deletions src/core/module/witness/data.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,45 @@
import { get_commit_id } from './util.js'
import { get_program } from '@/core/lib/program.js'

import {
get_commit_id,
get_witness_id
} from './util.js'

import {
now,
sort_record
} from '../../util/index.js'
} from '@/core/util/index.js'

import {
SignerAPI,
MachineConfig,
MachineData,
SignerAPI,
WitnessCommit,
WitnessData,
WitnessCommit
} from '../../types/index.js'
WitnessTemplate
} from '@/core/types/index.js'

export function create_witness (
config : MachineConfig | MachineData,
pubkeys : string | string[],
template : WitnessTemplate
) : WitnessData {
const { args = [], action, content = '', method, path, stamp = now() } = template

const keys = (Array.isArray(pubkeys)) ? pubkeys : [ pubkeys ]
const query = { method, action, path, includes: keys }
const pdata = get_program(query, config.programs)

if (pdata === undefined) {
throw new Error('matching program not found')
}

const prog_id = pdata.prog_id
const vmid = config.vmid
const tmpl = { ...template, args, content, prog_id, stamp, vmid }
const wid = get_witness_id(tmpl)
return sort_record({ ...tmpl, sigs: [], wid })
}

export function create_commit (
data : MachineData,
Expand Down
2 changes: 1 addition & 1 deletion src/core/module/witness/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Buff } from '@cmdcode/buff'
import {
WitnessCommitPreImage,
WitnessDataPreImage
} from '../../types/index.js'
} from '@/core/types/index.js'

/**
* Returns a serialized preimage
Expand Down
1 change: 0 additions & 1 deletion src/core/schema/machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const vm_closed = z.object({
const close_state = z.discriminatedUnion('closed', [ vm_open, vm_closed ])

const submit_req = z.object({
vmid : hash,
witness : wit.data
})

Expand Down
1 change: 0 additions & 1 deletion src/core/types/machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export interface MachineConfig {
}

export interface VMSubmitRequest {
vmid : string
witness : WitnessData
}

Expand Down
12 changes: 6 additions & 6 deletions src/core/validation/witness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,18 @@ export function verify_witness_sigs (

export function verify_witness_commit (
commit : WitnessCommit,
vmdata : MachineData,
vmstate : MachineData,
witness : WitnessData
) {
const { commit_id, commit_sig, agent_pk } = commit

// Don't forget to check that vm matches commit.
assert.ok(witness.vmid === vmdata.vmid, 'provided vmdata and witness vmid does not match')
assert.ok(witness.stamp === vmdata.commit_at, 'provided vmdata and witness stamp does not match')
assert.ok(witness.vmid === vmstate.vmid, 'provided vmstate and witness vmid does not match')
assert.ok(witness.stamp === vmstate.commit_at, 'provided vmstate and witness stamp does not match')
assert.ok(witness.vmid === commit.vmid, 'commit vmid does not match witness')
assert.ok(vmdata.head === commit.vm_head, 'commit vm_head does not match vmdata head')
assert.ok(vmdata.output === commit.vm_output, 'commit vm_output does not match vmdata output')
assert.ok(vmdata.step === commit.vm_step, 'commit vm_step does not match vmdata step count')
assert.ok(vmstate.head === commit.vm_head, 'commit vm_head does not match vmstate head')
assert.ok(vmstate.output === commit.vm_output, 'commit vm_output does not match vmstate output')
assert.ok(vmstate.step === commit.vm_step, 'commit vm_step does not match vmstate step count')

const int_wid = get_witness_id(commit)
const int_rid = get_commit_id(commit)
Expand Down

0 comments on commit 013358d

Please sign in to comment.