Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdruid committed Aug 6, 2024
2 parents 98a5333 + 0e14c1e commit 21fd07b
Show file tree
Hide file tree
Showing 19 changed files with 148 additions and 458 deletions.
342 changes: 7 additions & 335 deletions README.md

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions demo/02_create_signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export const signers = config.members.map(alias => {
return EscrowSigner.import(config.client).from_phrase(alias)
})

/**
* Define a signer that will be used for funding.
*/
export const funder = signers[0]

if (DEMO_MODE) {
print_banner('signing members')
console.log(config.members)
Expand Down
8 changes: 4 additions & 4 deletions demo/04_finish_proposal.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { print_banner } from '@scrow/test'
import { DraftUtil } from '@scrow/sdk/client'

import { signers } from './02_create_signer.js'
import { proposal, roles } from './03_create_proposal.js'
import { signers } from '@scrow/demo/02_create_signer.js'
import { proposal, roles } from '@scrow/demo/03_create_proposal.js'

const DEMO_MODE = process.env.VERBOSE === 'true'

Expand Down Expand Up @@ -38,8 +38,8 @@ signers.forEach(mbr => {
})

/**
* Verify the proposal is complete, all positions are
* filled, and endorsements are valid.
* Validate the proposal data, and check
* each member position is filled properly.
*/
DraftUtil.verify(draft)

Expand Down
30 changes: 22 additions & 8 deletions demo/05_create_contract.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import CVM from '@scrow/sdk/cvm'
import { print_banner } from '@scrow/test'
import { print_banner } from '@scrow/test'
import { DEFAULT_POLICY } from '@scrow/sdk/client'
import { verify_publish_req } from '@scrow/sdk/verify'
import CVM from '@scrow/sdk/cvm'

import { client } from './01_create_client.js'
import { publish_req } from './04_finish_proposal.js'
import { client } from './01_create_client.js'
import { publish_req } from './04_finish_proposal.js'

const DEMO_MODE = process.env.VERBOSE === 'true'

/**
* We will need to pass in a reference to the scripting engine
* defined in the proposal, so that it can verify the terms set
* for each program.
* To verify a proposal for publishing, we need to
* pass in a reference to the scripting engine that
* will be used, so it can verify the program terms.
*/
const engine = CVM

/**
* We also need a reference to the server's escrow
* policy, which may change depending on the server.
*/
const policy = DEFAULT_POLICY.proposal

/**
* Verify that the publish request is valid, and
* fits within the policy of the escrow server.
*/
verify_publish_req(engine, policy, publish_req)

/**
* Request to create a new contract on the escrow server.
*/
const res = await client.contract.create(publish_req, engine)
const res = await client.contract.create(publish_req)
// Check the server response is valid.
if (!res.ok) throw new Error(res.error)

Expand Down
11 changes: 6 additions & 5 deletions demo/api/account/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
import { print_banner } from '@scrow/test'
import { get_contract_balance } from '@scrow/sdk/contract'
import { sleep } from '@scrow/sdk/util'
import { config } from '@scrow/demo/00_demo_config.js'
import { client } from '@scrow/demo/01_create_client.js'
import { signers } from '@scrow/demo/02_create_signer.js'
import { new_contract } from '@scrow/demo/05_create_contract.js'
import { new_account } from '@scrow/demo/06_request_account.js'

import { config } from '@scrow/demo/00_demo_config.js'
import { client } from '@scrow/demo/01_create_client.js'
import { signers } from '@scrow/demo/02_create_signer.js'
import { new_contract } from '@scrow/demo/05_create_contract.js'
import { new_account } from '@scrow/demo/06_request_account.js'

import {
fund_mutiny_address,
Expand Down
24 changes: 24 additions & 0 deletions demo/api/account/recover.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { print_banner } from '@scrow/test'
import { now } from '@scrow/sdk/util'

import { config } from '@scrow/demo/00_demo_config.js'
import { client } from '@scrow/demo/01_create_client.js'
import { funder } from '@scrow/demo/02_create_signer.js'
import { new_account } from '@scrow/demo/06_request_account.js'

// Define an address to receive the funds.
const address = funder.wallet.new(now())
// Define a feerate for the return transaction.
const ret_rate = config.feerate
// Grab the first utxo from the deposit account.
const res = await client.oracle.get_first_utxo(new_account.deposit_addr)
// Check that the response is valid.
if (res === null) throw new Error('utxo not found')
// Create a signed recovery transaction for the utxo.
const txhex = funder.account.recover(new_account, address, ret_rate, res.utxo)
// Deliver our registration request to the server.
const txid = await client.oracle.broadcast_tx(txhex)

print_banner('recovery txid')
console.log(txid)
console.log('\n')
23 changes: 5 additions & 18 deletions src/client/api/client/contract.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { assert, parser } from '@/core/util/index.js'
import { assert } from '@/core/util/index.js'
import { create_publish_req } from '@/core/module/contract/index.js'
import { EscrowClient } from '@/client/class/client.js'
import { DEFAULT_POLICY } from '@/client/config/index.js'

import {
verify_contract_session,
verify_contract_sigs,
verify_endorsements,
verify_proposal_data
verify_contract_sigs
} from '@/core/validation/index.js'

import {
ContractPublishRequest,
ContractSession,
ProposalPolicy,
ScriptEngineAPI
PublishRequest,
ContractSession
} from '@/core/types/index.js'

import {
Expand All @@ -31,18 +26,10 @@ function create_contract_api (
client : EscrowClient
) {
return async (
request : ContractPublishRequest,
engine : ScriptEngineAPI,
policy : ProposalPolicy = DEFAULT_POLICY.proposal
request : PublishRequest
) : Promise<ApiResponse<ContractDataResponse>> => {
// Unpack configurations from client.
const { endorsements, proposal } = request
// Parse and validate the proposal.
const prop = parser.parse_proposal(proposal)
// Verify the proposal's terms.
verify_proposal_data(engine, policy, prop)
// Verify any signatures.
verify_endorsements(prop, endorsements)
// Create a contract publish request.
const req = create_publish_req(proposal, endorsements)
// Formulate the request url.
Expand Down
6 changes: 4 additions & 2 deletions src/client/api/client/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import {
create_session,
decode_session,
encode_session,
publish_session
publish_session,
verify_session
} from '@/client/lib/session.js'

export default function (_client : EscrowClient) {
return {
create : create_session,
decode : decode_session,
encode : encode_session,
publish : publish_session
publish : publish_session,
verify : verify_session
}
}
19 changes: 19 additions & 0 deletions src/client/api/signer/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ContractData,
CommitRequest
} from '@/core/types/index.js'
import { get_recovery_config, get_recovery_tx, sign_recovery_tx } from '@/core/lib/recovery.js'

export function request_account_api (esigner : EscrowSigner) {
return (
Expand Down Expand Up @@ -46,6 +47,23 @@ export function verify_account_api (esigner : EscrowSigner) {
}
}

export function recover_funds_api (esigner : EscrowSigner) {
return (
account : AccountData,
address : string,
feerate : number,
utxo : TxOutput
) : string => {
const { server_pk, _signer } = esigner
// Assert the correct pubkey is used by the server.
assert.ok(server_pk === account.agent_pk, 'invalid server pubkey')
verify_account_data(account, _signer)
const config = get_recovery_config(account)
const templ = get_recovery_tx(config, feerate, address, utxo)
return sign_recovery_tx(config, _signer, templ, utxo)
}
}

export function register_funds_api (esigner : EscrowSigner) {
return (
account : AccountData,
Expand Down Expand Up @@ -78,6 +96,7 @@ export function commit_funds_api (esigner : EscrowSigner) {
export default function (esigner : EscrowSigner) {
return {
commit : commit_funds_api(esigner),
recover : recover_funds_api(esigner),
request : request_account_api(esigner),
register : register_funds_api(esigner),
verify : verify_account_api(esigner)
Expand Down
7 changes: 5 additions & 2 deletions src/client/lib/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Buff } from '@cmdcode/buff'
import { assert } from '@/core/util/index.js'
import { has_membership } from './membership.js'

import { verify_endorsements } from '@/core/validation/contract.js'
import { validate_proposal_data } from '@/core/validation/proposal.js'

import {
Expand All @@ -12,7 +13,7 @@ import {
} from '@/core/lib/proposal.js'

import {
ContractPublishRequest,
PublishRequest,
SignerAPI
} from '@/core/types/index.js'

Expand Down Expand Up @@ -145,8 +146,10 @@ export function verify_session (

export function publish_session (
session : DraftSession
) : ContractPublishRequest {
) : PublishRequest {
const { proposal, sigs: endorsements } = session
validate_proposal_data(proposal)
verify_endorsements(proposal, endorsements)
return { endorsements, proposal }
}

Expand Down
18 changes: 16 additions & 2 deletions src/core/lib/proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ type PathTotal = [ path: string, total : number ]
const PROPOSAL_DEFAULTS = () => {
return {
created_at : now(),
deadline : CONST.DEADLINE_DEFAULT,
duration : CONST.DURATION_DEFAULT,
engine : CONST.ENGINE_DEFAULT,
paths : [],
Expand All @@ -43,7 +42,8 @@ const PROPOSAL_DEFAULTS = () => {
}

export function create_proposal (template : ProposalTemplate) {
return parser.parse_proposal({ ...PROPOSAL_DEFAULTS(), ...template })
const deadline = get_proposal_deadline(template)
return parser.parse_proposal({ ...PROPOSAL_DEFAULTS(), ...template, deadline })
}

/**
Expand Down Expand Up @@ -140,6 +140,20 @@ export function get_path_total (
return path_totals
}

export function get_proposal_deadline (
proposal : ProposalTemplate,
current = now()
) {
const { deadline, effective } = proposal
if (deadline !== undefined) {
return deadline
} else if (effective !== undefined) {
return effective - current
} else {
return CONST.DEADLINE_DEFAULT
}
}

export function get_proposal_id (
proposal : ProposalData
) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/module/contract/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
ContractPublishRequest,
PublishRequest,
ProposalData
} from '../../types/index.js'

Expand All @@ -8,6 +8,6 @@ import ContractSchema from '../../schema/contract.js'
export function create_publish_req (
proposal : ProposalData,
endorsements ?: string[]
) : ContractPublishRequest {
) : PublishRequest {
return ContractSchema.publish_req.parse({ endorsements, proposal })
}
4 changes: 2 additions & 2 deletions src/core/module/contract/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import TxSchema from '@/core/schema/tx.js'
import {
ContractCreateConfig,
ContractData,
ContractPublishRequest,
PublishRequest,
ContractStatus,
DepositData,
SignerAPI,
Expand All @@ -34,7 +34,7 @@ import {
*/
export function create_contract (
config : ContractCreateConfig,
request : ContractPublishRequest,
request : PublishRequest,
signer : SignerAPI
) : ContractData {
// Unpack config object.
Expand Down
2 changes: 1 addition & 1 deletion src/core/types/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ interface ContractIsOpen {
machine_vout : null
}

export interface ContractPublishRequest {
export interface PublishRequest {
endorsements ?: string[]
proposal : ProposalData
}
Expand Down
34 changes: 17 additions & 17 deletions src/core/types/proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ export interface ProposalTemplate extends Omit<Partial<ProposalData>, 'network'>
}

export interface ProposalData {
content ?: string
created_at : number
deadline : number
duration : number
effective ?: number
engine : string
feerate ?: number
moderator ?: string
network : ChainNetwork
paths : PathEntry[]
payments : PaymentEntry[]
programs : ProgramEntry[]
schedule : ScheduleEntry[]
title : string
txtimeout : number
value : number
version : number
content ?: string
created_at : number
deadline : number
duration : number
effective ?: number
engine : string
feerate ?: number
moderator ?: string
network : ChainNetwork
paths : PathEntry[]
payments : PaymentEntry[]
programs : ProgramEntry[]
schedule : ScheduleEntry[]
title : string
txtimeout : number
value : number
version : number
}

export interface ProposalPolicy {
Expand Down
4 changes: 2 additions & 2 deletions src/core/types/recovery.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ScriptWord } from '@scrow/tapscript'
import { ChainNetwork } from './base.js'
import { ScriptWord } from '@scrow/tapscript'
import { ChainNetwork } from './base.js'

export interface RecoveryConfig {
agent_pk : string
Expand Down
Loading

0 comments on commit 21fd07b

Please sign in to comment.