This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebnative.js
137 lines (108 loc) · 3.08 KB
/
webnative.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
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
131
132
133
134
135
136
137
import * as wn from "webnative"
import * as BrowserCrypto from "webnative/components/crypto/implementation/browser"
import { PublicFile } from "webnative/fs/v1/PublicFile"
import { PublicTree } from "webnative/fs/v1/PublicTree"
import { getSimpleLinks } from "webnative/fs/protocol/basic"
import "./fetch-polyfill.js"
const USERNAME_TO_LOOKUP = "icidasset"
export async function lookup() {
const config = {
namespace: "public-data-viewer",
debug: true
}
// Components
const crypto = createCryptoComponent()
const depot = createDepotComponent()
const storage = wn.storage.memory()
const fissionComponents = await wn.compositions.fission({
...config,
crypto,
storage,
})
const componentsWithCustomDepot = {
...fissionComponents,
depot
}
// Create program
const program = await wn.assemble(
config,
componentsWithCustomDepot
)
const { reference } = program.components
const cid = await reference.dataRoot.lookup(USERNAME_TO_LOOKUP)
const publicCid = (await getSimpleLinks(depot, cid)).public.cid
const publicTree = await PublicTree.fromCID(depot, reference, publicCid)
const links = Object.values(await publicTree.ls(
wn.path.unwrap(wn.path.directory("Unsplash")) // [ "Unsplash" ]
))
// Render pictures
return await Promise.all(
links.map(async picture => {
const file = await PublicFile.fromCID(depot, picture.cid)
return `https://ipfs.runfission.com/ipfs/${picture.cid.toString()}/userland`
// Alternatively you can get the content from the file and convert it in a data URI
})
)
}
// Node.js Components
function boom() {
throw new Error("Method not implemented")
}
function createCryptoComponent() {
const {
aes,
did,
hash,
misc,
rsa,
} = BrowserCrypto
return {
aes,
did,
hash,
misc,
rsa,
// We're avoiding having to implement all of this,
// because we're not using it anyway.
// ---
// One way to actually implement this would be to
// set up the keystore-idb library to use an in-memory
// store instead of indexedDB. There's an example in
// the Webnative tests.
keystore: {
clearStore: boom,
decrypt: boom,
exportSymmKey: boom,
getAlgorithm: boom,
getUcanAlgorithm: boom,
importSymmKey: boom,
keyExists: boom,
publicExchangeKey: boom,
publicWriteKey: boom,
sign: boom,
}
}
}
function createDepotComponent() {
const ipfsGateway = `https://ipfs.runfission.com`
function ipfs(path) {
return fetch(`${ipfsGateway}${path}`)
.then(r => r.arrayBuffer())
.then(r => new Uint8Array(r))
}
return {
// Get the data behind a CID
getBlock: async cid => {
return ipfs(`/api/v0/block/get?arg=${cid.toString()}`)
},
getUnixFile: async cid => {
return ipfs(`/api/v0/cat?arg=${cid.toString()}`)
},
// We're avoiding having to implement all of this,
// because we're not using it anyway.
getUnixDirectory: boom,
putBlock: boom,
putChunked: boom,
size: boom,
}
}