Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cmd committed Feb 2, 2024
1 parent 5148931 commit 077cd09
Show file tree
Hide file tree
Showing 20 changed files with 203 additions and 107 deletions.
4 changes: 2 additions & 2 deletions demo/07_deposit_funds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ const btc_total = amt_total / 100_000_000

/** ========== [ Print Deposit Info ] ========== **/

if (DEMO_MODE) {
if (DEMO_MODE || config.network !== 'regtest') {
print_banner('make a deposit')
console.log('copy this address :', address)
console.log('send this amount :', `${amt_total} sats || ${btc_total} btc`)
console.log('get funds here :', config.faucet, '\n')
} else if (config.network === 'regtest') {
} else {
print_banner('sending deposit')
await fund_address(address, amt_total)
await sleep(2000)
Expand Down
Empty file added demo/api/deposit/close.ts
Empty file.
Empty file added demo/api/deposit/digest.ts
Empty file.
Empty file added demo/api/deposit/lock.ts
Empty file.
20 changes: 9 additions & 11 deletions demo/api/deposit/read.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { EscrowClient } from '@scrow/core/client'
import { config } from '../../00_demo_config.js'

const dpid = '798e5e4a51e60dea79690dcd3114f65fa510c539514e8f89d6a22beaed98473a'

const client = new EscrowClient(config)

// Request an account for the member to use.
const res = await client.deposit.read(dpid)
import { print_banner } from '@scrow/test'
import { client } from '@scrow/demo/01_create_client.js'
import { locked_deposit } from '@scrow/demo/07_deposit_funds.js'

// Request to read a deposit via dpid.
const res = await client.deposit.read(locked_deposit.dpid)
// Check the response is valid.
if (!res.ok) throw new Error(res.error)
// Unpack the data response
const deposit = res.data.deposit

const { deposit } = res.data

print_banner('locked deposit')
console.dir(deposit, { depth : null })
console.log('\n')
Empty file added demo/api/deposit/register.ts
Empty file.
Empty file added demo/api/deposit/request.ts
Empty file.
Empty file added demo/api/server/keys.ts
Empty file.
Empty file added demo/api/server/policy.ts
Empty file.
Empty file added demo/api/server/status.ts
Empty file.
Empty file added demo/api/witness/read.ts
Empty file.
9 changes: 6 additions & 3 deletions demo/vm/eval.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { now } from '@scrow/core/util'
import { VMUtil } from '@scrow/test'
import { now } from '@scrow/core/util'

import { print_banner, VMUtil } from '@scrow/test'

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

Expand All @@ -16,5 +17,7 @@ const config = VMUtil.parse_config({ activated, cid, pathnames, programs : pro
const witness = VMUtil.compile_witness(progs, statements)
// Run VM with witness and final timestamp.
const vm_state = VMUtil.run_vm(config, duration, witness)
// Print results to console.

print_banner('vm state')
console.dir(vm_state, { depth: null })
console.log('\n')
46 changes: 46 additions & 0 deletions src/client/api/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { EscrowClient } from '../class/client.js'

import {
ServerKeysResponse,
ServerPolicyResponse,
ServerStatusResponse
} from '@/types/api/server.js'

import {
ApiResponse
} from '@/types/index.js'

function server_keys_api (client : EscrowClient) {
return async () : Promise<ApiResponse<ServerKeysResponse>> => {
// Formulate the request.
const url = `${client.host}/api/server/keys`
// Return a response.
return client.fetcher<ServerKeysResponse>({ url })
}
}

function server_policy_api (client : EscrowClient) {
return async () : Promise<ApiResponse<ServerPolicyResponse>> => {
// Formulate the request.
const url = `${client.host}/api/server/policy`
// Return a response.
return client.fetcher<ServerPolicyResponse>({ url })
}
}

function server_status_api (client : EscrowClient) {
return async () : Promise<ApiResponse<ServerStatusResponse>> => {
// Formulate the request.
const url = `${client.host}/api/server/status`
// Return a response.
return client.fetcher<ServerStatusResponse>({ url })
}
}

export default function (client : EscrowClient) {
return {
keys : server_keys_api(client),
policy : server_policy_api(client),
status : server_status_api(client)
}
}
67 changes: 67 additions & 0 deletions src/client/api/statement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

import { EscrowSigner } from '@/client/class/signer.js'
import { find_program } from '@/lib/proposal.js'
import { validate_witness } from '@/validators/program.js'

import {
create_witness,
sign_witness
} from '@/lib/witness.js'

import {
ContractData,
WitnessData,
WitnessTemplate
} from '@/types/index.js'

export function can_sign_api (signer : EscrowSigner) {
return (
contract : ContractData,
template : WitnessTemplate | WitnessData
) => {
const { action, path, method } = template
const terms = contract.terms

if (!signer.membership.exists(terms)) {
return false
}

const cred = signer.membership.claim(terms)
const query = { action, path, method, includes: [ cred.data.pub ] }
const prog = find_program(query, terms.programs)
return prog !== undefined
}
}

export function sign_witness_api (signer : EscrowSigner) {
return (
contract : ContractData,
template : WitnessTemplate
) => {
const terms = contract.terms
const cred = signer.membership.claim(terms)
let wdat = create_witness(terms.programs, cred.data.pub, template)
validate_witness(contract, wdat)
return sign_witness(cred.signer, wdat)
}
}

export function endorse_witness_api (signer : EscrowSigner) {
return (
contract : ContractData,
witness : WitnessData
) => {
const terms = contract.terms
const cred = signer.membership.claim(terms)
validate_witness(contract, witness)
return sign_witness(cred.signer, witness)
}
}

export default function (signer : EscrowSigner) {
return {
can_sign : can_sign_api(signer),
endorse : endorse_witness_api(signer),
sign : sign_witness_api(signer),
}
}
27 changes: 0 additions & 27 deletions src/client/api/wid.ts

This file was deleted.

74 changes: 17 additions & 57 deletions src/client/api/witness.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,27 @@

import { EscrowSigner } from '@/client/class/signer.js'
import { find_program } from '@/lib/proposal.js'
import { validate_witness } from '@/validators/program.js'

import {
create_witness,
sign_witness
} from '@/lib/witness.js'
import { EscrowClient } from '../class/client.js'

import {
ContractData,
WitnessData,
WitnessTemplate
ApiResponse,
WitnessDataResponse
} from '@/types/index.js'

export function can_sign_api (signer : EscrowSigner) {
return (
contract : ContractData,
template : WitnessTemplate | WitnessData
) => {
const { action, path, method } = template
const terms = contract.terms

if (!signer.membership.exists(terms)) {
return false
}

const cred = signer.membership.claim(terms)
const query = { action, path, method, includes: [ cred.data.pub ] }
const prog = find_program(query, terms.programs)
return prog !== undefined
}
}

export function sign_witness_api (signer : EscrowSigner) {
return (
contract : ContractData,
template : WitnessTemplate
) => {
const terms = contract.terms
const cred = signer.membership.claim(terms)
let wdat = create_witness(terms.programs, cred.data.pub, template)
validate_witness(contract, wdat)
return sign_witness(cred.signer, wdat)
}
}

export function endorse_witness_api (signer : EscrowSigner) {
return (
contract : ContractData,
witness : WitnessData
) => {
const terms = contract.terms
const cred = signer.membership.claim(terms)
validate_witness(contract, witness)
return sign_witness(cred.signer, witness)
import * as assert from '@/assert.js'

function read_witness_api (client : EscrowClient) {
return async (
wid : string
) : Promise<ApiResponse<WitnessDataResponse>> => {
// Validate witness id.
assert.is_hash(wid)
// Formulate the request.
const url = `${client.host}/api/witness/${wid}`
// Return a response.
return client.fetcher<WitnessDataResponse>({ url })
}
}

export default function (signer : EscrowSigner) {
export default function (client : EscrowClient) {
return {
can_sign : can_sign_api(signer),
endorse : endorse_witness_api(signer),
sign : sign_witness_api(signer),
read : read_witness_api(client)
}
}
4 changes: 3 additions & 1 deletion src/client/class/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { Network } from '@/types/index.js'
import contract_api from '@/client/api/contract.js'
import deposit_api from '@/client/api/deposit.js'
import oracle_api from '@/client/api/oracle.js'
import witness_api from '@/client/api/wid.js'
import server_api from '@/client/api/server.js'
import witness_api from '@/client/api/witness.js'

import {
ClientConfig,
Expand Down Expand Up @@ -48,6 +49,7 @@ export class EscrowClient {
contract = contract_api(this)
deposit = deposit_api(this)
oracle = oracle_api(this)
server = server_api(this)
witness = witness_api(this)

toJSON() {
Expand Down
12 changes: 6 additions & 6 deletions src/client/class/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
Wallet
} from '@cmdcode/signer'

import account_api from '../api/account.js'
import member_api from '../api/member.js'
import proposal_api from '../api/proposal.js'
import request_api from '../api/request.js'
import witness_api from '../api/witness.js'
import account_api from '../api/account.js'
import member_api from '../api/member.js'
import proposal_api from '../api/proposal.js'
import request_api from '../api/request.js'
import statement_api from '../api/statement.js'

import {
CredSignerAPI,
Expand Down Expand Up @@ -130,7 +130,7 @@ export class EscrowSigner {
}
}

witness = witness_api(this)
witness = statement_api(this)

save (password : string) {
const pass = Buff.str(password)
Expand Down
13 changes: 13 additions & 0 deletions src/types/api/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ServerConfig } from '../server.js'

export interface ServerKeysResponse {
pubkeys : string[]
}

export interface ServerPolicyResponse {
policy : ServerConfig
}

export interface ServerStatusResponse {
status : string
}
34 changes: 34 additions & 0 deletions src/types/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export interface ServerConfig {
contract : {
AGENT_FEE : number
UPDATE_IVAL : number
}

deposit : {
DEFAULT_LOCKTIME : number
STALE_WINDOW : number
UPDATE_IVAL : number
}

general : {
STAMP_THOLD : number
}

proposal : {
ACTION_LIST : string[]
METHOD_LIST : string[]
DEFAULT_NETWORK : string
DEFAULT_DEADLINE : number
DEFAULT_EXPIRES : number
MIN_DEADLINE : number
MAX_EFFECT : number
MIN_EXPIRY : number
MAX_EXPIRY : number
MIN_FEERATE : number
MAX_FEERATE : number
MIN_WINDOW : number
MAX_WINDOW : number
GRACE_PERIOD : number
MAX_MULTISIG : number
}
}

0 comments on commit 077cd09

Please sign in to comment.