-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathipfsHelper.ts
154 lines (143 loc) · 4.58 KB
/
ipfsHelper.ts
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright ©️ 2019 GaltProject Society Construction and Terraforming Company
* (Founded by [Nikolai Popeka](https://github.com/npopeka)
*
* Copyright ©️ 2019 Galt•Core Blockchain Company
* (Founded by [Nikolai Popeka](https://github.com/npopeka) by
* [Basic Agreement](ipfs/QmaCiXUmSrP16Gz8Jdzq6AJESY1EAANmmwha15uR3c1bsS)).
*/
import { CID } from 'multiformats';
import * as codec from "@ipld/dag-cbor";
import libp2pCrypto from 'libp2p-crypto';
import { sha256 } from 'multiformats/hashes/sha2';
import * as jsonCodec from 'multiformats/codecs/json';
import _ from 'lodash';
import common from "./common";
const {startsWith, isString, pick, isUndefined, isDate} = _;
// import * as dagCBOR from '@ipld/dag-cbor';
// import * as dagJSON from '@ipld/dag-json';
const ipfsHelper = {
isIpfsHash(value) {
if (!value) {
return false;
}
return (startsWith(value, 'Qm') || ipfsHelper.isCidHash(value)) && /^\w+$/.test(value);
},
isCidHash(value) {
if (!value) {
return false;
}
return startsWith(value.codec, 'dag-') || (isString(value) && value.length === 59 && /^\w+$/.test(value) && (startsWith(value, 'zd') || startsWith(value, 'ba')));
},
isFileCidHash(value) {
if (!value) {
return false;
}
//TODO: spec about bafybe
return isString(value) && value.length === 59 && /^\w+$/.test(value) && (startsWith(value, 'bafkre') || startsWith(value, 'bafybe'));
},
isObjectCidHash(value) {
if (!value) {
return false;
}
return isString(value) && (value.length === 59 || value.length === 61) && /^\w+$/.test(value) && (startsWith(value, 'bafyre') || startsWith(value, 'bagaai'));
},
isAccountCidHash(value) {
if (!value) {
return false;
}
return isString(value) && value.length === 59 && /^\w+$/.test(value) && startsWith(value, 'bafzbe');
},
isCid(value) {
const cid = CID.asCID(value);
return !!cid;
},
cidToHash(cid) {
// const cidsResult = new CID(1, 'dag-cbor', cid.multihash || Buffer.from(cid.hash.data));
return cid.toString();
},
cidToIpfsHash(cid) {
cid = CID.asCID(cid);
return cid.toString();
},
ipfsHashToCid(hash) {
return CID.parse(hash);
},
getObjectRef(storageId) {
return storageId;
},
pickObjectFields(object, fields) {
object = pick(object, fields);
fields.forEach(f => {
if (isUndefined(object[f])) {
object[f] = null;
}
if (isDate(object[f])) {
object[f] = object[f].getTime() / 1000;
}
});
return object;
},
async keyLookup(ipfsNode, kname, pass) {
const pem = await ipfsNode.key.export(kname, pass);
return libp2pCrypto.keys.import(pem, pass);
},
async getIpfsHashFromString(string) {
return ipfsHelper.cidHashFromBytes(new (TextEncoder as any)('utf8').encode(string), 0x55);
},
async cidHashFromBytes(bytes, code) {
// 0x55 - raw ipfs hash
// 0x72 - pubkey
// https://github.com/multiformats/multicodec/blob/5de6f09bdf7ed137f47c94a2e61866a87b4b3141/table.csv
const res = (sha256.digest(bytes) as any);
return CID.createV1(code, res).toString();
},
async getJsonHashFromObject(object) {
object = common.sortObject(object);
const buf = jsonCodec.encode(object);
const hash = await sha256.digest(buf);
const cid = CID.createV1(jsonCodec.code, hash);
return ipfsHelper.cidToHash(cid);
},
async getIpldHashFromObject(object) {
object = common.sortObject(object);
const buf = codec.encode(object);
const hash = await sha256.digest(buf);
const cid = CID.createV1(codec.code, hash);
return ipfsHelper.cidToHash(cid);
},
async createDaemonNode(options = {}, ipfsOptions = {}) {
// const hat = require('hat');
// const {createFactory} = require('ipfsd-ctl');
//
// const factory = createFactory({
// type: 'proc', // or 'js' to run in a separate process
// // type: 'js',
// ipfsHttpModule: require('ipfs-http-client'),
// ipfsModule: require('ipfs'), // only if you gonna spawn 'proc' controllers
// ...options
// })
//
// const node = await factory.spawn({
// ipfsOptions: {
// pass: hat(),
// init: true,
// // start: true,
// ...ipfsOptions
// },
// // preload: {enabled: false, addresses: await this.getPreloadAddresses()}
// });
//
// return node.api;
},
getStorageIdHash(storageId) {
if (ipfsHelper.isCid(storageId)) {
storageId = ipfsHelper.cidToHash(storageId);
}
if (storageId['/']) {
storageId = storageId['/'];
}
return storageId;
}
};
export default ipfsHelper;