예제 #1
0
        public async Task BeginLinking()
        {
            try
            {
                CancelSource = new CancellationTokenSource();
                string deviceName = DeviceName;
                LinkingTask = Task.Run(() =>
                {
                    /* clean the database from stale values */
                    LibsignalDBContext.PurgeAccountData();

                    /* prepare qrcode */
                    string password             = Base64.encodeBytes(Util.getSecretBytes(18));
                    IdentityKeyPair tmpIdentity = KeyHelper.generateIdentityKeyPair();
                    SignalServiceAccountManager accountManager = new SignalServiceAccountManager(App.ServiceUrls, CancelSource.Token, "Signal-Windows");
                    string uuid     = accountManager.GetNewDeviceUuid(CancelSource.Token);
                    string tsdevice = "tsdevice:/?uuid=" + Uri.EscapeDataString(uuid) + "&pub_key=" + Uri.EscapeDataString(Base64.encodeBytesWithoutPadding(tmpIdentity.getPublicKey().serialize()));
                    Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        View.SetQR(tsdevice);
                        QRVisible    = Visibility.Visible;
                        QRCodeString = tsdevice;
                    }).AsTask().Wait();

                    string tmpSignalingKey = Base64.encodeBytes(Util.getSecretBytes(52));
                    int registrationId     = (int)KeyHelper.generateRegistrationId(false);

                    NewDeviceLinkResult result = accountManager.FinishNewDeviceRegistration(tmpIdentity, tmpSignalingKey, password, false, true, registrationId, deviceName);
                    SignalStore store          = new SignalStore()
                    {
                        DeviceId           = (uint)result.DeviceId,
                        IdentityKeyPair    = Base64.encodeBytes(result.Identity.serialize()),
                        NextSignedPreKeyId = 1,
                        Password           = password,
                        PreKeyIdOffset     = 1,
                        Registered         = true,
                        RegistrationId     = (uint)registrationId,
                        SignalingKey       = tmpSignalingKey,
                        Username           = result.Number
                    };
                    LibsignalDBContext.SaveOrUpdateSignalStore(store);

                    /* reload registered state */
                    Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        UIEnabled = false;
                        App.Store = store;
                    }).AsTask().Wait();

                    /* create prekeys */
                    LibsignalDBContext.RefreshPreKeys(new SignalServiceAccountManager(App.ServiceUrls, store.Username, store.Password, (int)store.DeviceId, App.USER_AGENT));

                    /* reload again with prekeys and their offsets */
                    store = LibsignalDBContext.GetSignalStore();
                    Debug.WriteLine("success!");
                    Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        App.Store = store;
                        View.Finish(true);
                    }).AsTask().Wait();
                });
                await LinkingTask;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                Debug.WriteLine(e.StackTrace);
            }
        }
예제 #2
0
        public async Task BeginLinking()
        {
            try
            {
                CancelSource = new CancellationTokenSource();
                // clean the database from stale values
                await Task.Run(() =>
                {
                    LibsignalDBContext.PurgeAccountData();
                });

                (string password, IdentityKeyPair tmpIdentity) = await Task.Run(() =>
                {
                    string newPassword             = Base64.EncodeBytes(Util.GetSecretBytes(18));
                    IdentityKeyPair newTmpIdentity = KeyHelper.generateIdentityKeyPair();
                    return(newPassword, newTmpIdentity);
                });

                // fetch new device uuid
                SignalServiceAccountManager accountManager = new SignalServiceAccountManager(App.ServiceConfiguration, "Signal-Windows", new SignalWebSocketFactory());
                string uuid = await accountManager.GetNewDeviceUuid(CancelSource.Token, new SignalWebSocketFactory());

                string tsdevice = "tsdevice:/?uuid=" + Uri.EscapeDataString(uuid) + "&pub_key=" + Uri.EscapeDataString(Base64.EncodeBytesWithoutPadding(tmpIdentity.getPublicKey().serialize()));

                View.SetQR(tsdevice); //TODO generate qrcode in worker task
                QRVisible    = Visibility.Visible;
                QRCodeString = tsdevice;

                string tmpSignalingKey = Base64.EncodeBytes(Util.GetSecretBytes(52));
                int    registrationId  = (int)KeyHelper.generateRegistrationId(false);

                var provisionMessage = await accountManager.GetProvisioningMessage(CancelSource.Token, tmpIdentity);

                int deviceId = await accountManager.FinishNewDeviceRegistration(CancelSource.Token, provisionMessage, tmpSignalingKey, password, false, true, registrationId, View.GetDeviceName());

                SignalStore store = new SignalStore()
                {
                    DeviceId           = (uint)deviceId,
                    IdentityKeyPair    = Base64.EncodeBytes(provisionMessage.Identity.serialize()),
                    NextSignedPreKeyId = 1,
                    Password           = password,
                    PreKeyIdOffset     = 1,
                    Registered         = true,
                    RegistrationId     = (uint)registrationId,
                    SignalingKey       = tmpSignalingKey,
                    Username           = provisionMessage.Number
                };
                await Task.Run(() =>
                {
                    LibsignalDBContext.SaveOrUpdateSignalStore(store);
                });

                // reload registered state
                UIEnabled        = false;
                App.Handle.Store = store;

                // create prekeys
                await LibsignalDBContext.RefreshPreKeys(CancelSource.Token, new SignalServiceAccountManager(App.ServiceConfiguration, store.Username, store.Password, (int)store.DeviceId, App.USER_AGENT));

                // reload again with prekeys and their offsets
                App.Handle.Store = LibsignalDBContext.GetSignalStore();
                await View.Finish(true);
            }
            catch (Exception e)
            {
                var line = new StackTrace(e, true).GetFrames()[0].GetFileLineNumber();
                Logger.LogError("BeginLinking() failed in line {0}: {1}\n{2}", line, e.Message, e.StackTrace);
            }
        }