Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cmd committed Oct 16, 2023
1 parent 3bb93e3 commit 5434625
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 71 deletions.
46 changes: 21 additions & 25 deletions src/lib/contract.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { Buff, Bytes } from '@cmdcode/buff'
import { hash340 } from '@cmdcode/crypto-tools/hash'
import { DEFAULT_DEADLINE } from '../config.js'
import { Signer } from '../signer.js'
import { create_session } from './session.js'
import { create_settlment } from './spend.js'
import { now } from './util.js'
import { DEFAULT_DEADLINE } from '../config.js'
import { Signer } from '../signer.js'
import { create_settlment } from './spend.js'
import { now } from './util.js'

import {
get_path_names,
get_path_vouts,
get_pay_total,
get_prop_id
} from './proposal.js'

import {
Expand All @@ -20,6 +16,7 @@ import {
} from '../vm/main.js'

import {
AgentSession,
ContractConfig,
ContractData,
Payment,
Expand All @@ -30,24 +27,23 @@ import {
import * as assert from '../assert.js'

export function create_contract (
agent : Signer,
cid : string,
proposal : ProposalData,
options ?: Partial<ContractConfig>
session : AgentSession,
options : Partial<ContractConfig> = {}
) : ContractData {
const { aux = [], fees = [], published = now() } = options ?? {}
const cid = get_contract_id(proposal, published, ...aux)
const { fees = [], published = now() } = options

return {
cid,
fees,
published,
session,
activated : null,
agent_id : agent.id,
cid : cid.hex,
deadline : get_deadline(proposal, published),
expires : null,
funds : [],
outputs : get_spend_outputs(proposal, fees),
session : create_session(agent, cid),
state : null,
status : 'published',
terms : proposal,
Expand All @@ -57,16 +53,16 @@ export function create_contract (
}
}

export function get_contract_id (
proposal : ProposalData,
published : number,
...aux : Bytes[]
) {
/** Calculate the session id from the proposal and agent session. */
const prop_id = get_prop_id(proposal)
const stamp = Buff.num(published, 4)
return hash340('contract/cid', prop_id, stamp, ...aux)
}
// export function get_contract_id (
// proposal : ProposalData,
// published : number,
// ...aux : Bytes[]
// ) {
// /** Calculate the session id from the proposal and agent session. */
// const prop_id = get_prop_id(proposal)
// const stamp = Buff.num(published, 4)
// return hash340('contract/cid', prop_id, stamp, ...aux)
// }

export function get_deadline (
proposal : ProposalData,
Expand Down
7 changes: 0 additions & 7 deletions src/lib/proposal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Buff } from '@cmdcode/buff'
import { parse_addr } from '@scrow/tapscript/address'
import { create_vout } from '@scrow/tapscript/tx'
import { TxOutput } from '@scrow/tapscript'
Expand Down Expand Up @@ -71,9 +70,3 @@ export function get_path_total (
}
return path_totals
}

export function get_prop_id (
proposal : ProposalData
) {
return Buff.json(proposal).digest
}
16 changes: 8 additions & 8 deletions src/lib/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ export function create_session (
agent : Signer,
cid : Bytes
) : AgentSession {
const aid = agent.id
const sid = get_session_id(aid, cid)
const sid = get_session_id(agent.id, cid)
return {
sid : sid.hex,
pnonce : get_session_pnonce(sid, agent).hex,
pubkey : agent.pubkey
agent_id : agent.id,
sid : sid.hex,
pnonce : get_session_pnonce(sid, agent).hex,
pubkey : agent.pubkey
}
}

Expand All @@ -52,13 +52,13 @@ export function create_covenant (
deposit : Deposit,
signer : Signer
) : CovenantData {
const { session } = contract
const sid = session.sid
const { session } = contract
const { agent_id, sid } = session
const pnonce = get_session_pnonce(sid, signer).hex
const pnonces = [ pnonce, session.pnonce ]
const mupaths = get_mutex_entries(contract, deposit, pnonces)
const psigs = create_path_psigs(mupaths, signer)
return { sid, pnonce, psigs }
return { agent_id, sid, pnonce, psigs }
}

export function parse_covenant (
Expand Down
8 changes: 4 additions & 4 deletions src/schema/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ const vm_status = z.enum([ 'init', 'open', 'hold', 'disputed', 'closed' ])
const output = z.tuple([ label, vout.array() ])

const session = z.object({
sid : hash,
pnonce : nonce,
pubkey : hash,
agent_id : hash,
sid : hash,
pnonce : nonce,
pubkey : hash,
})

const state = z.object({
Expand Down Expand Up @@ -50,7 +51,6 @@ const data = z.object({
status,
tx,
activated : stamp.nullable(),
agent_id : hash,
cid : hash,
deadline : stamp,
expires : stamp.nullable(),
Expand Down
7 changes: 4 additions & 3 deletions src/schema/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import {
import { txinput } from './tx.js'

const covenant = z.object({
sid : nonce,
pnonce : nonce,
psigs : z.tuple([ str, str ]).array()
agent_id : hash,
sid : nonce,
pnonce : nonce,
psigs : z.tuple([ str, str ]).array()
})

const template = z.object({
Expand Down
3 changes: 0 additions & 3 deletions src/types/contract.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Bytes } from '@cmdcode/buff'
import { TxOutput } from '@scrow/tapscript'
import { Literal } from './base.js'
import { Deposit } from './deposit.js'
Expand All @@ -18,14 +17,12 @@ export type SpendOutput = [
]

export interface ContractConfig {
aux : Bytes[]
fees : Payment[]
published : number
}

export interface ContractData {
activated : null | number
agent_id : string
cid : string
deadline : number
expires : null | number
Expand Down
14 changes: 8 additions & 6 deletions src/types/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ export type MutexEntry = [
]

export interface AgentSession {
sid : string,
pubkey : string,
pnonce : string
agent_id : string,
sid : string,
pubkey : string,
pnonce : string
}

export interface CovenantData {
sid : string
pnonce : string
psigs : [ string, string ][]
agent_id : string
sid : string
pnonce : string
psigs : [ string, string ][]
}

export interface MutexContext {
Expand Down
4 changes: 2 additions & 2 deletions src/validators/covenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ function check_session_agent (
agent : Signer,
contract : ContractData
) {
const { agent_id, session } = contract
assert.ok(agent_id === agent.id, 'Agent ID does not match session.')
const { session } = contract
assert.ok(session.agent_id === agent.id, 'Agent ID does not match session.')
const pnonce = get_session_pnonce(session.sid, agent)
assert.ok(pnonce.hex === session.pnonce, 'Agent pnonce does not match session.')
}
Expand Down
6 changes: 5 additions & 1 deletion test/scratch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
} from '@scrow/core/validate'

import vgen from './src/vectorgen.js'
import { create_session } from '@scrow/core/session'
import { Buff } from '@cmdcode/buff'

/* ------------------- [ Init ] ------------------- */

Expand Down Expand Up @@ -61,7 +63,9 @@ console.dir(proposal, { depth : null })

/* ------------------- [ Contract ] ------------------- */

const contract = create_contract(agent.signer, proposal)
const cid = Buff.random().hex
const session = create_session(agent.signer, cid)
const contract = create_contract(cid, proposal, session)

console.log(banner('contract'))
console.dir(contract, { depth : null })
Expand Down
12 changes: 0 additions & 12 deletions test/src/vectorgen.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Buff } from '@cmdcode/buff'
import { create_sequence } from '@scrow/tapscript/tx'
import { Signer } from '@scrow/core/signer'
import { parse_payments } from '@scrow/core/parse'
import { create_contract } from '@scrow/core/contract'
import { create_covenant } from '@scrow/core/session'
import { parse_timelock } from '@scrow/core/tx'
import { now } from '@scrow/core/util'
Expand Down Expand Up @@ -121,15 +119,6 @@ export async function gen_deposits (
return deposits
}

async function gen_contract (
agent : MemberData,
proposal : ProposalData
) : Promise<ContractData> {
const address = await agent.wallet.new_address
const fees = parse_payments([[ 1000, address ]])
return create_contract(agent.signer, proposal, { fees })
}

async function gen_funds (
contract : ContractData,
deposits : Deposit[],
Expand Down Expand Up @@ -168,7 +157,6 @@ async function gen_witness (
export default {
agent : gen_agent,
deposits : gen_deposits,
contract : gen_contract,
funds : gen_funds,
members : gen_members,
proposal : gen_proposal,
Expand Down

0 comments on commit 5434625

Please sign in to comment.