-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cmd
committed
Oct 2, 2023
1 parent
d5f243c
commit 6402b75
Showing
49 changed files
with
1,538 additions
and
456 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |
Oops, something went wrong.