Skip to content

Commit a8110a8

Browse files
committed
Update libsignal, refactor LinkPageViewModel
fixes #112
1 parent c92c582 commit a8110a8

File tree

2 files changed

+51
-65
lines changed

2 files changed

+51
-65
lines changed

Signal-Windows.Lib/Signal-Windows.Lib.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
</ItemGroup>
162162
<ItemGroup>
163163
<PackageReference Include="libsignal-service-dotnet">
164-
<Version>2.7.5</Version>
164+
<Version>2.7.5.1</Version>
165165
</PackageReference>
166166
<PackageReference Include="Microsoft.EntityFrameworkCore">
167167
<Version>1.1.4</Version>

Signal-Windows/ViewModels/LinkPageViewModel.cs

Lines changed: 50 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public class LinkPageViewModel : ViewModelBase
2323
public LinkPage View;
2424
private CancellationTokenSource CancelSource;
2525
private bool UIEnabled = true;
26-
private Task LinkingTask;
2726

2827
public string DeviceName { get; set; } = "Signal on Windows";
2928

@@ -71,63 +70,62 @@ public async Task BeginLinking()
7170
{
7271
try
7372
{
73+
Debug.WriteLine(SynchronizationContext.Current);
7474
CancelSource = new CancellationTokenSource();
75-
string deviceName = DeviceName;
76-
LinkingTask = Task.Run(() =>
75+
// clean the database from stale values
76+
await Task.Run(() =>
7777
{
78-
/* clean the database from stale values */
7978
LibsignalDBContext.PurgeAccountData();
79+
});
8080

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);
11199

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);
129117
});
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);
131129
}
132130
catch (Exception e)
133131
{
@@ -141,18 +139,6 @@ internal async void BackButton_Click(object sender, BackRequestedEventArgs e)
141139
if (UIEnabled)
142140
{
143141
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-
}
156142
await Task.Run(() =>
157143
{
158144
App.Handle.PurgeAccountData();

0 commit comments

Comments
 (0)