forked from ipfs-shipyard/git-remote-ipld
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgit-igis
executable file
·115 lines (95 loc) · 4.03 KB
/
git-igis
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
#!/usr/bin/env node
console.error = console.debug = console.warn = (
(msg) => process.stderr.write(`${msg}\n`)
)
const yargs = require('yargs');
const IPFSProxy = require('ipfs-http-client')
const OrbitDB = require('orbit-db')
var levelup = require('levelup')
var leveldown = require('leveldown')
const genSeed = () => {
const length = 81
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ9'
var randomValues = new Uint32Array(length)
var result = new Array(length)
window.crypto.getRandomValues(randomValues)
let cursor = 0
for(var i = 0; i < randomValues.length; i++) {
cursor += randomValues[i]
result[i] = chars[cursor % chars.length]
}
return result.join()
}
;(async () => {
const cacheDir = `${__dirname}/.git/remote-igis`
const argv = (
yargs
.command('hash-cache:clear', 'Remove all entries from OID cache')
.command('hash-cache:dump', 'Display all entries from OID cache')
.help().alias('help', 'h')
.argv
)
if(argv._.includes('hash-cache:clear')) {
const ipfs = IPFSProxy()
const orbitdb = await OrbitDB.createInstance(ipfs, { directory: cacheDir })
const dbOptions = { accessController: { write: [orbitdb.identity.id] }}
const cache = await orbitdb.kvstore('igis:hash-cache', dbOptions)
cache.drop()
}
if(argv._.includes('ls')) {
const ipfs = IPFSProxy()
const orbitdb = await OrbitDB.createInstance(ipfs, { directory: cacheDir })
const dbOptions = { accessController: { write: [orbitdb.identity.id] } }
const refs = await orbitdb.kvstore('igis:revs', dbOptions)
console.log(`Reading From: ${refs.address}`)
await Promise.all(Object.entries(refs.index).map(async ([ref, cid]) => {
console.log('REF', ref, cid)
}))
}
if(argv._.includes('hash-cache:dump')) {
const cache = levelup(leveldown(cacheDir))
await new Promise((resolve, reject) => (
cache.createReadStream()
.on('data', function (data) {
process.stdout.write(`${data.key} :: ${data.value}\n`)
})
.on('error', reject)
.on('close', resolve)
.on('end', resolve)
))
cache.close()
}
if(argv._.includes('publish')) {
const { channelRoot, createChannel, createMessage, parseMessage, mamAttach, mamFetch, mamFetchAll } = require('@iota/mam.js');
// Setup the details for the channel.
const seed = genSeed()
const mode = 'public'
// Create a new channel using the details
// You could also load the state from persistence.
const channelState = createChannel(seed, 2, mode)
// Create a MAM message using the channel state.
// The returned mamMessage will contain address, root, nextRoot and payload.
// The channel state is also updated, so you should persist it if you want
// to add further messages in the same channel.
// The payload should be attached to the tangle.
const mamMessage = createMessage(channelState, 'MY9MESSAGE');
console.debug(mamMessage)
// Decode the message using the root and sideKey.
// The decodedMessage will contain nextRoot and message.
const decodedMessage = parseMessage(mamMessage.payload, mamMessage.root, sideKey);
// If we want to attach the message to the tangle we first compose the API
const api = composeAPI({ provider: "https://altnodes.devnet.iota.org:443" });
// And then attach the message, tagging it if required.
// Attaching will return the actual transactions attached to the tangle if you need them.
await mamAttach(api, mamMessage, 3, 9, "MY9MAM");
// We can also fetch a message given its root and channel details.
// The fetched data will contain the nextRoot and the message.
const fetched = await mamFetch(api, mamMessage.root, mode, sideKey)
// If you want to fetch multiple messages from a channel
// you need either its initial root (or start from another root).
const channelState = createChannel(seed, 2, mode, sideKey);
const initialRoot = channelRoot(channelState);
const chunkSize = 4;
const chunk = await mamFetchAll(api, channelState.initialRoot, mode, sideKey, chunkSize);
}
})()