1
1
import { TariPermissions } from "./tari_permissions" ;
2
2
import { TariConnection } from "./webrtc" ;
3
3
import { TariProvider } from '../index' ;
4
- import { TransactionSubmitRequest , TransactionResult , TransactionStatus , TransactionSubmitResponse } from '../types' ;
4
+ import { SubmitTransactionRequest , TransactionResult , TransactionStatus , SubmitTransactionResponse } from '../types' ;
5
5
import { Account } from "../types" ;
6
+ import {
7
+ WalletDaemonClient ,
8
+ stringToSubstateId ,
9
+ Instruction ,
10
+ TransactionSubmitRequest , SubstateType , SubstatesListRequest
11
+ } from "@tariproject/wallet_jrpc_client" ;
12
+ import { WebRtcRpcTransport } from "./webrtc_transport" ;
6
13
7
14
export const WalletDaemonNotConnected = 'WALLET_DAEMON_NOT_CONNECTED' ;
8
15
export const Unsupported = 'UNSUPPORTED' ;
@@ -19,11 +26,11 @@ export type WalletDaemonParameters = {
19
26
export class WalletDaemonTariProvider implements TariProvider {
20
27
public providerName = "WalletDaemon" ;
21
28
params : WalletDaemonParameters ;
22
- connection : TariConnection ;
29
+ client : WalletDaemonClient ;
23
30
24
- private constructor ( params : WalletDaemonParameters , connection : TariConnection ) {
31
+ private constructor ( params : WalletDaemonParameters , connection : WalletDaemonClient ) {
25
32
this . params = params ;
26
- this . connection = connection ;
33
+ this . client = connection ;
27
34
}
28
35
29
36
static async build ( params : WalletDaemonParameters ) : Promise < WalletDaemonTariProvider > {
@@ -32,49 +39,54 @@ export class WalletDaemonTariProvider implements TariProvider {
32
39
allPermissions . addPermissions ( params . optionalPermissions ) ;
33
40
console . log ( { allPermissions} ) ;
34
41
let connection = new TariConnection ( params . signalingServerUrl , params . webRtcConfig ) ;
35
- await connection . init ( allPermissions , params . onConnection ) ;
36
- return new WalletDaemonTariProvider ( params , connection ) ;
42
+ const client = WalletDaemonClient . new ( WebRtcRpcTransport . new ( connection ) ) ;
43
+ await connection . init ( allPermissions , ( conn ) => {
44
+ params . onConnection ?.( ) ;
45
+ if ( conn . token ) {
46
+ client . setToken ( conn . token ) ;
47
+ }
48
+ } ) ;
49
+ return new WalletDaemonTariProvider ( params , client ) ;
37
50
}
38
51
39
52
public get token ( ) : string | undefined {
40
- return this . connection . token ;
53
+ return ( this . client . getTransport ( ) as WebRtcRpcTransport ) . token ( ) ;
41
54
}
42
55
43
56
public get tokenUrl ( ) : string | undefined {
44
- if ( this . connection . token ) {
45
- const name = this . params . name && encodeURIComponent ( this . params . name ) || '' ;
46
- const token = this . connection . token ;
47
- const permissions = JSON . stringify ( this . params . permissions ) ;
48
- const optionalPermissions = JSON . stringify ( this . params . optionalPermissions ) ;
49
-
50
- return `tari://${ name } /${ token } /${ permissions } /${ optionalPermissions } `
57
+ if ( ! this . token ) {
58
+ return undefined ;
51
59
}
52
- return undefined ;
60
+
61
+ const name = this . params . name && encodeURIComponent ( this . params . name ) || '' ;
62
+ const token = this . token ;
63
+ const permissions = JSON . stringify ( this . params . permissions ) ;
64
+ const optionalPermissions = JSON . stringify ( this . params . optionalPermissions ) ;
65
+
66
+ return `tari://${ name } /${ token } /${ permissions } /${ optionalPermissions } `
53
67
}
54
68
55
69
public isConnected ( ) : boolean {
56
- return this . connection . isConnected ( ) ;
70
+ return ( this . client . getTransport ( ) as WebRtcRpcTransport ) . isConnected ( ) ;
57
71
}
58
72
59
73
public async createFreeTestCoins ( ) : Promise < Account > {
60
- const method = "accounts.create_free_test_coins" ;
61
- const res = await this . connection . sendMessage ( method , this . connection . token , {
62
- account : null ,
74
+ const res = await this . client . createFreeTestCoins ( {
75
+ account : { Name : "template_web" } ,
63
76
amount : 1000000 ,
64
77
max_fee : null ,
65
78
key_id : 0
66
- } ) as any ;
79
+ } ) ;
67
80
return {
68
81
account_id : res . account . key_index ,
69
- address : res . account . address . Component ,
82
+ address : ( res . account . address as { Component : string } ) . Component ,
70
83
public_key : res . public_key ,
71
84
resources : [ ]
72
85
} ;
73
86
}
74
87
75
88
public async getAccount ( ) : Promise < Account > {
76
- const method = "accounts.get_default" ;
77
- const { account, public_key} = await this . connection . sendMessage ( method , this . connection . token , { } ) as any ;
89
+ const { account, public_key} = await this . client . accountsGetDefault ( { } ) as any ;
78
90
79
91
return {
80
92
account_id : account . key_index ,
@@ -86,38 +98,37 @@ export class WalletDaemonTariProvider implements TariProvider {
86
98
}
87
99
88
100
public async getAccountBalances ( componentAddress : string ) : Promise < unknown > {
89
- const method = "accounts.get_balances" ;
90
- const args = { ComponentAddress : componentAddress } ;
91
- const res = await this . connection . sendMessage ( method , this . connection . token , args ) ;
92
-
93
- return res ;
101
+ return await this . client . accountsGetBalances ( { account : { ComponentAddress : componentAddress } , refresh : true } ) ;
94
102
}
95
103
96
- public async getSubstate ( _substate_address : string ) : Promise < unknown > {
97
- // TODO: the wallet daemon should expose a JRPC method to retrieve any substate
98
- throw Unsupported ;
104
+ public async getSubstate ( substate_id : string ) : Promise < unknown > {
105
+ const substateId = stringToSubstateId ( substate_id ) ;
106
+ return await this . client . substatesGet ( { substate_id : substateId } ) ;
99
107
}
100
108
101
- public async submitTransaction ( req : TransactionSubmitRequest ) : Promise < TransactionSubmitResponse > {
109
+ public async submitTransaction ( req : SubmitTransactionRequest ) : Promise < SubmitTransactionResponse > {
102
110
const params = {
103
111
signing_key_index : req . account_id ,
104
- fee_instructions : req . fee_instructions ,
105
- instructions : req . instructions ,
106
- inputs : req . required_substates ,
112
+ fee_instructions : req . fee_instructions as Instruction [ ] ,
113
+ instructions : req . instructions as Instruction [ ] ,
114
+ inputs : req . required_substates . map ( ( s ) => ( {
115
+ // TODO: Hmm The bindings want a SubstateId object, but the wallet only wants a string. Any is used to skip type checking here
116
+ substate_id : s . substate_id as any ,
117
+ version : s . version
118
+ } ) ) ,
107
119
override_inputs : false ,
108
120
is_dry_run : req . is_dry_run ,
109
121
proof_ids : [ ] ,
110
122
min_epoch : null ,
111
123
max_epoch : null ,
112
- } ;
113
- const res = await this . connection . sendMessage < any > ( "transactions.submit" , this . connection . token , params , 10 ) ;
124
+ } as TransactionSubmitRequest ;
125
+ const res = await this . client . submitTransaction ( params ) ;
114
126
115
127
return { transaction_id : res . transaction_id } ;
116
128
}
117
129
118
130
public async getTransactionResult ( transactionId : string ) : Promise < TransactionResult > {
119
- const params = { transaction_id : transactionId } ;
120
- const res = await this . connection . sendMessage ( "transactions.get_result" , this . connection . token , params ) as any ;
131
+ const res = await this . client . getTransactionResult ( { transaction_id : transactionId } ) ;
121
132
122
133
return {
123
134
transaction_id : transactionId ,
@@ -127,8 +138,18 @@ export class WalletDaemonTariProvider implements TariProvider {
127
138
}
128
139
129
140
public async getTemplateDefinition ( template_address : string ) : Promise < unknown > {
130
- const params = { template_address} ;
131
- return await this . connection . sendMessage ( "templates.get" , this . connection . token , params ) ;
141
+ return await this . client . templatesGet ( { template_address} ) ;
142
+ }
143
+
144
+ public async listSubstates (
145
+ template : string | null ,
146
+ substateType : SubstateType | null
147
+ ) {
148
+ const resp = await this . client . substatesList ( {
149
+ filter_by_template : template ,
150
+ filter_by_type : substateType
151
+ } as SubstatesListRequest ) ;
152
+ return resp . substates as any [ ] ;
132
153
}
133
154
}
134
155
0 commit comments