-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
86 lines (78 loc) · 2.84 KB
/
main.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
/*!
* Copyright (c) 2019 Digital Bazaar, Inc. All rights reserved.
*/
'use strict';
//import * as base58 from 'base58-universal';
//import crypto from './crypto.js';
import * as defaultCodecs from './codecs.js';
import {Hashlink} from './Hashlink.js';
//import {stringToUint8Array} from './util.js';
// setup exports for this module
export {Hashlink} from './Hashlink.js';
export {
encode,
decode,
verify,
};
// setup the default encoder/decoder
const hlDefault = new Hashlink();
hlDefault.use(new defaultCodecs.MultihashSha2256());
hlDefault.use(new defaultCodecs.MultihashBlake2b64());
hlDefault.use(new defaultCodecs.MultibaseBase58btc());
/**
* Encodes a hashlink. If only a `url` parameter is provided, the URL is
* fetched, transformed, and encoded into a hashlink. If a data parameter
* is provided, the hashlink is encoded from the data.
*
* @param {object} options - The options for the encode operation.
* @param {Uint8Array} options.data - The data associated with the given URL. If
* provided, this data is used to encode the cryptographic hash.
* @param {Array} options.urls - One or more URLs that contain the data
* referred to by the hashlink.
* @param {Array} options.codecs - One or more URLs that contain the data
* referred to by the hashlink.
* @param {object} options.meta - A set of key-value metadata that will be
* encoded into the hashlink.
*
* @returns {Promise<string>} Resolves to a string that is a hashlink.
*/
async function encode({data, urls, url,
codecs = ['mh-sha2-256', 'mb-base58-btc'], meta = {}}) {
if(url && !urls) {
urls = [url];
}
return hlDefault.encode({data, urls, codecs, meta});
}
/**
* Decodes a hashlink resulting in an object with key-value pairs
* representing the values encoded in the hashlink.
*
* @param {object} options - The options for the encode operation.
* @param {string} options.hashlink - The encoded hashlink value to decode.
*
* @returns {object} Returns an object with the decoded hashlink values.
*/
function decode({
/* eslint-disable-next-line no-unused-vars */
hashlink
}) {
throw new Error('Not implemented.');
}
/**
* Verifies a hashlink resulting in a simple true or false value.
*
* @param {object} options - The options for the encode operation.
* @param {string} options.hashlink - The encoded hashlink value to verify.
* @param {Uint8Array} options.data - Optional data to use when verifying
* hashlink.
* @param {Array} options.resolvers - An array of Objects with key-value
* pairs. Each object must contain a `scheme` key associated with a
* Function({url, options}) that resolves any URL with the given scheme
* and options to data.
*
* @returns {Promise<boolean>} Return true if the hashlink is valid, false
* otherwise.
*/
async function verify({hashlink, data, resolvers}) {
return hlDefault.verify({hashlink, data, resolvers});
}