1 import type { Address as tsAddress, User as tsUser } from '../../lib/interfaces';
2 import { getDecryptedAddressKeysHelper, getDecryptedUserKeysHelper } from '../../lib/keys';
4 getAddressReactivationPayload,
5 getReactivatedAddressKeys,
6 getReactivatedAddressesKeys,
7 } from '../../lib/keys/reactivation/reactivateKeyHelper';
8 import { getAddressKey, getUserKey } from './keyDataHelper';
10 const DEFAULT_KEYPASSWORD = '1';
12 const getSetup1 = async () => {
13 const keyPassword = DEFAULT_KEYPASSWORD;
14 const UserKeysFull = await Promise.all([getUserKey('1', keyPassword), getUserKey('2', keyPassword)]);
16 Keys: UserKeysFull.map(({ Key }) => Key),
17 } as unknown as tsUser;
18 const address1 = 'test@test.com';
19 const userKeys = await getDecryptedUserKeysHelper(User, keyPassword);
20 const addressKeysFull = await Promise.all([
21 getAddressKey('a', userKeys[0].privateKey, address1),
22 getAddressKey('b', userKeys[0].privateKey, address1),
23 getAddressKey('c', userKeys[1].privateKey, address1),
28 Keys: addressKeysFull.map(({ Key }) => Key),
29 } as unknown as tsAddress;
34 addressKeys: await getDecryptedAddressKeysHelper(Address.Keys, User, userKeys, keyPassword),
38 const getSetup2 = async () => {
39 const keyPassword = DEFAULT_KEYPASSWORD;
40 const UserKeysFull = await Promise.all([
41 getUserKey('1', keyPassword),
42 getUserKey('2', keyPassword),
43 getUserKey('3', keyPassword),
44 getUserKey('4', keyPassword),
47 Keys: UserKeysFull.map(({ Key }) => Key),
48 } as unknown as tsUser;
49 const address1 = 'test@test.com';
50 const userKeys = await getDecryptedUserKeysHelper(User, keyPassword);
51 const AddressKeys1 = await Promise.all([
52 getAddressKey('1a', userKeys[0].privateKey, address1),
53 getAddressKey('1b', userKeys[0].privateKey, address1),
54 getAddressKey('1c', userKeys[1].privateKey, address1),
55 getAddressKey('1d', userKeys[2].privateKey, address1),
57 const address2 = 'test2@test.com';
58 const AddressKeys2 = await Promise.all([
59 getAddressKey('2a', userKeys[1].privateKey, address2),
60 getAddressKey('2b', userKeys[1].privateKey, address2).then((r) => ({ ...r, Key: { ...r.Key, Flags: 0 } })),
61 getAddressKey('2c', userKeys[2].privateKey, address2),
62 getAddressKey('2d', userKeys[2].privateKey, address2),
63 getAddressKey('2e', userKeys[3].privateKey, address2),
68 Keys: AddressKeys1.map(({ Key }) => Key),
69 } as unknown as tsAddress;
73 Keys: AddressKeys2.map(({ Key }) => Key),
74 } as unknown as tsAddress;
77 Addresses: [Address1, Address2],
79 addressKeys1: await getDecryptedAddressKeysHelper(Address1.Keys, User, userKeys, keyPassword),
80 addressKeys2: await getDecryptedAddressKeysHelper(Address2.Keys, User, userKeys, keyPassword),
84 describe('reactivate address keys', () => {
85 it('should return an empty result', async () => {
86 const { User, Address, userKeys } = await getSetup1();
87 const result = await getReactivatedAddressKeys({
90 oldUserKeys: userKeys,
91 newUserKeys: userKeys,
93 keyTransparencyVerify: async () => {},
95 expect(result).toEqual({
97 reactivatedKeys: undefined,
98 signedKeyList: undefined,
102 it('should return keys that got reactivated', async () => {
103 const { User, Address, userKeys } = await getSetup1();
104 const result = await getReactivatedAddressKeys({
107 oldUserKeys: [userKeys[0]],
108 newUserKeys: userKeys,
110 keyTransparencyVerify: async () => {},
112 expect(result).toEqual(
113 jasmine.objectContaining({
115 reactivatedKeys: jasmine.any(Array),
116 signedKeyList: jasmine.any(Object),
121 it('should get correct payload from keys that got reactivated', async () => {
122 const { User, Address, userKeys, addressKeys } = await getSetup1();
123 const result = await getReactivatedAddressKeys({
126 oldUserKeys: [userKeys[0]],
127 newUserKeys: userKeys,
129 keyTransparencyVerify: async () => {},
131 const payload = await getAddressReactivationPayload([result]);
132 expect(payload).toEqual(
133 jasmine.objectContaining({
134 AddressKeyFingerprints: [...[addressKeys[2]].map(({ privateKey }) => privateKey.getFingerprint())],
137 Data: jasmine.any(String),
138 Signature: jasmine.any(String),
145 it('should get correct payload from keys that got reactivated on multiple addresses', async () => {
146 const { User, Addresses, userKeys, addressKeys1, addressKeys2 } = await getSetup2();
147 const result = await getReactivatedAddressesKeys({
149 addresses: Addresses,
150 oldUserKeys: [userKeys[0]],
151 newUserKeys: [userKeys[0], userKeys[1]],
153 keyTransparencyVerify: async () => {},
155 expect(JSON.parse(result[0].signedKeyList?.Data || '')).toEqual([
156 { Primary: 1, Flags: 3, Fingerprint: jasmine.any(String), SHA256Fingerprints: jasmine.any(Array) },
157 { Primary: 0, Flags: 3, Fingerprint: jasmine.any(String), SHA256Fingerprints: jasmine.any(Array) },
161 Fingerprint: jasmine.any(String),
162 SHA256Fingerprints: jasmine.any(Array),
165 expect(JSON.parse(result[1].signedKeyList?.Data || '')).toEqual([
166 { Primary: 1, Flags: 1, Fingerprint: jasmine.any(String), SHA256Fingerprints: jasmine.any(Array) },
170 Fingerprint: jasmine.any(String),
171 SHA256Fingerprints: jasmine.any(Array),
174 const payload = await getAddressReactivationPayload(result);
175 expect(payload).toEqual(
176 jasmine.objectContaining({
177 AddressKeyFingerprints: [
178 ...[addressKeys1[2], addressKeys2[0], addressKeys2[1]].map(({ privateKey }) =>
179 privateKey.getFingerprint()
184 Data: jasmine.any(String),
185 Signature: jasmine.any(String),
188 Data: jasmine.any(String),
189 Signature: jasmine.any(String),