-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
138 lines (84 loc) · 3.35 KB
/
index.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
136
137
import Verify from './modules/verify';
import Sign from './modules/sign';
import CodenotaryFoundationClient from "./lib/codenotaryFoundation";
import CodenotaryBlockchainClient from "./lib/codenotaryBlockchain";
import CodenotaryApiClient from "./lib/codenotaryApi";
import { ASSET_URL, BLOCKCHAIN_URL, BLOCKCHAIN_ASSET_ADDRESS, BLOCKCHAIN_ORG_ADDRESS, API_URL } from './config'
import { isValidLocalPath } from './utils/misc'
class Jsvcn {
constructor(options) {
const config = options || {}
const mode = config.mode || 'api'
const credentials = config.credentials || {}
const assetUrl = config.assetUrl || ASSET_URL;
const apiUrl = config.apiUrl || API_URL;
const blockchainUrl = config.blockchainUrl || BLOCKCHAIN_URL;
const blockchainAssetAddress = config.blockchainAssetAddress || BLOCKCHAIN_ASSET_ADDRESS;
const blockchainOrganizationAddress = config.blockchainOrganizationAddress || BLOCKCHAIN_ORG_ADDRESS;
this.checksums = config.checksums || []
this.validationOnly = !!config.validationOnly
// init clients
this.clientService = (mode === 'blockchain') ? ({
type: 'blockchain',
service: {
blockchainService: new CodenotaryBlockchainClient(blockchainUrl, blockchainAssetAddress, blockchainOrganizationAddress),
assetService: new CodenotaryFoundationClient(assetUrl)
}
}) : ({
type: 'api',
service: new CodenotaryApiClient(apiUrl, credentials)
});
}
verify(input, onProgress, against) {
const { checksums, validationOnly } = this
const organization = typeof against === "string" ? against : undefined
const signers = Array.isArray(against) ? against : undefined
const verify = new Verify(this.clientService, { organization, signers, checksums, validationOnly })
if (input instanceof File) {
return verify.file(input, onProgress)
} else if (isValidLocalPath(input)) {
return verify.url(input)
} else if (typeof input === "string") {
return verify.hash(input)
} else {
throw new Error("Invalid frist argument, please provide hash, file or local file url")
}
}
sign(input, signData, onProgress) {
const sign = new Sign(this.clientService)
if (input instanceof File) {
return sign.file(input, onProgress, "SIGN", signData)
} else if (isValidLocalPath(input)) {
return sign.url(input, "SIGN", signData)
} else if (typeof input === "string") {
return sign.hash(input, "SIGN", signData)
} else {
throw new Error("Invalid frist argument, please provide hash, file or local file url")
}
}
untrust(input, onProgress) {
const sign = new Sign(this.clientService)
if (input instanceof File) {
return sign.file(input, onProgress, "UNTRUST")
} else if (isValidLocalPath(input)) {
return sign.url(input, "UNTRUST")
} else if (typeof input === "string") {
return sign.hash(input, "UNTRUST")
} else {
throw new Error("Invalid frist argument, please provide hash, file or local file url")
}
}
unsupport(input, onProgress) {
const sign = new Sign(this.clientService)
if (input instanceof File) {
return sign.file(input, onProgress, "UNSUPPORT")
} else if (isValidLocalPath(input)) {
return sign.url(input, "UNSUPPORT")
} else if (typeof input === "string") {
return sign.hash(input, "UNSUPPORT")
} else {
throw new Error("Invalid frist argument, please provide hash, file or local file url")
}
}
}
export default Jsvcn;