-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeychain_update.js
More file actions
130 lines (106 loc) · 5.16 KB
/
keychain_update.js
File metadata and controls
130 lines (106 loc) · 5.16 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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, keychain = undefined, index = 0) {
const keychainSeed = randomBytes(32)
if (keychain === undefined) {
keychain = new Keychain(keychainSeed)
.addService("uco", "m/650'/0/0")
.addAuthorizedPublicKey(accessPublicKey)
}
return archethic.account.newKeychainTransaction(keychain, index)
.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
}
logger.debug("Keychain retrieved with success")
keychain.addService("website", "m/650'/website/0")
const newKeychainTx = generateKeychainTransaction(accessPublicKey, keychain, 1)
newKeychainTx
.on("sent", () => {
logger.debug("Keychain's update transaction sent")
logger.debug("Await validation ...")
})
.on("requiredConfirmation", async (confirmation, sender) => {
sender.unsubscribe()
logger.debug(`Keychain's update transaction created - ${Utils.uint8ArrayToHex(newKeychainTx.address)}`)
logger.debug("Keychain fetching...")
const keychain = await archethic.account.getKeychain(accessSeed)
if (keychain.services.website === undefined) {
reject(`Keychain' update doestn't match`)
return
}
resolve("Keychain updated with success")
return
})
.on("error", (context, reason) => {
reject(`Keychain's update transaction failed - ${reason}`)
return
})
.send()
})
.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)