Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cmd committed Oct 2, 2023
1 parent d5f243c commit 6402b75
Show file tree
Hide file tree
Showing 49 changed files with 1,538 additions and 456 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Integration Tests](https://github.com/BitEscrow/escrow-core/actions/workflows/integration.yml/badge.svg?branch=master)](https://github.com/BitEscrow/escrow-core/actions/workflows/integration.yml)

# escrow-core

Core library for implenting the escrow protocol.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/assert.ts → src/assert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Network } from '../types/index.js'
import { Network } from './types/index.js'

export function ok (
value : unknown,
Expand Down
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ export const MIN_WINDOW = _HOUR * 2
export const MAX_WINDOW = _DAY * 30
export const GRACE_PERIOD = _DAY * 2
export const MAX_MULTISIG = 100
export const STAMP_THOLD = 500_000_000

export const DEFAULT_DEADLINE = MIN_WINDOW
export const DEFAULT_EXPIRES = MIN_WINDOW
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export * as assert from './lib/assert.js'
export * as assert from './assert.js'
export * as config from './config.js'
export * as lib from './lib/index.js'
export * as schema from './schema/index.js'
export * as validate from './validators/index.js'
export * as vm from './vm/index.js'

export * from './types/index.js'
36 changes: 17 additions & 19 deletions src/lib/context.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { Bytes } from '@cmdcode/buff'
import { tap_pubkey } from '@scrow/tapscript/tapkey'
import { TxPrevout } from '@scrow/tapscript'
import { GRACE_PERIOD } from '../config.js'

import {
create_sighash,
get_refund_script
} from './tx.js'
import { TxPrevout } from '@scrow/tapscript'

import {
create_ctx,
Expand All @@ -17,7 +12,6 @@ import {

import {
get_path_templates,
get_program_hashes
} from './proposal.js'

import {
Expand All @@ -27,34 +21,37 @@ import {
} from './session.js'

import {
AgentData,
create_sighash,
get_refund_script
} from './tx.js'

import {
AgentSession,
DepositContext,
ProposalData,
SessionContext,
SessionEntry
} from '../types/index.js'

export function get_deposit_ctx (
agent : AgentData,
proposal : ProposalData,
agent : AgentSession,
deposit_pub : Bytes
) : DepositContext {
const members = [ deposit_pub, agent.pubkey ]
const locktime = get_deposit_locktime(agent, proposal)
const members = [ deposit_pub, agent.signing_key ]
const locktime = get_deposit_locktime(proposal, agent)
const script = get_refund_script(deposit_pub, locktime)
const int_data = get_key_ctx(members)
const tap_data = tap_pubkey(int_data.group_pubkey, { script })
const key_data = tweak_key_ctx(int_data, [ tap_data.taptweak ])
const programs = get_program_hashes(proposal)
const session_id = get_session_id(proposal)
const templates = get_path_templates(agent, proposal)
const session_id = get_session_id(proposal, agent)
const templates = get_path_templates(proposal, agent)

return {
agent,
key_data,
locktime,
proposal,
programs,
session_id,
tap_data,
templates,
Expand All @@ -80,7 +77,7 @@ export function get_session_ctx (
}
}

export function get_full_ctx (
export function get_sessions (
deposit_ctx : DepositContext,
session_pubs : Bytes[],
txinput : TxPrevout
Expand All @@ -94,10 +91,11 @@ export function get_full_ctx (
}

function get_deposit_locktime (
agent : AgentData,
proposal : ProposalData
proposal : ProposalData,
agent : AgentSession
) : number {
const created = agent.created_at
const expires = proposal.schedule.expires
const expires = proposal.expires

return created + expires + GRACE_PERIOD
}
51 changes: 51 additions & 0 deletions src/lib/contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Buff, Bytes } from '@cmdcode/buff'
import { hash340 } from '@cmdcode/crypto-tools/hash'
import { init_vm } from '../vm/vm.js'
import { parse_txin } from './parse.js'
import { get_session_id } from './session.js'
import { now } from './util.js'

import {
AgentSession,
ContractData,
DepositData,
ProposalData
} from '../types/index.js'

export function get_contract (
deposits : DepositData[],
proposal : ProposalData,
session : AgentSession,
published : number = now()
) : ContractData {
const sid = get_session_id(proposal, session)
const cid = get_contract_id(deposits, sid, published)
const vin = get_deposit_vin(deposits)

return {
cid,
deposits,
published,
agent : session,
members : [],
state : init_vm(cid, proposal, published),
terms : proposal,
total : vin.reduce((p, n) => p + Number(n.prevout.value), 0),
witness : []
}
}

function get_contract_id (
deposits : DepositData[],
session_id : Bytes,
published : number
) {
const stamp = Buff.num(published, 4)
const txids = deposits.map(e => parse_txin(e.txinput).txid)
const preimg = Buff.join([ session_id, stamp, ...txids.sort() ])
return hash340('escrow/contract_id', preimg).hex
}

function get_deposit_vin (deposits : DepositData[]) {
return deposits.map(e => parse_txin(e.txinput))
}
28 changes: 15 additions & 13 deletions src/lib/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import {

import {
get_deposit_ctx,
get_full_ctx
get_sessions
} from './context.js'

import {
AgentData,
AgentSession,
DepositContext,
DepositTemplate,
DepositData,
ProposalData,
SessionContext,
SessionEntry
} from '../types/index.js'

import * as assert from './assert.js'
import * as assert from '../assert.js'

export function get_deposit_address (
context : DepositContext,
Expand All @@ -35,24 +35,26 @@ export function get_deposit_address (
}

export function create_deposit (
agent : AgentData,
proposal : ProposalData,
agent : AgentSession,
signer : Signer,
txdata : TxBytes | TxData
) : DepositTemplate {
const context = get_deposit_ctx(agent, proposal, signer.pubkey)
) : DepositData {
const context = get_deposit_ctx(proposal, agent, signer.pubkey)
const group_pub = context.group_pub
const txinput = parse_prevout(group_pub, txdata)
assert.ok(txinput !== null)
const recover_psig = 'deadbeef' //get_recover_psig()
const session_pnonce = get_session_key(context, signer)
const session_pnonces = [ session_pnonce, agent.pnonce ]
const sessions_ctx = get_full_ctx(context, session_pnonces, txinput)
const session_pnonces = [ session_pnonce, agent.session_key ]
const sessions_ctx = get_sessions(context, session_pnonces, txinput)
const session_psigs = create_deposit_psigs(sessions_ctx, signer)
return {
pubkey : signer.pubkey.hex,
pnonce : session_pnonce.hex,
psigs : session_psigs,
txvin : Buff.json(txinput).to_bech32m('txvin')
deposit_key : signer.pubkey.hex,
recover_sig : recover_psig,
session_key : session_pnonce.hex,
signatures : session_psigs,
txinput : Buff.json(txinput).to_bech32m('txvin'),
}
}

Expand Down
17 changes: 11 additions & 6 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export * as ctx from './context.js'
export * as prop from './proposal.js'
export * as refund from './refund.js'
export * as spend from './spend.js'
export * as tx from './tx.js'
export * as util from './util.js'
export * as ctx from './context.js'
export * as contract from './contract.js'
export * as deposit from './deposit.js'
export * as program from './program.js'
export * as proposal from './proposal.js'
export * as refund from './refund.js'
export * as session from './session.js'
export * as spend from './spend.js'
export * as tx from './tx.js'
export * as util from './util.js'
export * as witness from './witness.js'
67 changes: 67 additions & 0 deletions src/lib/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Buff } from '@cmdcode/buff'
import { create_prevout } from '@scrow/tapscript/tx'

import {
ProposalData,
ProgramData,
ProgramTerms,
WitnessEntry,
WitnessData,
MachineState,
ContractState,
ContractData
} from '../types/index.js'

import * as schema from '../schema/index.js'

export function parse_proposal (
proposal : Record<string, any>
) : ProposalData {
return schema.proposal.parse(proposal) as ProposalData
}

export function parse_txin (encoded : string) {
const txinput = Buff.bech32m(encoded).to_json()
return create_prevout(txinput)
}

export function parse_exp (paths : string[], expr : string) {
let blist : string[], wlist : string[]
if (expr === '*') {
wlist = paths
blist = []
} else if (expr.includes('|')) {
wlist = expr.split('|')
blist = paths.filter(e => !wlist.includes(e))
} else {
wlist = [ expr ]
blist = paths.filter(e => e !== expr)
}
return { wlist, blist }
}

export function parse_program (terms : ProgramTerms) : ProgramData {
const [ actions, paths, method, ...literal ] = terms
const params = literal.map(e => String(e))
const id = Buff.json(terms.slice(2)).digest.hex
return { actions, id, method, params, paths }
}

export function parse_contract (contract : unknown) {
return schema.contract.data.parse(contract) as ContractData
}

export function parse_vm (state : MachineState) : ContractState {
const { log, paths, progs, store, tasks, ...cstate } = state
const pmap = [ ...paths.entries() ]
const smap = [ ...store.entries() ]
const dmap = smap.map(e => [ e[0], [ ...e[1].entries() ]])
return { ...cstate, paths : pmap, store : dmap }
}

export function parse_witness (witness : WitnessEntry) : WitnessData {
const wit = schema.contract.witness
const [ stamp, action, path, prog_id, ...args ] = wit.parse(witness)
const id = Buff.json(witness.slice(0, 4)).digest.hex
return { action, args, id, path, prog_id, stamp }
}
Loading

0 comments on commit 6402b75

Please sign in to comment.