1
1
import { Buff } from '@cmdcode/buff'
2
- import { create_return_tx } from '@/lib/return.js '
2
+ import { parse_extkey } from '@cmdcode/crypto-tools/hd '
3
3
import { EscrowSigner } from '@/client/class/signer.js'
4
4
import { get_deposit_ctx } from '@/lib/deposit.js'
5
- import { verify_account } from '@/validators/deposit.js'
5
+ import { verify_account } from '@/client/ validators/deposit.js'
6
6
7
7
import {
8
8
create_covenant ,
9
- create_return
9
+ create_return_psig
10
10
} from '@/lib/session.js'
11
11
12
12
import {
@@ -16,113 +16,102 @@ import {
16
16
CovenantData ,
17
17
DepositAccount ,
18
18
DepositData ,
19
- ReturnData ,
20
19
TxOutput
21
20
} from '@/types/index.js'
22
21
23
- export function request_account_api ( client : EscrowSigner ) {
22
+ export function request_account_api ( signer : EscrowSigner ) {
24
23
return async (
25
- locktime : number
24
+ locktime : number ,
25
+ index ?: number
26
26
) : Promise < ApiResponse < AccountDataResponse > > => {
27
- const pubkey = client . pubkey
28
- return client . client . deposit . request ( { pubkey, locktime } )
27
+ const deposit_pk = signer . pubkey
28
+ const spend_xpub = signer . get_account ( index ) . xpub
29
+ const req = { deposit_pk, locktime, spend_xpub }
30
+ return signer . client . deposit . request ( req )
29
31
}
30
32
}
31
33
32
34
export function verify_account_api ( signer : EscrowSigner ) {
33
35
return ( account : DepositAccount ) : void => {
34
- const host_pub = signer . host_pub
35
- const network = signer . client . network
36
- if ( host_pub === undefined ) {
37
- throw new Error ( 'host pubkey is not set on device' )
38
- }
39
- verify_account ( account , signer . pubkey , host_pub , network )
40
- }
41
- }
42
-
43
- /**
44
- * Create a deposit template for registration.
45
- */
46
- export function register_utxo_api ( client : EscrowSigner ) {
47
- return async (
48
- account : DepositAccount ,
49
- utxo : TxOutput ,
50
- txfee ?: number
51
- ) : Promise < string > => {
52
- // Unpack the deposit object.
53
- const { agent_pk, sequence } = account
54
- // Define our pubkey.
55
- const pub = client . pubkey
56
- const idx = Buff . hex ( utxo . txid ) . slice ( 0 , 4 ) . num
57
- const addr = client . _wallet . get_account ( idx ) . new_address ( )
58
- // Get the context object for our deposit account.
59
- const ctx = get_deposit_ctx ( agent_pk , pub , sequence )
60
- // Create the return transaction.
61
- return create_return_tx ( addr , ctx , client . _signer , utxo , txfee )
36
+ verify_account ( account , signer )
62
37
}
63
38
}
64
39
65
- export function commit_utxo_api ( client : EscrowSigner ) {
40
+ export function commit_utxo_api ( signer : EscrowSigner ) {
66
41
return async (
67
42
account : DepositAccount ,
68
43
contract : ContractData ,
69
44
utxo : TxOutput
70
45
) : Promise < CovenantData > => {
71
46
// Unpack the deposit object.
72
- const { agent_pk, sequence } = account
73
- // Define our pubkey.
74
- const pub = client . pubkey
47
+ const { agent_pk, sequence, spend_xpub } = account
48
+ // Check if account xpub is valid.
49
+ if ( ! signer . has_account ( spend_xpub ) ) {
50
+ throw new Error ( 'account xpub is not recognized by master wallet' )
51
+ }
52
+ // Define our pubkey as the deposit pubkey.
53
+ const deposit_pk = signer . pubkey
54
+ // Define our xpub as the return pubkey.
55
+ const return_pk = parse_extkey ( spend_xpub ) . pubkey
75
56
// Get the context object for our deposit account.
76
- const ctx = get_deposit_ctx ( agent_pk , pub , sequence )
57
+ const ctx = get_deposit_ctx ( agent_pk , deposit_pk , return_pk , sequence )
77
58
// Create a covenant with the contract and deposit.
78
- return create_covenant ( ctx , contract , client . _signer , utxo )
59
+ return create_covenant ( ctx , contract , signer . _signer , utxo )
79
60
}
80
61
}
81
62
82
- export function commit_deposit_api ( client : EscrowSigner ) {
63
+ export function commit_deposit_api ( signer : EscrowSigner ) {
83
64
return async (
84
65
contract : ContractData ,
85
66
deposit : DepositData
86
67
) : Promise < CovenantData > => {
87
68
// Unpack the deposit object.
88
- const { agent_pk, sequence, txid, vout, value, scriptkey } = deposit
89
- // Define our pubkey.
90
- const pub = client . pubkey
69
+ const {
70
+ agent_pk, sequence, txid, vout,
71
+ value, scriptkey, spend_xpub
72
+ } = deposit
73
+ // Check if account xpub is valid.
74
+ if ( ! signer . has_account ( spend_xpub ) ) {
75
+ throw new Error ( 'account xpub is not recognized by master wallet' )
76
+ }
77
+ // Define our pubkey as the deposit pubkey.
78
+ const deposit_pk = signer . pubkey
79
+ // Define our xpub as the return pubkey.
80
+ const return_pk = parse_extkey ( spend_xpub ) . pubkey
91
81
// Get the context object for our deposit account.
92
- const ctx = get_deposit_ctx ( agent_pk , pub , sequence )
82
+ const ctx = get_deposit_ctx ( agent_pk , deposit_pk , return_pk , sequence )
93
83
// Define utxo object from deposit data.
94
84
const utxo = { txid, vout, value, scriptkey }
95
85
// Create a covenant with the contract and deposit.
96
- return create_covenant ( ctx , contract , client . _signer , utxo )
86
+ return create_covenant ( ctx , contract , signer . _signer , utxo )
97
87
}
98
88
}
99
89
100
- export function close_deposit_api ( client : EscrowSigner ) {
90
+ export function close_deposit_api ( signer : EscrowSigner ) {
101
91
return async (
102
92
deposit : DepositData ,
103
93
txfee : number ,
104
94
address ?: string
105
- ) : Promise < ReturnData > => {
106
- // Unpack client object.
95
+ ) : Promise < string > => {
96
+ // Unpack signer object.
107
97
const { txid } = deposit
108
98
if ( address === undefined ) {
109
99
// Compute an index value from the deposit txid.
110
100
const acct = Buff . hex ( txid ) . slice ( 0 , 4 ) . num
111
101
// Generate refund address.
112
- address = client . _wallet . get_account ( acct ) . new_address ( )
102
+ address = signer . get_account ( acct ) . new_address ( )
113
103
}
114
104
// Create the return transaction.
115
- return create_return ( address , deposit , client . _signer , txfee )
105
+ return create_return_psig ( deposit , signer . _signer , txfee )
116
106
}
117
107
}
118
108
119
- export default function ( client : EscrowSigner ) {
109
+ export default function ( signer : EscrowSigner ) {
120
110
return {
121
- request_account : request_account_api ( client ) ,
122
- verify_account : verify_account_api ( client ) ,
123
- register_utxo : register_utxo_api ( client ) ,
124
- commit_utxo : commit_utxo_api ( client ) ,
125
- commit_deposit : commit_deposit_api ( client ) ,
126
- close_deposit : close_deposit_api ( client )
111
+ request_account : request_account_api ( signer ) ,
112
+ verify_account : verify_account_api ( signer ) ,
113
+ close_account : close_deposit_api ( signer ) ,
114
+ commit_utxo : commit_utxo_api ( signer ) ,
115
+ commit_deposit : commit_deposit_api ( signer )
127
116
}
128
117
}
0 commit comments