@@ -23,7 +23,6 @@ public class LinkPageViewModel : ViewModelBase
23
23
public LinkPage View ;
24
24
private CancellationTokenSource CancelSource ;
25
25
private bool UIEnabled = true ;
26
- private Task LinkingTask ;
27
26
28
27
public string DeviceName { get ; set ; } = "Signal on Windows" ;
29
28
@@ -71,63 +70,62 @@ public async Task BeginLinking()
71
70
{
72
71
try
73
72
{
73
+ Debug . WriteLine ( SynchronizationContext . Current ) ;
74
74
CancelSource = new CancellationTokenSource ( ) ;
75
- string deviceName = DeviceName ;
76
- LinkingTask = Task . Run ( ( ) =>
75
+ // clean the database from stale values
76
+ await Task . Run ( ( ) =>
77
77
{
78
- /* clean the database from stale values */
79
78
LibsignalDBContext . PurgeAccountData ( ) ;
79
+ } ) ;
80
80
81
- /* prepare qrcode */
82
- string password = Base64 . EncodeBytes ( Util . getSecretBytes ( 18 ) ) ;
83
- IdentityKeyPair tmpIdentity = KeyHelper . generateIdentityKeyPair ( ) ;
84
- SignalServiceAccountManager accountManager = new SignalServiceAccountManager ( App . ServiceConfiguration , CancelSource . Token , "Signal-Windows" ) ;
85
- string uuid = accountManager . GetNewDeviceUuid ( CancelSource . Token ) ;
86
- string tsdevice = "tsdevice:/?uuid=" + Uri . EscapeDataString ( uuid ) + "&pub_key=" + Uri . EscapeDataString ( Base64 . encodeBytesWithoutPadding ( tmpIdentity . getPublicKey ( ) . serialize ( ) ) ) ;
87
- Windows . ApplicationModel . Core . CoreApplication . MainView . CoreWindow . Dispatcher . RunAsync ( Windows . UI . Core . CoreDispatcherPriority . Normal , ( ) =>
88
- {
89
- View . SetQR ( tsdevice ) ;
90
- QRVisible = Visibility . Visible ;
91
- QRCodeString = tsdevice ;
92
- } ) . AsTask ( ) . Wait ( ) ;
93
-
94
- string tmpSignalingKey = Base64 . EncodeBytes ( Util . getSecretBytes ( 52 ) ) ;
95
- int registrationId = ( int ) KeyHelper . generateRegistrationId ( false ) ;
96
-
97
- NewDeviceLinkResult result = accountManager . FinishNewDeviceRegistration ( tmpIdentity , tmpSignalingKey , password , false , true , registrationId , deviceName ) ;
98
- SignalStore store = new SignalStore ( )
99
- {
100
- DeviceId = ( uint ) result . DeviceId ,
101
- IdentityKeyPair = Base64 . EncodeBytes ( result . Identity . serialize ( ) ) ,
102
- NextSignedPreKeyId = 1 ,
103
- Password = password ,
104
- PreKeyIdOffset = 1 ,
105
- Registered = true ,
106
- RegistrationId = ( uint ) registrationId ,
107
- SignalingKey = tmpSignalingKey ,
108
- Username = result . Number
109
- } ;
110
- LibsignalDBContext . SaveOrUpdateSignalStore ( store ) ;
81
+ ( string password , IdentityKeyPair tmpIdentity ) = await Task . Run ( ( ) =>
82
+ {
83
+ string newPassword = Base64 . EncodeBytes ( Util . getSecretBytes ( 18 ) ) ;
84
+ IdentityKeyPair newTmpIdentity = KeyHelper . generateIdentityKeyPair ( ) ;
85
+ return ( newPassword , newTmpIdentity ) ;
86
+ } ) ;
87
+
88
+ // fetch new device uuid
89
+ SignalServiceAccountManager accountManager = new SignalServiceAccountManager ( App . ServiceConfiguration , CancelSource . Token , "Signal-Windows" ) ;
90
+ string uuid = await accountManager . GetNewDeviceUuid ( CancelSource . Token ) ;
91
+ string tsdevice = "tsdevice:/?uuid=" + Uri . EscapeDataString ( uuid ) + "&pub_key=" + Uri . EscapeDataString ( Base64 . encodeBytesWithoutPadding ( tmpIdentity . getPublicKey ( ) . serialize ( ) ) ) ;
92
+
93
+ View . SetQR ( tsdevice ) ; //TODO generate qrcode in worker task
94
+ QRVisible = Visibility . Visible ;
95
+ QRCodeString = tsdevice ;
96
+
97
+ string tmpSignalingKey = Base64 . EncodeBytes ( Util . getSecretBytes ( 52 ) ) ;
98
+ int registrationId = ( int ) KeyHelper . generateRegistrationId ( false ) ;
111
99
112
- /* reload registered state */
113
- Windows . ApplicationModel . Core . CoreApplication . MainView . CoreWindow . Dispatcher . RunAsync ( Windows . UI . Core . CoreDispatcherPriority . Normal , ( ) =>
114
- {
115
- UIEnabled = false ;
116
- App . Handle . Store = store ;
117
- } ) . AsTask ( ) . Wait ( ) ;
118
-
119
- /* create prekeys */
120
- LibsignalDBContext . RefreshPreKeys ( new SignalServiceAccountManager ( App . ServiceConfiguration , store . Username , store . Password , ( int ) store . DeviceId , App . USER_AGENT ) ) ;
121
-
122
- /* reload again with prekeys and their offsets */
123
- store = LibsignalDBContext . GetSignalStore ( ) ;
124
- Windows . ApplicationModel . Core . CoreApplication . MainView . CoreWindow . Dispatcher . RunAsync ( Windows . UI . Core . CoreDispatcherPriority . Normal , async ( ) =>
125
- {
126
- App . Handle . Store = store ;
127
- await View . Finish ( true ) ;
128
- } ) . AsTask ( ) . Wait ( ) ;
100
+ var provisionMessage = await accountManager . GetProvisioningMessage ( CancelSource . Token , tmpIdentity ) ;
101
+ int deviceId = await accountManager . FinishNewDeviceRegistration ( CancelSource . Token , provisionMessage , tmpSignalingKey , password , false , true , registrationId , DeviceName ) ;
102
+ SignalStore store = new SignalStore ( )
103
+ {
104
+ DeviceId = ( uint ) deviceId ,
105
+ IdentityKeyPair = Base64 . EncodeBytes ( provisionMessage . Identity . serialize ( ) ) ,
106
+ NextSignedPreKeyId = 1 ,
107
+ Password = password ,
108
+ PreKeyIdOffset = 1 ,
109
+ Registered = true ,
110
+ RegistrationId = ( uint ) registrationId ,
111
+ SignalingKey = tmpSignalingKey ,
112
+ Username = provisionMessage . Number
113
+ } ;
114
+ await Task . Run ( ( ) =>
115
+ {
116
+ LibsignalDBContext . SaveOrUpdateSignalStore ( store ) ;
129
117
} ) ;
130
- await LinkingTask ;
118
+
119
+ // reload registered state
120
+ UIEnabled = false ;
121
+ App . Handle . Store = store ;
122
+
123
+ // create prekeys
124
+ LibsignalDBContext . RefreshPreKeys ( new SignalServiceAccountManager ( App . ServiceConfiguration , store . Username , store . Password , ( int ) store . DeviceId , App . USER_AGENT ) ) ;
125
+
126
+ // reload again with prekeys and their offsets
127
+ App . Handle . Store = LibsignalDBContext . GetSignalStore ( ) ;
128
+ await View . Finish ( true ) ;
131
129
}
132
130
catch ( Exception e )
133
131
{
@@ -141,18 +139,6 @@ internal async void BackButton_Click(object sender, BackRequestedEventArgs e)
141
139
if ( UIEnabled )
142
140
{
143
141
CancelSource . Cancel ( ) ;
144
- if ( LinkingTask != null )
145
- {
146
- try
147
- {
148
- await LinkingTask ;
149
- }
150
- catch ( Exception ex )
151
- {
152
- var line = new StackTrace ( ex , true ) . GetFrames ( ) [ 0 ] . GetFileLineNumber ( ) ;
153
- Logger . LogError ( "BackButton_Click() failed in line {0}: {1}\n {2}" , line , ex . Message , ex . StackTrace ) ;
154
- }
155
- }
156
142
await Task . Run ( ( ) =>
157
143
{
158
144
App . Handle . PurgeAccountData ( ) ;
0 commit comments