Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cmd committed Sep 26, 2023
1 parent 2300cef commit 440e32c
Show file tree
Hide file tree
Showing 35 changed files with 561 additions and 645 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Integration Tests

on:
push:
branches: [ master ] # This assumes your primary branch is named "master".
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [19.x]
os: [ ubuntu-latest, macos-latest, windows-latest ]

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Install yarn
run: npm install -g yarn

- name: Install dependencies
run: yarn install

- name: Run test script
run: yarn test
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
test/bin
test/data
test/bitcoin.conf
coredata
dist
node_modules
TODO.md
NOTES.md
*.log
.env*
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@
"types": "tsc"
},
"devDependencies": {
"@cmdcode/core-cmd": "1.0.4",
"@cmdcode/core-cmd": "1.1.7",
"@rollup/plugin-node-resolve": "^15.2.1",
"@rollup/plugin-typescript": "^11.1.3",
"@types/node": "^20.6.3",
"@rollup/plugin-typescript": "^11.1.4",
"@types/node": "^20.7.0",
"@types/tape": "^5.6.1",
"rollup": "^3.29.2",
"tape": "^5.6.6",
"rollup": "^3.29.3",
"tape": "^5.7.0",
"tslib": "^2.6.2",
"tsx": "^3.12.10",
"tsx": "^3.13.0",
"typescript": "^5.2.2"
},
"dependencies": {
"@cmdcode/buff": "2.0.3",
"@cmdcode/crypto-tools": "2.5.0",
"@cmdcode/musig2": "2.3.0",
"@cmdcode/signer": "^1.1.17",
"@scrow/tapscript": "2.0.16",
"@cmdcode/signer": "^1.1.18",
"@scrow/tapscript": "2.0.17",
"zod": "^3.22.2"
}
}
2 changes: 1 addition & 1 deletion rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default {
file: 'dist/module.mjs',
format: 'es',
sourcemap: true,
minifyInternalExports: false
minifyInternalExports: true
}
],
plugins: [ typescript(), nodeResolve() ],
Expand Down
5 changes: 1 addition & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import config from './config.js'

export * as assert from './lib/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 { config }

export * from './types/index.js'
13 changes: 1 addition & 12 deletions src/lib/assert.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
Network,
ProposalData
} from '../types/index.js'

import * as schema from '../schema/index.js'
import { Network } from '../types/index.js'

export function ok (
value : unknown,
Expand Down Expand Up @@ -35,9 +30,3 @@ export function valid_address (
throw new Error(`Address does not match "${network}" network: ${address}`)
}
}

export function valid_proposal (
proposal : ProposalData
) : asserts proposal is ProposalData {
void schema.proposal.data.parse(proposal)
}
38 changes: 19 additions & 19 deletions src/lib/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import {
ProposalData,
SessionContext,
SessionEntry
} from '@/types/index.js'
} from '../types/index.js'

export function get_deposit_ctx (
agent : AgentData,
proposal : ProposalData,
deposit_pub : Bytes
) : DepositContext {
const members = [ deposit_pub, agent.signing_pub ]
const members = [ deposit_pub, agent.pubkey ]
const prop_id = Buff.json(proposal).digest.hex
const locktime = get_deposit_locktime(agent, proposal)
const script = get_refund_script(deposit_pub, locktime)
Expand All @@ -58,13 +58,13 @@ export function get_deposit_ctx (
}

export function get_session_ctx (
deposit_ctx : DepositContext,
session_pubs : Bytes[],
sighash : Bytes
context : DepositContext,
pnonces : Bytes[],
sighash : Bytes
) : SessionContext {
const { group_pub, prop_id, key_data, tap_data } = deposit_ctx
const nonce_twk = get_nonce_tweak(deposit_ctx, session_pubs, sighash)
const pubnonces = tweak_nonce_pubs(session_pubs, nonce_twk)
const { group_pub, prop_id, key_data, tap_data } = context
const nonce_twk = get_nonce_tweak(context, pnonces, sighash)
const pubnonces = get_tweaked_pnonces(pnonces, nonce_twk)
const nonce_ctx = get_nonce_ctx(pubnonces, group_pub, sighash)
const musig_opt = { key_tweaks : [ tap_data.taptweak ] }
const musig_ctx = create_ctx(key_data, nonce_ctx, musig_opt)
Expand All @@ -91,34 +91,34 @@ export function get_full_ctx (
function get_deposit_locktime (
agent : AgentData,
proposal : ProposalData
) {
) : number {
const created = agent.created_at
const expires = proposal.schedule.expires
return created + expires + GRACE_PERIOD
}

export function get_nonce_tweak (
deposit_ctx : DepositContext,
session_pubs : Bytes[],
sighash : Bytes
context : DepositContext,
pnonces : Bytes[],
sighash : Bytes
) : Buff {
const { group_pub } = deposit_ctx
const { group_pub } = context
return hash340 (
'musig/nonce_tweak',
group_pub,
sighash,
...sort_bytes(session_pubs)
...sort_bytes(pnonces)
)
}

export function tweak_nonce_pubs (
session_pubs : Bytes[],
session_tweak : Bytes
export function get_tweaked_pnonces (
pnonces : Bytes[],
tweak : Bytes
) : Buff[] {
return session_pubs.map(e => {
return pnonces.map(e => {
const pnonces = Buff
.parse(e, 32, 64)
.map(k => tweak_pubkey(k, [ session_tweak ], true))
.map(k => tweak_pubkey(k, [ tweak ], true))
return Buff.join(pnonces)
})
}
38 changes: 16 additions & 22 deletions src/lib/deposit.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Bytes } from '@cmdcode/buff'
import { Signer } from '@cmdcode/signer'
import { P2TR } from '@scrow/tapscript/address'

import { Buff, Bytes } from '@cmdcode/buff'
import { Signer } from '@cmdcode/signer'
import { P2TR } from '@scrow/tapscript/address'
import { parse_prevout } from './tx.js'

import {
Expand All @@ -22,28 +21,23 @@ import {
ProposalData,
SessionContext,
SessionEntry
} from '@/types/index.js'
} from '../types/index.js'

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

export function get_deposit_address (
agent : AgentData,
proposal : ProposalData,
deposit_pub : Bytes,
network ?: Network
context : DepositContext,
network ?: Network
) {
const { tap_data } = get_deposit_ctx(agent, proposal, deposit_pub)
const { tap_data } = context
return P2TR.encode(tap_data.tapkey, network)
}

export function get_deposit_nonce (
context : DepositContext,
signer : Signer
) {
const { group_pub, prop_id } = context
console.log('group pub:', group_pub)
console.log('prop_id:', prop_id)
return signer.gen_session_nonce(group_pub, prop_id)
return signer.gen_session_nonce(context.prop_id)
}

export function create_deposit (
Expand All @@ -56,15 +50,15 @@ export function create_deposit (
const group_pub = context.group_pub
const txinput = parse_prevout(group_pub, txdata)
assert.ok(txinput !== null)
const session_pub = get_deposit_nonce(context, signer)
const session_pubs = [ session_pub, agent.session_pub ]
const sessions_ctx = get_full_ctx(context, session_pubs, txinput)
const session_psigs = create_deposit_psigs(sessions_ctx, signer)
const session_pnonce = get_deposit_nonce(context, signer)
const session_pnonces = [ session_pnonce, agent.pnonce ]
const sessions_ctx = get_full_ctx(context, session_pnonces, txinput)
const session_psigs = create_deposit_psigs(sessions_ctx, signer)
return {
session_pub,
txinput,
deposit_pub : signer.pubkey.hex,
psigs : session_psigs
pubkey : signer.pubkey.hex,
pnonce : session_pnonce.hex,
psigs : session_psigs,
txvin : Buff.json(txinput).to_bech32m('txvin')
}
}

Expand Down
47 changes: 15 additions & 32 deletions src/lib/proposal.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { parse_addr } from '@scrow/tapscript/address'
import { create_vout } from '@scrow/tapscript/tx'
import { Signer } from '@cmdcode/signer'
import { TxOutput } from '@scrow/tapscript'

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

import {
Fee,
Payout,
Payment,
PayPath,
PathTemplate,
ProposalData,
AgentData
Expand All @@ -21,38 +20,22 @@ export function parse_proposal (
return schema.data.parse(proposal)
}

export function endorse_proposal (
signer : Signer,
proposal : ProposalData
) {
const prop = parse_proposal(proposal)
return signer.sign_note(prop)
}

export function verify_endorsement (
proof : string,
proposal : ProposalData
) {
const prop = parse_proposal(proposal)
return Signer.proof.verify(proof, prop)
}

export function filter_paths (
label : string,
paths : Payout[]
paths : PayPath[]
) {
return paths.filter(e => e[0] === label)
}

export function get_path_names (paths : Payout[]) {
export function get_path_names (paths : PayPath[]) {
return [ ...new Set(paths.map(e => e[0])) ]
}

export function get_fee_totals (fees : Fee[]) : number {
return fees.map(e => e[0]).reduce((acc, curr) => acc + curr, 0)
export function get_pay_totals (payments : Payment[]) : number {
return payments.map(e => e[0]).reduce((acc, curr) => acc + curr, 0)
}

export function get_addresses (paths : Payout[]) {
export function get_addresses (paths : PayPath[]) {
return [ ...new Set(paths.map(e => e[2])) ]
}

Expand All @@ -71,8 +54,8 @@ export function get_path_templates (
agent : AgentData,
proposal : ProposalData
) : PathTemplate[] {
const { fees, paths } = proposal
const total_fees = [ ...fees, ...agent.fees ]
const { payments, paths } = proposal
const total_fees = [ ...payments, ...agent.payments ]
const path_names = get_path_names(paths)
const templates : PathTemplate[] = []
for (const name of path_names) {
Expand All @@ -83,20 +66,20 @@ export function get_path_templates (
}

export function get_path_vouts (
label : string,
paths : Payout[] = [],
fees : Fee[] = []
label : string,
paths : PayPath[] = [],
payouts : Payment[] = []
) : TxOutput[] {
const filtered : Fee[] = filter_paths(label, paths).map(e => [ e[1], e[2] ])
const template : Fee[] = [ ...filtered.sort(), ...fees.sort() ]
const filtered : Payment[] = filter_paths(label, paths).map(e => [ e[1], e[2] ])
const template : Payment[] = [ ...filtered.sort(), ...payouts.sort() ]
return template.map(([ value, addr ]) => {
const scriptPubKey = parse_addr(addr).script
return create_vout({ value, scriptPubKey })
})
}

export function get_path_totals (
paths : Payout[]
paths : PayPath[]
) : PathTotal[] {
// Setup an array for out totals.
const path_totals : PathTotal[] = []
Expand Down
2 changes: 1 addition & 1 deletion src/lib/refund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Signer } from '@cmdcode/signer'
import { TxData } from '@scrow/tapscript'
import { parse_prevout } from './tx.js'
import { parse_addr } from '@scrow/tapscript/address'
import { DepositContext } from '@/types/index.js'
import { DepositContext } from '../types/index.js'

import {
create_tx,
Expand Down
Loading

0 comments on commit 440e32c

Please sign in to comment.