This repository has been archived by the owner on May 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
68 lines (61 loc) · 1.83 KB
/
example.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
// SPDX-FileCopyrightText: 2021 Anders Rune Jensen
//
// SPDX-License-Identifier: Unlicense
const fs = require('fs')
const path = require('path')
const ssbKeys = require('ssb-keys')
const bencode = require('bencode')
const mfff = require('./')
const bfe = require('./ssb-bfe')
const msg = {
previous: '%H3MlLmVPVgHU6rBSzautUBZibDttkI+cU4lAFUIM8Ag=.bbmsg-v1',
author: '@6CAxOI3f+LUOVrbAl0IemqiS7ATpQvr9Mdw9LC4+Uv0=.bbfeed-v1',
sequence: 2,
timestamp: 1456154934819,
content: {
type: 'metafeed/add',
feedformat: 'classic',
subfeed: '@6CAxOI3f+LUOVrbAl0IemqiS7ATpQvr9Mdw9LC4+Uv0=.bbfeed-v1', // bad example
bool: true,
tangles: {
metafeed: {
root: null,
previous: ['%H3MlLmVPVgHU6rBSzautUBZibDttkI+cU4lAFUIM8Ag=.bbmsg-v1'],
},
},
},
}
const keys = ssbKeys.loadOrCreateSync(path.join('/home/arj/.ssb', 'secret'))
var curve = require('ssb-keys/sodium')
toBuffer = function (buf) {
if (buf == null) return buf
if (Buffer.isBuffer(buf)) return buf
var i = buf.indexOf('.')
var start = 0
return Buffer.from(buf.substring(start, ~i ? i : buf.length), 'base64')
}
const encodedContent = bfe.encode.convert(msg.content)
msg.contentSignature = bfe.decode.signature(
Buffer.concat([
Buffer.from([4]),
Buffer.from([0]),
curve.sign(toBuffer(keys.private), bencode.encode(encodedContent)),
])
)
const payload = [
bfe.encode.feed(msg.author),
msg.sequence,
bfe.encode.message(msg.previous),
msg.timestamp,
[bfe.encode.convert(msg.content), bfe.encode.signature(msg.contentSignature)],
]
msg.signature = bfe.decode.signature(
Buffer.concat([
Buffer.from([4]),
Buffer.from([0]),
curve.sign(toBuffer(keys.private), bencode.encode(payload)),
])
)
console.log(JSON.stringify(msg, null, 2))
//const data = fs.readFileSync('m0.bencode')
//console.log(mfff.decode(data))