This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseedBackup.js
70 lines (61 loc) · 1.96 KB
/
seedBackup.js
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
import bip39 from 'bip39'
import { BIP32Factory as bip32 } from 'bip32'
import * as ecc from 'tiny-secp256k1'
import readline from 'node:readline'
import SDK, { constants } from '@synonymdev/slashtags-sdk'
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const mnemonic = await getMnemonic(rl)
console.log(`Using mnemonic: "${mnemonic}"`)
const drive = await getDrive(mnemonic)
const currentContent = await drive.get('/seed/test')
if (currentContent) {
console.log('Current content of /seed/test is:', currentContent.toString())
} else {
console.log('Current drive is empty')
}
await puToDrive(rl, drive)
rl.on('close', async () => {
await drive.close()
console.log('Input stream closed.')
process.exit()
})
function getMnemonic (rl) {
return new Promise((resolve, reject) => {
rl.question('Type seed phrase or press Enter to generate new one: ', (line) => {
const mnemonic = line || bip39.generateMnemonic()
if (!bip39.validateMnemonic(mnemonic)) {
console.log('Invalid Mnemonic')
reject('Invalid Mnemonic')
process.exit()
}
resolve(mnemonic)
})
})
}
function puToDrive (rl, drive) {
return new Promise((resolve, reject) => {
rl.question('Type something to be stored on your public drive: ', async (line) => {
try {
await drive.put('/seed/test', Buffer.from(line))
console.log(`Stored ${line} to public drive`)
console.log('Exit the process and try again with precviously used seed')
process.exit()
} catch (e) {
reject(e)
}
})
})
}
async function getDrive (mnemonic) {
const seed = await bip39.mnemonicToSeed(mnemonic)
const root = bip32(ecc).fromSeed(seed)
const primaryKey = root.derivePath(constants.PRIMARY_KEY_DERIVATION_PATH).privateKey
const sdk = new SDK({ primaryKey })
const slashtag = sdk.slashtag('seed demo')
const drive = slashtag.drivestore.get()
await drive.ready()
return drive
}