-
Notifications
You must be signed in to change notification settings - Fork 2
/
arweave.js
128 lines (120 loc) · 3.51 KB
/
arweave.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
require('dotenv').config()
const Arweave = require('arweave')
const fetch = require('node-fetch')
const arweave = Arweave.init({
host: 'arweave.net',
port: 443,
protocol: 'https',
timeout: 20000,
logging: false,
})
const ADMIN_ADDR = process.env.ARWEAVE_ADDRESS
const KEY = JSON.parse(process.env.ARWEAVE_KEY)
const DOC_TYPE = "interdependence_doc_type"
const DOC_ORIGIN = "interdependence_doc_origin"
const DOC_REF = "interdependence_doc_ref"
const SIG_NAME = "interdependence_sig_name"
const SIG_HANDLE = "interdependence_sig_handle"
const SIG_ADDR = "interdependence_sig_addr"
const SIG_ISVERIFIED = "interdependence_sig_verified"
const SIG_SIG = "interdependence_sig_signature"
const VERIFICATION_HANDLE = "interdependence_verif_handle"
const VERIFICATION_ADDR = "interdependence_verif_addr"
async function checkIfVerifiedAr(handle, address) {
const req = await fetch('https://arweave.net/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
query: `
query {
transactions(
tags: [
{
name: "${DOC_TYPE}",
values: ["verification"]
}
],
owners: ["${ADMIN_ADDR}"]
) {
edges {
node {
id
owner {
address
}
tags {
name
value
}
}
}
}
}
`
})
})
const json = await req.json()
for (const edge of json.data.transactions.edges) {
const n = edge.node
if (n.owner.address === ADMIN_ADDR) {
const parsedHandle = n.tags.find(tag => tag.name === VERIFICATION_HANDLE).value
const parsedAddress = n.tags.find(tag => tag.name === VERIFICATION_ADDR).value
if (handle === parsedHandle && address === parsedAddress) {
return n.id
}
}
}
return false
}
async function persistVerificationAr(handle, address) {
let transaction = await arweave.createTransaction({
data: handle
}, KEY)
transaction.addTag(DOC_TYPE, 'verification')
transaction.addTag(VERIFICATION_HANDLE, handle)
transaction.addTag(VERIFICATION_ADDR, address)
await arweave.transactions.sign(transaction, KEY)
return {
...await arweave.transactions.post(transaction),
id: transaction.id,
}
}
async function signDocumentAr(documentId, address, name, handle, signature, isVerified) {
let transaction = await arweave.createTransaction({ data: address }, KEY)
transaction.addTag(DOC_TYPE, 'signature')
transaction.addTag(DOC_REF, documentId)
transaction.addTag(SIG_NAME, name)
transaction.addTag(SIG_HANDLE, handle)
transaction.addTag(SIG_ADDR, address)
transaction.addTag(SIG_SIG, signature)
transaction.addTag(SIG_ISVERIFIED, isVerified)
await arweave.transactions.sign(transaction, KEY)
return await arweave.transactions.post(transaction)
}
async function forkDocumentAr(oldDocumentId, text, title, authors) {
let transaction = await arweave.createTransaction({
data: JSON.stringify({
title,
document: text,
authors: authors
})
}, KEY)
transaction.addTag(DOC_TYPE, 'document')
if (oldDocumentId) {
transaction.addTag(DOC_ORIGIN, oldDocumentId)
}
await arweave.transactions.sign(transaction, KEY)
return {
...await arweave.transactions.post(transaction),
id: transaction.id,
}
}
module.exports = {
checkIfVerifiedAr,
persistVerificationAr,
signDocumentAr,
forkDocumentAr,
}