Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cmd committed Jan 15, 2024
1 parent d17a637 commit 8103b28
Show file tree
Hide file tree
Showing 30 changed files with 214 additions and 200 deletions.
6 changes: 3 additions & 3 deletions src/client/api/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
ReturnData,
DepositData,
ApiResponse,
DepositAccount,
DepositSession,
DepositRequest
} from '@/types/index.js'

Expand All @@ -21,15 +21,15 @@ import * as assert from '@/assert.js'
function request_deposit_api (client : EscrowClient) {
return async (
req : DepositRequest
) : Promise<ApiResponse<DepositAccount>> => {
) : Promise<ApiResponse<DepositSession>> => {
// Ensure params are string values.
const arr = Object.entries(req)
// Build a query string with params.
const qry = new URLSearchParams(arr).toString()
// Formulate the request.
const url = `${client.host}/api/deposit/request?${qry}`
// Return the response.
return client.fetcher<DepositAccount>({ url })
return client.fetcher<DepositSession>({ url })
}
}

Expand Down
41 changes: 20 additions & 21 deletions src/client/api/depositor.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Buff } from '@cmdcode/buff'
import { create_return_tx } from '@/lib/return.js'
import { get_deposit_ctx } from '@/lib/deposit.js'
import { EscrowMember } from '@/client/class/member.js'
import { EscrowSigner } from '@/client/class/signer.js'

import {
create_spend_psigs,
create_return_psig
create_covenant,
create_return
} from '@/lib/session.js'

import {
ContractData,
CovenantData,
DepositAccount,
DepositSession,
DepositData,
DepositRegister,
ReturnData,
Expand All @@ -21,66 +21,65 @@ import {
/**
* Create a deposit template for registration.
*/
export function register_deposit_api (client : EscrowMember) {
export function register_deposit_api (client : EscrowSigner) {
return async (
acct : DepositAccount,
sess : DepositSession,
utxo : TxOutput
) : Promise<DepositRegister> => {
// Unpack the deposit object.
const { agent_id, agent_pk, sequence } = acct
const { agent_id, agent_pk, sequence } = sess
// Define our pubkey.
const pub = client.signer.pubkey
const pub = client.pubkey
const idx = Buff.hex(utxo.txid).slice(0, 4).num
const addr = client.wallet.get_account(idx).new_address()
const addr = client._wallet.get_account(idx).new_address()
// Get the context object for our deposit account.
const ctx = get_deposit_ctx(agent_pk, pub, sequence)
// Create the return transaction.
const rtx = create_return_tx(addr, ctx, client.signer, utxo)
const rtx = create_return_tx(addr, ctx, client._signer, utxo)
return { agent_id, return_tx : rtx }
}
}

export function commit_deposit_api (client : EscrowMember) {
export function create_covenant_api (client : EscrowSigner) {
return async (
req : DepositAccount | DepositData,
req : DepositSession | DepositData,
contract : ContractData,
utxo : TxOutput
) : Promise<CovenantData> => {
// Unpack the deposit object.
const { agent_pk, sequence } = req
// Define our pubkey.
const pub = client.signer.pubkey
const pub = client.pubkey
// Get the context object for our deposit account.
const ctx = get_deposit_ctx(agent_pk, pub, sequence)
// Create a covenant with the contract and deposit.
return create_spend_psigs(ctx, contract, client.signer, utxo)
return create_covenant(ctx, contract, client._signer, utxo)
}
}

export function close_deposit_api (client : EscrowMember) {
export function create_return_api (client : EscrowSigner) {
return async (
deposit : DepositData,
txfee : number,
address ?: string
) : Promise<ReturnData> => {
// Unpack client object.
const { signer, wallet } = client
const { txid } = deposit
if (address === undefined) {
// Compute an index value from the deposit txid.
const acct = Buff.hex(txid).slice(0, 4).num
// Generate refund address.
address = wallet.get_account(acct).new_address()
address = client._wallet.get_account(acct).new_address()
}
// Create the return transaction.
return create_return_psig(address, deposit, signer, txfee)
return create_return(address, deposit, client._signer, txfee)
}
}

export default function (client : EscrowMember) {
export default function (client : EscrowSigner) {
return {
create_registration : register_deposit_api(client),
create_covenant : commit_deposit_api(client),
create_refund : close_deposit_api(client)
create_covenant : create_covenant_api(client),
create_return : create_return_api(client)
}
}
23 changes: 11 additions & 12 deletions src/client/api/endorse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { get_proposal_id } from '@/lib/proposal.js'
import { endorse_proposal } from '@/lib/proposal.js'
import { sign_witness } from '@/lib/witness.js'
import { EscrowContract } from '@/client/class/contract.js'
import { EscrowMember } from '@/client/class/member.js'
import { EscrowSigner } from '@/client/class/signer.js'
import { EscrowProposal } from '@/client/class/proposal.js'
import { validate_witness } from '@/validators/program.js'

Expand All @@ -16,47 +16,46 @@ import {
WitnessData
} from '@/types/index.js'

export function endorse_proposal_api (member : EscrowMember) {
export function endorse_proposal_api (client : EscrowSigner) {
return (proposal : ProposalData | EscrowProposal) => {
if (proposal instanceof EscrowProposal) {
proposal = proposal.data
}
validate_proposal(proposal)
verify_proposal(proposal)
const hash = get_proposal_id(proposal)
return member.signer.sign(hash)
return endorse_proposal(proposal, client._signer)
}
}
export function endorse_request_api (member : EscrowMember) {

export function endorse_request_api (client : EscrowSigner) {
return (
url : string,
body : string = '{}',
method : string = 'GET'
) => {
const content = method + url + body
return member.signer.gen_token(content)
return client._signer.gen_token(content)
}
}

export function endorse_witness_api (member : EscrowMember) {
export function endorse_witness_api (client : EscrowSigner) {
return (
contract : ContractData | EscrowContract,
witness : WitnessData
) => {
if (contract instanceof EscrowContract) {
contract = contract.data
}
const cred = member.get_membership(contract.terms)
const cred = client.get_membership(contract.terms)
validate_witness(contract, witness)
return sign_witness(cred.signer, witness)
}
}

export default function (client : EscrowMember) {
export default function (client : EscrowSigner) {
return {
proposal : endorse_proposal_api(client),
request : endorse_request_api(client),
witness : endorse_witness_api(client)
}
}
}
24 changes: 11 additions & 13 deletions src/client/api/member.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Wallet } from '@cmdcode/signer'
import { EscrowMember } from '@/client/class/member.js'
import { EscrowSigner } from '@/client/class/signer.js'
import { EscrowProposal } from '@/client/class/proposal.js'
import { ProposalData } from '@/types/index.js'

Expand All @@ -11,47 +11,45 @@ import {

import { Membership } from '../types.js'

export function gen_membership_api (client : EscrowMember) {
export function gen_membership_api (client : EscrowSigner) {
return (index ?: number) => {
const { signer, wallet } = client
const idx = index ?? client.new_idx
return gen_membership(idx, signer, wallet)
const idx = index ?? client._gen_idx()
return gen_membership(idx, client._signer, client._wallet)
}
}

export function has_membership_api (
client : EscrowMember
client : EscrowSigner
) {
return (proposal : ProposalData | EscrowProposal) => {
if (proposal instanceof EscrowProposal) {
proposal = proposal.data
}
return has_membership(proposal.members, client.signer)
return has_membership(proposal.members, client._signer)
}
}

export function claim_membership_api (
client : EscrowMember
client : EscrowSigner
) {
const { signer } = client
return (proposal : ProposalData | EscrowProposal) : Membership => {
if (proposal instanceof EscrowProposal) {
proposal = proposal.data
}
if (!has_membership(proposal.members, signer)) {
if (!has_membership(proposal.members, client._signer)) {
throw new Error('not a member of the proposal')
}
const mship = get_membership(proposal.members, signer)
const mship = get_membership(proposal.members, client._signer)
// TODO: validate membership
return {
signer : client.signer.get_id(mship.id),
signer : client._signer.get_id(mship.id),
token : mship,
wallet : new Wallet(mship.xpub)
}
}
}

export default function (client : EscrowMember) {
export default function (client : EscrowSigner) {
return {
claim : claim_membership_api(client),
create : gen_membership_api(client),
Expand Down
21 changes: 15 additions & 6 deletions src/client/class/proposal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { EventEmitter } from './emitter.js'
import { EscrowMember } from './member.js'
import { find_program } from '@/lib/proposal.js'
import { EscrowSigner } from './signer.js'

import {
find_program,
get_proposal_id
} from '@/lib/proposal.js'

import {
MemberData,
Expand All @@ -13,6 +17,7 @@ import {
add_membership,
rem_membership
} from '../../lib/policy.js'
import { parse_proposal } from '@/lib/parse.js'

export class EscrowProposal extends EventEmitter<{
'update' : EscrowProposal
Expand All @@ -22,7 +27,7 @@ export class EscrowProposal extends EventEmitter<{

constructor (data : ProposalData) {
super()
this._data = data
this._data = parse_proposal(data)
}

get copy () {
Expand All @@ -33,21 +38,25 @@ export class EscrowProposal extends EventEmitter<{
return this._data
}

get id () {
return get_proposal_id(this.data).hex
}

_update (data : ProposalData) {
this._data = data
this._data = parse_proposal(data)
this.emit('update', this)
}

join (
role : RolePolicy,
member : EscrowMember,
member : EscrowSigner,
index ?: number
) {
const mdata = member.gen_membership(index)
this.add_membership(mdata, role)
}

leave (member : EscrowMember) {
leave (member : EscrowSigner) {
const mship = member.get_membership(this)
this.rem_membership(mship.token)
}
Expand Down
14 changes: 1 addition & 13 deletions src/client/class/member.ts → src/client/class/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {

const DEFAULT_IDXGEN = () => Buff.now(4).num

export class EscrowMember {
export class EscrowSigner {

readonly _client : EscrowClient
readonly _gen_idx : () => number
Expand All @@ -40,18 +40,6 @@ export class EscrowMember {
return this._signer.pubkey
}

get new_idx () {
return this._gen_idx()
}

get signer () {
return this._signer
}

get wallet () {
return this._wallet
}

deposit = deposit_api(this)
endorse = endorse_api(this)

Expand Down
2 changes: 1 addition & 1 deletion src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export * from './class/client.js'
export * from './class/contract.js'
export * from './class/deposit.js'
export * from './class/proposal.js'
export * from './class/member.js'
export * from './class/signer.js'
export * from './types.js'
Loading

0 comments on commit 8103b28

Please sign in to comment.