Skip to content

Commit 6bcf0ea

Browse files
authored
feat: Backup and Restore (#215)
Signed-off-by: Curtis Harding <[email protected]>
1 parent 78838ea commit 6bcf0ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1601
-206
lines changed

Diff for: .vscode/launch.json

+19-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"request": "launch",
3333
"program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
3434
"args": [
35-
"${fileBasenameNoExtension}",
3635
"--colors",
3736
"--workerThreads",
3837
"--detectOpenHandles",
@@ -44,6 +43,24 @@
4443
"${workspaceRoot}/../../node_modules/**/*",
4544
"<node_internals>/**/*"
4645
]
46+
},
47+
{
48+
"name": "TEST:file",
49+
"type": "node",
50+
"request": "launch",
51+
"program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
52+
"args": [
53+
"--colors",
54+
"--workerThreads",
55+
"--detectOpenHandles",
56+
"--maxWorkers",
57+
"1",
58+
"${fileBasenameNoExtension}",
59+
],
60+
"skipFiles": [
61+
"${workspaceRoot}/../../node_modules/**/*",
62+
"<node_internals>/**/*"
63+
]
4764
}
4865
]
49-
}
66+
}

Diff for: docs/examples/Backup.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Edge SDK Backup & Restore
2+
3+
## Requirements
4+
1. SDK A - a used instance with data in the store.
5+
2. SDK B - an unused instance, with an empty store.
6+
7+
8+
## Flow
9+
1. SDK A creates a Backup `jwe`
10+
2. SDK B restores from the `jwe`
11+
12+
13+
## Code Reference
14+
* Create the `jwe` using the Agent function provided.
15+
16+
Example
17+
```TS
18+
const jwe = await Agent_a.backup.createJWE();
19+
```
20+
21+
* Transfer the `jwe` to another SDK.
22+
* Restore from the `jwe` using the Agent function provided.
23+
24+
Example
25+
```TS
26+
await Agent_b.backup.restore(jwe);
27+
```

Diff for: package-lock.json

+16-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,10 @@
180180
"outputName": "junit.xml"
181181
},
182182
"dependencies": {
183-
"@atala/apollo": "~1.2.16",
183+
"@atala/apollo": "^1.3.4",
184184
"@scure/bip32": "^1.3.0",
185185
"@scure/bip39": "^1.1.1",
186+
"@sinclair/typebox": "^0.32.31",
186187
"@stablelib/base64": "^1.0.1",
187188
"@stablelib/sha256": "^1.0.1",
188189
"@stablelib/uuid": "^1.0.2",
@@ -210,4 +211,4 @@
210211
"overrides": {
211212
"crypto-js": "^4.2.0"
212213
}
213-
}
214+
}

Diff for: src/apollo/Apollo.ts

+29-3
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ import { Secp256k1PublicKey } from "./utils/Secp256k1PublicKey";
3030
import { Ed25519PublicKey } from "./utils/Ed25519PublicKey";
3131
import { X25519PublicKey } from "./utils/X25519PublicKey";
3232

33-
import ApolloPKG from "@atala/apollo";
3433
import { DerivationPath } from "./utils/derivation/DerivationPath";
35-
36-
const ApolloSDK = ApolloPKG.io.iohk.atala.prism.apollo;
34+
import { notEmptyString } from "../utils";
35+
import ApolloPKG from "@atala/apollo";
36+
const ApolloSDK = ApolloPKG.org.hyperledger.identus.apollo;
3737
const Mnemonic = ApolloSDK.derivation.Mnemonic.Companion;
3838
const HDKey = ApolloSDK.derivation.HDKey;
3939
const BigIntegerWrapper = ApolloSDK.derivation.BigIntegerWrapper;
40+
4041
/**
4142
* Apollo defines the set of cryptographic operations.
4243
*
@@ -274,6 +275,18 @@ export default class Apollo implements ApolloInterface, KeyRestoration {
274275
if (keyData) {
275276
return new Ed25519PrivateKey(keyData);
276277
}
278+
279+
const seedHex = parameters[KeyProperties.seed];
280+
if (notEmptyString(seedHex)) {
281+
const derivationParam = parameters[KeyProperties.derivationPath] ?? "m/0'/0'/0'";
282+
const derivationPath = DerivationPath.from(derivationParam);
283+
const seed = Int8Array.from(Buffer.from(seedHex, "hex"));
284+
const hdKey = ApolloSDK.derivation.EdHDKey.Companion.initFromSeed(seed).derive(derivationPath.toString());
285+
const edKey = Ed25519PrivateKey.from.Buffer(Buffer.from(hdKey.privateKey));
286+
287+
return edKey;
288+
}
289+
277290
const keyPair = Ed25519KeyPair.generateKeyPair();
278291
return keyPair.privateKey;
279292
}
@@ -322,6 +335,19 @@ export default class Apollo implements ApolloInterface, KeyRestoration {
322335
if (keyData) {
323336
return new X25519PrivateKey(keyData);
324337
}
338+
339+
const seedHex = parameters[KeyProperties.seed];
340+
if (notEmptyString(seedHex)) {
341+
const derivationParam = parameters[KeyProperties.derivationPath] ?? "m/0'/0'/0'";
342+
const derivationPath = DerivationPath.from(derivationParam);
343+
const seed = Int8Array.from(Buffer.from(seedHex, "hex"));
344+
const hdKey = ApolloSDK.derivation.EdHDKey.Companion.initFromSeed(seed).derive(derivationPath.toString());
345+
const edKey = Ed25519PrivateKey.from.Buffer(Buffer.from(hdKey.privateKey));
346+
const xKey = edKey.x25519();
347+
348+
return xKey;
349+
}
350+
325351
const keyPair = X25519KeyPair.generateKeyPair();
326352
return keyPair.privateKey;
327353
}

Diff for: src/apollo/utils/Ed25519KeyPair.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import ApolloBaseAsymmetricEncryption from "@atala/apollo";
21
import { KeyPair } from "../../domain";
32
import { Ed25519PrivateKey } from "./Ed25519PrivateKey";
43
import { Ed25519PublicKey } from "./Ed25519PublicKey";
4+
import ApolloPKG from "@atala/apollo";
5+
const ApolloSDK = ApolloPKG.org.hyperledger.identus.apollo;
56

67
/**
78
* @ignore
@@ -15,8 +16,7 @@ export class Ed25519KeyPair extends KeyPair {
1516
}
1617

1718
static generateKeyPair() {
18-
const keyPair =
19-
ApolloBaseAsymmetricEncryption.io.iohk.atala.prism.apollo.utils.KMMEdKeyPair.Companion.generateKeyPair();
19+
const keyPair = ApolloSDK.utils.KMMEdKeyPair.Companion.generateKeyPair();
2020

2121
return new Ed25519KeyPair(
2222
new Ed25519PrivateKey(keyPair.privateKey.raw),

Diff for: src/apollo/utils/Ed25519PrivateKey.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import ApolloPkg from "@atala/apollo";
21
import { Ed25519PublicKey } from "./Ed25519PublicKey";
2+
import { X25519PrivateKey } from "./X25519PrivateKey";
33
import {
44
Curve,
55
ExportableKey,
@@ -11,6 +11,8 @@ import {
1111
SignableKey
1212
} from "../../domain";
1313

14+
import ApolloPKG from "@atala/apollo";
15+
const ApolloSDK = ApolloPKG.org.hyperledger.identus.apollo;
1416

1517
/**
1618
* @ignore
@@ -48,15 +50,17 @@ export class Ed25519PrivateKey extends PrivateKey implements ExportableKey, Sign
4850
return Buffer.from(signature);
4951
}
5052

53+
x25519() {
54+
const key = this.getInstance().x25519PrivateKey();
55+
return X25519PrivateKey.from.Buffer(key.raw);
56+
}
57+
5158
private getInstance(
5259
value?: Int8Array | Uint8Array
53-
): ApolloPkg.io.iohk.atala.prism.apollo.utils.KMMEdPrivateKey {
60+
) {
5461
// eslint-disable-next-line no-extra-boolean-cast
5562
const bytes = !!value ? Buffer.from(value) : this.raw;
56-
const instance =
57-
new ApolloPkg.io.iohk.atala.prism.apollo.utils.KMMEdPrivateKey(
58-
Int8Array.from(bytes)
59-
);
63+
const instance = new ApolloSDK.utils.KMMEdPrivateKey(Int8Array.from(bytes));
6064

6165
return instance;
6266
}

Diff for: src/apollo/utils/Ed25519PublicKey.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import ApolloPkg from "@atala/apollo";
21
import {
32
Curve,
43
ExportableKey,
@@ -10,6 +9,9 @@ import {
109
VerifiableKey
1110
} from "../../domain";
1211

12+
import ApolloPKG from "@atala/apollo";
13+
const ApolloSDK = ApolloPKG.org.hyperledger.identus.apollo;
14+
1315
/**
1416
* @ignore
1517
*/
@@ -46,13 +48,10 @@ export class Ed25519PublicKey extends PublicKey implements ExportableKey, Storab
4648

4749
private getInstance(
4850
value?: Int8Array | Uint8Array
49-
): ApolloPkg.io.iohk.atala.prism.apollo.utils.KMMEdPublicKey {
51+
) {
5052
// eslint-disable-next-line no-extra-boolean-cast
5153
const bytes = !!value ? Buffer.from(value) : this.raw;
52-
const instance =
53-
new ApolloPkg.io.iohk.atala.prism.apollo.utils.KMMEdPublicKey(
54-
Int8Array.from(bytes)
55-
);
54+
const instance = new ApolloSDK.utils.KMMEdPublicKey(Int8Array.from(bytes));
5655

5756
return instance;
5857
}

Diff for: src/apollo/utils/Secp256k1KeyPair.ts

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { KeyPair, MnemonicWordList } from "../../domain";
2-
32
import { Secp256k1PrivateKey } from "./Secp256k1PrivateKey";
43
import { Secp256k1PublicKey } from "./Secp256k1PublicKey";
5-
6-
import * as ApolloPKG from "@atala/apollo";
4+
import ApolloPKG from "@atala/apollo";
5+
const ApolloSDK = ApolloPKG.org.hyperledger.identus.apollo;
76

87
/**
98
* @ignore
@@ -17,16 +16,7 @@ export class Secp256k1KeyPair extends KeyPair {
1716
}
1817

1918
static generateKeyPair() {
20-
const {
21-
io: {
22-
iohk: {
23-
atala: {
24-
prism: { apollo },
25-
},
26-
},
27-
},
28-
} = ApolloPKG;
29-
const Mnemonic = apollo.derivation.Mnemonic.Companion;
19+
const Mnemonic = ApolloSDK.derivation.Mnemonic.Companion;
3020
const mnemonics = Mnemonic.createRandomMnemonics() as MnemonicWordList;
3121
const seed = Mnemonic.createSeed(mnemonics, `mnemonic`);
3222
const priv = new Secp256k1PrivateKey(Uint8Array.from(seed.slice(0, 32)));

Diff for: src/apollo/utils/Secp256k1PrivateKey.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import ApolloPkg from "@atala/apollo";
21
import BN from "bn.js";
32

43
import * as ECConfig from "../../config/ECConfig";
@@ -14,9 +13,10 @@ import {
1413
StorableKey,
1514
} from "../../domain/models/keyManagement";
1615

17-
const Apollo = ApolloPkg.io.iohk.atala.prism.apollo;
18-
const HDKey = Apollo.derivation.HDKey;
19-
const BigIntegerWrapper = Apollo.derivation.BigIntegerWrapper;
16+
import ApolloPKG from "@atala/apollo";
17+
const ApolloSDK = ApolloPKG.org.hyperledger.identus.apollo;
18+
const HDKey = ApolloSDK.derivation.HDKey;
19+
const BigIntegerWrapper = ApolloSDK.derivation.BigIntegerWrapper;
2020

2121
/**
2222
* @ignore
@@ -36,7 +36,7 @@ export class Secp256k1PrivateKey
3636
static from = ImportableKey.factory(Secp256k1PrivateKey, { pemLabel: "EC PRIVATE KEY" });
3737

3838
private get native() {
39-
return Apollo.utils.KMMECSecp256k1PrivateKey.Companion.secp256k1FromByteArray(
39+
return ApolloSDK.utils.KMMECSecp256k1PrivateKey.Companion.secp256k1FromByteArray(
4040
Int8Array.from(this.raw)
4141
);
4242
}

Diff for: src/apollo/utils/Secp256k1PublicKey.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import ApolloPkg from "@atala/apollo";
21
import BN from "bn.js";
32
import BigInteger from "bn.js";
43

@@ -13,6 +12,9 @@ import {
1312
VerifiableKey
1413
} from "../../domain/models/keyManagement";
1514

15+
import ApolloPKG from "@atala/apollo";
16+
const ApolloSDK = ApolloPKG.org.hyperledger.identus.apollo;
17+
1618
/**
1719
* @ignore
1820
*/
@@ -35,7 +37,7 @@ export class Secp256k1PublicKey extends PublicKey implements StorableKey, Export
3537
}
3638

3739
private get native() {
38-
return ApolloPkg.io.iohk.atala.prism.apollo.utils.KMMECSecp256k1PublicKey.Companion.secp256k1FromBytes(
40+
return ApolloSDK.utils.KMMECSecp256k1PublicKey.Companion.secp256k1FromBytes(
3941
Int8Array.from(this.raw)
4042
);
4143
}
@@ -143,7 +145,7 @@ export class Secp256k1PublicKey extends PublicKey implements StorableKey, Export
143145
static secp256k1FromBytes(encoded: Uint8Array): Secp256k1PublicKey {
144146
return new Secp256k1PublicKey(
145147
Uint8Array.from(
146-
ApolloPkg.io.iohk.atala.prism.apollo.utils.KMMECSecp256k1PublicKey.Companion.secp256k1FromBytes(
148+
ApolloSDK.utils.KMMECSecp256k1PublicKey.Companion.secp256k1FromBytes(
147149
Int8Array.from(encoded)
148150
).raw
149151
)
@@ -178,7 +180,7 @@ export class Secp256k1PublicKey extends PublicKey implements StorableKey, Export
178180
const xCoord = Buffer.from(x.toArray());
179181
const yCoord = Buffer.from(y.toArray());
180182
const publicKey =
181-
ApolloPkg.io.iohk.atala.prism.apollo.utils.KMMECSecp256k1PublicKey.Companion.secp256k1FromByteCoordinates(
183+
ApolloSDK.utils.KMMECSecp256k1PublicKey.Companion.secp256k1FromByteCoordinates(
182184
Int8Array.from(xCoord),
183185
Int8Array.from(yCoord)
184186
);

0 commit comments

Comments
 (0)