-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmmt-metadata.js
135 lines (94 loc) · 3.1 KB
/
mmt-metadata.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
var pull = require('pull-stream')
var ssbClient = require('ssb-client')
// will hold current payments
var payments = {}
ssbClient(function (err, sbot) {
console.log('ssb ready.')
var payment = {
// the 'key' would be a bitcoin transaction id
key: 'd5f2a6a8cd1e8c35466cfec16551',
// the actual metadata
// note - no date, amounts or recieve addresses - for this we have a better
// source of truth
rate: 5000,
cosigners: ['ssbpublickey1', 'ssbpublickey2'],
description: 'this is just an example',
comments: []
}
//addPayment(sbot, payment)
var paymentComment = {
key: 'd5f2a6a8cd1e8c35466cfec16551',
comment: 'this payment was a mistake'
}
//addPaymentComment(sbot, paymentComment)
pull(sbot.createLogStream({ live: true }), pull.drain(processMsg))
//pull(sbot.messagesByType({ type: 'addMmtPaymentTest', live: true }), pull.drain(processMsg))
sbot.close()
})
function testPublish() {
//publish a test message
sbot.publish({ type: 'testtype', text: 'hello, scuttleverse' }, function (err, msg) {
console.log(msg.key)
console.log(msg.value.author)
console.log(msg.value.content)
})
}
function processMsg (msg) {
// process a message from the drain
// is this the right way to handle the end of the message stream?
try {
if (msg)
switch(msg.value.content.type) {
case 'addMmtPaymentTest':
console.log('Found a payment:')
console.log(msg.value.content)
// if we've never seen this transaction before, add it
if (!payments[msg.value.content.payment.key]) {
payments[msg.value.content.payment.key] = msg.value.content.payment
} else {
// what to do here?
}
break
case 'modifyMmtPaymentTest':
console.log('Found a payment comment:')
console.log(msg.value.content)
payments[msg.value.content.paymentComment.key].comments.push( {
author: msg.value.author,
comment: msg.value.content.paymentComment.comment
} )
}
} catch(e) {
displayPayments()
return false
}
}
function pullWithFeedStream() {
// not using this right now
pull(
sbot.createFeedStream(),
pull.collect(function (err, msgs) {
console.log(msgs[1].key)
console.log(msgs[1].value)
})
)
}
function addPayment(sbot, paymentToAdd) {
sbot.publish({ type: 'addMmtPaymentTest', payment: paymentToAdd }, function (err, msg) {
console.log('Adding payment:')
console.log(msg.key)
console.log(msg.value.author)
console.log(msg.value.content)
})
}
function addPaymentComment(sbot, paymentComment) {
sbot.publish({ type: 'modifyMmtPaymentTest', paymentComment: paymentComment }, function (err, msg) {
console.log('Adding payment Comment:')
console.log(msg.key)
console.log(msg.value.author)
console.log(msg.value.content)
})
}
function displayPayments() {
console.log('payments now looks like this:')
console.log(JSON.stringify(payments, null, 4))
}