-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeychain_creation.js
More file actions
100 lines (81 loc) · 3.51 KB
/
keychain_creation.js
File metadata and controls
100 lines (81 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import Archethic, { Keychain, Crypto, Utils } from "@archethicjs/sdk"
import { randomBytes } from "crypto"
import { getLogger } from "./logger.js"
const logger = getLogger()
const originPrivateKey = Utils.originPrivateKey;
let archethic
function generateKeychainTransaction(accessPublicKey) {
const keychainSeed = randomBytes(32)
const keychain = new Keychain(keychainSeed)
.addService("uco", "m/650'/0/0")
.addAuthorizedPublicKey(accessPublicKey)
return archethic.account.newKeychainTransaction(keychain, 0)
.originSign(originPrivateKey)
}
function generateKeychainAccessTransaction(accessSeed, keychainAddress) {
return archethic.account
.newAccessTransaction(accessSeed, keychainAddress)
.originSign(originPrivateKey)
}
async function run() {
return new Promise(async function (resolve, reject) {
try {
const endpoint = process.env["ENDPOINT"] || "https://testnet.archethic.net"
archethic = new Archethic(endpoint)
await archethic.connect()
const accessSeed = randomBytes(32)
const { publicKey: accessPublicKey } = Crypto.deriveKeyPair(accessSeed, 0);
const keychainTx = generateKeychainTransaction(accessPublicKey)
keychainTx
.on("sent", () => {
logger.debug("Keychain transaction sent")
logger.debug("Await validation ...")
})
.on("requiredConfirmation", async (_confirmations, sender) => {
sender.unsubscribe()
logger.debug(`Keychain transaction created - ${Utils.uint8ArrayToHex(keychainTx.address)}`)
const accessKeychainTx = generateKeychainAccessTransaction(accessSeed, keychainTx.address)
accessKeychainTx
.on("sent", () => {
logger.debug("Keychain access transaction sent")
logger.debug("Await validation ...")
})
.on("requiredConfirmation", async (confirmation, sender) => {
sender.unsubscribe()
logger.debug(`Keychain access transaction created - ${Utils.uint8ArrayToHex(accessKeychainTx.address)}`)
logger.debug("Keychain fetching...")
const keychain = await archethic.account.getKeychain(accessSeed)
if (new TextDecoder().decode(keychainTx.data.content) != JSON.stringify(keychain.toDID())) {
reject("Keychain doesn't match")
return
}
resolve("Keychain retrieved with success")
return
})
.on("error", (context, reason) => {
reject(`Keychain accesss transaction failed - ${reason}`)
return
})
.send()
})
.on("error", (context, reason) => {
reject(`Keychain transaction failed - ${reason}`)
return
})
.send()
} catch (e) {
reject(e)
return
}
})
}
run()
.then((msg) => {
logger.info(msg)
return 0
})
.catch((msg) => {
logger.error(msg)
return 1
})
.then(logger.exit_when_flush)