@nuggetslife/vc 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.yarnrc.yml +3 -0
- package/Cargo.toml +24 -0
- package/README.md +9 -0
- package/build.rs +5 -0
- package/index.d.ts +28 -0
- package/index.js +315 -0
- package/package.json +71 -0
- package/src/lib.rs +146 -0
- package/test.mjs +46 -0
package/.yarnrc.yml
ADDED
package/Cargo.toml
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
edition = "2021"
|
|
3
|
+
name = "nuggetslife_vc"
|
|
4
|
+
version = "0.0.0"
|
|
5
|
+
|
|
6
|
+
[lib]
|
|
7
|
+
crate-type = ["cdylib"]
|
|
8
|
+
|
|
9
|
+
[dependencies]
|
|
10
|
+
base64 = "0.22.1"
|
|
11
|
+
bs58 = "0.5.1"
|
|
12
|
+
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
|
|
13
|
+
napi = { version = "2.12.2", default-features = false, features = ["napi4", "tokio_rt"] }
|
|
14
|
+
napi-derive = "2.12.2"
|
|
15
|
+
serde = { version = "1.0.203", features = ["derive"] }
|
|
16
|
+
serde_json = "1.0.117"
|
|
17
|
+
vc = {path = "../rs"}
|
|
18
|
+
|
|
19
|
+
[build-dependencies]
|
|
20
|
+
napi-build = "2.0.1"
|
|
21
|
+
|
|
22
|
+
[profile.release]
|
|
23
|
+
lto = true
|
|
24
|
+
strip = "symbols"
|
package/README.md
ADDED
package/build.rs
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
/* auto-generated by NAPI-RS */
|
|
5
|
+
|
|
6
|
+
export interface KeyPairOptions {
|
|
7
|
+
id?: string
|
|
8
|
+
controller?: string
|
|
9
|
+
privateKeyBase58?: string
|
|
10
|
+
publicKeyBase58?: string
|
|
11
|
+
}
|
|
12
|
+
export interface GenerateKeyPairOptions {
|
|
13
|
+
id?: string
|
|
14
|
+
controller?: string
|
|
15
|
+
seed?: Buffer
|
|
16
|
+
}
|
|
17
|
+
export class Bls12381G2KeyPair {
|
|
18
|
+
id?: string
|
|
19
|
+
controller?: string
|
|
20
|
+
privateKeyBuffer?: Array<number>
|
|
21
|
+
publicKeyBuffer?: Array<number>
|
|
22
|
+
type: string
|
|
23
|
+
constructor(options?: KeyPairOptions | undefined | null)
|
|
24
|
+
static generate(options?: GenerateKeyPairOptions | undefined | null): Promise<Bls12381G2KeyPair>
|
|
25
|
+
static from(options: KeyPairOptions): Promise<Bls12381G2KeyPair>
|
|
26
|
+
get publicKey(): string | null
|
|
27
|
+
get privateKey(): string | null
|
|
28
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/* prettier-ignore */
|
|
4
|
+
|
|
5
|
+
/* auto-generated by NAPI-RS */
|
|
6
|
+
|
|
7
|
+
const { existsSync, readFileSync } = require('fs')
|
|
8
|
+
const { join } = require('path')
|
|
9
|
+
|
|
10
|
+
const { platform, arch } = process
|
|
11
|
+
|
|
12
|
+
let nativeBinding = null
|
|
13
|
+
let localFileExisted = false
|
|
14
|
+
let loadError = null
|
|
15
|
+
|
|
16
|
+
function isMusl() {
|
|
17
|
+
// For Node 10
|
|
18
|
+
if (!process.report || typeof process.report.getReport !== 'function') {
|
|
19
|
+
try {
|
|
20
|
+
const lddPath = require('child_process').execSync('which ldd').toString().trim()
|
|
21
|
+
return readFileSync(lddPath, 'utf8').includes('musl')
|
|
22
|
+
} catch (e) {
|
|
23
|
+
return true
|
|
24
|
+
}
|
|
25
|
+
} else {
|
|
26
|
+
const { glibcVersionRuntime } = process.report.getReport().header
|
|
27
|
+
return !glibcVersionRuntime
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
switch (platform) {
|
|
32
|
+
case 'android':
|
|
33
|
+
switch (arch) {
|
|
34
|
+
case 'arm64':
|
|
35
|
+
localFileExisted = existsSync(join(__dirname, 'vc.android-arm64.node'))
|
|
36
|
+
try {
|
|
37
|
+
if (localFileExisted) {
|
|
38
|
+
nativeBinding = require('./vc.android-arm64.node')
|
|
39
|
+
} else {
|
|
40
|
+
nativeBinding = require('@nuggetslife/vc-android-arm64')
|
|
41
|
+
}
|
|
42
|
+
} catch (e) {
|
|
43
|
+
loadError = e
|
|
44
|
+
}
|
|
45
|
+
break
|
|
46
|
+
case 'arm':
|
|
47
|
+
localFileExisted = existsSync(join(__dirname, 'vc.android-arm-eabi.node'))
|
|
48
|
+
try {
|
|
49
|
+
if (localFileExisted) {
|
|
50
|
+
nativeBinding = require('./vc.android-arm-eabi.node')
|
|
51
|
+
} else {
|
|
52
|
+
nativeBinding = require('@nuggetslife/vc-android-arm-eabi')
|
|
53
|
+
}
|
|
54
|
+
} catch (e) {
|
|
55
|
+
loadError = e
|
|
56
|
+
}
|
|
57
|
+
break
|
|
58
|
+
default:
|
|
59
|
+
throw new Error(`Unsupported architecture on Android ${arch}`)
|
|
60
|
+
}
|
|
61
|
+
break
|
|
62
|
+
case 'win32':
|
|
63
|
+
switch (arch) {
|
|
64
|
+
case 'x64':
|
|
65
|
+
localFileExisted = existsSync(
|
|
66
|
+
join(__dirname, 'vc.win32-x64-msvc.node')
|
|
67
|
+
)
|
|
68
|
+
try {
|
|
69
|
+
if (localFileExisted) {
|
|
70
|
+
nativeBinding = require('./vc.win32-x64-msvc.node')
|
|
71
|
+
} else {
|
|
72
|
+
nativeBinding = require('@nuggetslife/vc-win32-x64-msvc')
|
|
73
|
+
}
|
|
74
|
+
} catch (e) {
|
|
75
|
+
loadError = e
|
|
76
|
+
}
|
|
77
|
+
break
|
|
78
|
+
case 'ia32':
|
|
79
|
+
localFileExisted = existsSync(
|
|
80
|
+
join(__dirname, 'vc.win32-ia32-msvc.node')
|
|
81
|
+
)
|
|
82
|
+
try {
|
|
83
|
+
if (localFileExisted) {
|
|
84
|
+
nativeBinding = require('./vc.win32-ia32-msvc.node')
|
|
85
|
+
} else {
|
|
86
|
+
nativeBinding = require('@nuggetslife/vc-win32-ia32-msvc')
|
|
87
|
+
}
|
|
88
|
+
} catch (e) {
|
|
89
|
+
loadError = e
|
|
90
|
+
}
|
|
91
|
+
break
|
|
92
|
+
case 'arm64':
|
|
93
|
+
localFileExisted = existsSync(
|
|
94
|
+
join(__dirname, 'vc.win32-arm64-msvc.node')
|
|
95
|
+
)
|
|
96
|
+
try {
|
|
97
|
+
if (localFileExisted) {
|
|
98
|
+
nativeBinding = require('./vc.win32-arm64-msvc.node')
|
|
99
|
+
} else {
|
|
100
|
+
nativeBinding = require('@nuggetslife/vc-win32-arm64-msvc')
|
|
101
|
+
}
|
|
102
|
+
} catch (e) {
|
|
103
|
+
loadError = e
|
|
104
|
+
}
|
|
105
|
+
break
|
|
106
|
+
default:
|
|
107
|
+
throw new Error(`Unsupported architecture on Windows: ${arch}`)
|
|
108
|
+
}
|
|
109
|
+
break
|
|
110
|
+
case 'darwin':
|
|
111
|
+
localFileExisted = existsSync(join(__dirname, 'vc.darwin-universal.node'))
|
|
112
|
+
try {
|
|
113
|
+
if (localFileExisted) {
|
|
114
|
+
nativeBinding = require('./vc.darwin-universal.node')
|
|
115
|
+
} else {
|
|
116
|
+
nativeBinding = require('@nuggetslife/vc-darwin-universal')
|
|
117
|
+
}
|
|
118
|
+
break
|
|
119
|
+
} catch {}
|
|
120
|
+
switch (arch) {
|
|
121
|
+
case 'x64':
|
|
122
|
+
localFileExisted = existsSync(join(__dirname, 'vc.darwin-x64.node'))
|
|
123
|
+
try {
|
|
124
|
+
if (localFileExisted) {
|
|
125
|
+
nativeBinding = require('./vc.darwin-x64.node')
|
|
126
|
+
} else {
|
|
127
|
+
nativeBinding = require('@nuggetslife/vc-darwin-x64')
|
|
128
|
+
}
|
|
129
|
+
} catch (e) {
|
|
130
|
+
loadError = e
|
|
131
|
+
}
|
|
132
|
+
break
|
|
133
|
+
case 'arm64':
|
|
134
|
+
localFileExisted = existsSync(
|
|
135
|
+
join(__dirname, 'vc.darwin-arm64.node')
|
|
136
|
+
)
|
|
137
|
+
try {
|
|
138
|
+
if (localFileExisted) {
|
|
139
|
+
nativeBinding = require('./vc.darwin-arm64.node')
|
|
140
|
+
} else {
|
|
141
|
+
nativeBinding = require('@nuggetslife/vc-darwin-arm64')
|
|
142
|
+
}
|
|
143
|
+
} catch (e) {
|
|
144
|
+
loadError = e
|
|
145
|
+
}
|
|
146
|
+
break
|
|
147
|
+
default:
|
|
148
|
+
throw new Error(`Unsupported architecture on macOS: ${arch}`)
|
|
149
|
+
}
|
|
150
|
+
break
|
|
151
|
+
case 'freebsd':
|
|
152
|
+
if (arch !== 'x64') {
|
|
153
|
+
throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
|
|
154
|
+
}
|
|
155
|
+
localFileExisted = existsSync(join(__dirname, 'vc.freebsd-x64.node'))
|
|
156
|
+
try {
|
|
157
|
+
if (localFileExisted) {
|
|
158
|
+
nativeBinding = require('./vc.freebsd-x64.node')
|
|
159
|
+
} else {
|
|
160
|
+
nativeBinding = require('@nuggetslife/vc-freebsd-x64')
|
|
161
|
+
}
|
|
162
|
+
} catch (e) {
|
|
163
|
+
loadError = e
|
|
164
|
+
}
|
|
165
|
+
break
|
|
166
|
+
case 'linux':
|
|
167
|
+
switch (arch) {
|
|
168
|
+
case 'x64':
|
|
169
|
+
if (isMusl()) {
|
|
170
|
+
localFileExisted = existsSync(
|
|
171
|
+
join(__dirname, 'vc.linux-x64-musl.node')
|
|
172
|
+
)
|
|
173
|
+
try {
|
|
174
|
+
if (localFileExisted) {
|
|
175
|
+
nativeBinding = require('./vc.linux-x64-musl.node')
|
|
176
|
+
} else {
|
|
177
|
+
nativeBinding = require('@nuggetslife/vc-linux-x64-musl')
|
|
178
|
+
}
|
|
179
|
+
} catch (e) {
|
|
180
|
+
loadError = e
|
|
181
|
+
}
|
|
182
|
+
} else {
|
|
183
|
+
localFileExisted = existsSync(
|
|
184
|
+
join(__dirname, 'vc.linux-x64-gnu.node')
|
|
185
|
+
)
|
|
186
|
+
try {
|
|
187
|
+
if (localFileExisted) {
|
|
188
|
+
nativeBinding = require('./vc.linux-x64-gnu.node')
|
|
189
|
+
} else {
|
|
190
|
+
nativeBinding = require('@nuggetslife/vc-linux-x64-gnu')
|
|
191
|
+
}
|
|
192
|
+
} catch (e) {
|
|
193
|
+
loadError = e
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
break
|
|
197
|
+
case 'arm64':
|
|
198
|
+
if (isMusl()) {
|
|
199
|
+
localFileExisted = existsSync(
|
|
200
|
+
join(__dirname, 'vc.linux-arm64-musl.node')
|
|
201
|
+
)
|
|
202
|
+
try {
|
|
203
|
+
if (localFileExisted) {
|
|
204
|
+
nativeBinding = require('./vc.linux-arm64-musl.node')
|
|
205
|
+
} else {
|
|
206
|
+
nativeBinding = require('@nuggetslife/vc-linux-arm64-musl')
|
|
207
|
+
}
|
|
208
|
+
} catch (e) {
|
|
209
|
+
loadError = e
|
|
210
|
+
}
|
|
211
|
+
} else {
|
|
212
|
+
localFileExisted = existsSync(
|
|
213
|
+
join(__dirname, 'vc.linux-arm64-gnu.node')
|
|
214
|
+
)
|
|
215
|
+
try {
|
|
216
|
+
if (localFileExisted) {
|
|
217
|
+
nativeBinding = require('./vc.linux-arm64-gnu.node')
|
|
218
|
+
} else {
|
|
219
|
+
nativeBinding = require('@nuggetslife/vc-linux-arm64-gnu')
|
|
220
|
+
}
|
|
221
|
+
} catch (e) {
|
|
222
|
+
loadError = e
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
break
|
|
226
|
+
case 'arm':
|
|
227
|
+
if (isMusl()) {
|
|
228
|
+
localFileExisted = existsSync(
|
|
229
|
+
join(__dirname, 'vc.linux-arm-musleabihf.node')
|
|
230
|
+
)
|
|
231
|
+
try {
|
|
232
|
+
if (localFileExisted) {
|
|
233
|
+
nativeBinding = require('./vc.linux-arm-musleabihf.node')
|
|
234
|
+
} else {
|
|
235
|
+
nativeBinding = require('@nuggetslife/vc-linux-arm-musleabihf')
|
|
236
|
+
}
|
|
237
|
+
} catch (e) {
|
|
238
|
+
loadError = e
|
|
239
|
+
}
|
|
240
|
+
} else {
|
|
241
|
+
localFileExisted = existsSync(
|
|
242
|
+
join(__dirname, 'vc.linux-arm-gnueabihf.node')
|
|
243
|
+
)
|
|
244
|
+
try {
|
|
245
|
+
if (localFileExisted) {
|
|
246
|
+
nativeBinding = require('./vc.linux-arm-gnueabihf.node')
|
|
247
|
+
} else {
|
|
248
|
+
nativeBinding = require('@nuggetslife/vc-linux-arm-gnueabihf')
|
|
249
|
+
}
|
|
250
|
+
} catch (e) {
|
|
251
|
+
loadError = e
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
break
|
|
255
|
+
case 'riscv64':
|
|
256
|
+
if (isMusl()) {
|
|
257
|
+
localFileExisted = existsSync(
|
|
258
|
+
join(__dirname, 'vc.linux-riscv64-musl.node')
|
|
259
|
+
)
|
|
260
|
+
try {
|
|
261
|
+
if (localFileExisted) {
|
|
262
|
+
nativeBinding = require('./vc.linux-riscv64-musl.node')
|
|
263
|
+
} else {
|
|
264
|
+
nativeBinding = require('@nuggetslife/vc-linux-riscv64-musl')
|
|
265
|
+
}
|
|
266
|
+
} catch (e) {
|
|
267
|
+
loadError = e
|
|
268
|
+
}
|
|
269
|
+
} else {
|
|
270
|
+
localFileExisted = existsSync(
|
|
271
|
+
join(__dirname, 'vc.linux-riscv64-gnu.node')
|
|
272
|
+
)
|
|
273
|
+
try {
|
|
274
|
+
if (localFileExisted) {
|
|
275
|
+
nativeBinding = require('./vc.linux-riscv64-gnu.node')
|
|
276
|
+
} else {
|
|
277
|
+
nativeBinding = require('@nuggetslife/vc-linux-riscv64-gnu')
|
|
278
|
+
}
|
|
279
|
+
} catch (e) {
|
|
280
|
+
loadError = e
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
break
|
|
284
|
+
case 's390x':
|
|
285
|
+
localFileExisted = existsSync(
|
|
286
|
+
join(__dirname, 'vc.linux-s390x-gnu.node')
|
|
287
|
+
)
|
|
288
|
+
try {
|
|
289
|
+
if (localFileExisted) {
|
|
290
|
+
nativeBinding = require('./vc.linux-s390x-gnu.node')
|
|
291
|
+
} else {
|
|
292
|
+
nativeBinding = require('@nuggetslife/vc-linux-s390x-gnu')
|
|
293
|
+
}
|
|
294
|
+
} catch (e) {
|
|
295
|
+
loadError = e
|
|
296
|
+
}
|
|
297
|
+
break
|
|
298
|
+
default:
|
|
299
|
+
throw new Error(`Unsupported architecture on Linux: ${arch}`)
|
|
300
|
+
}
|
|
301
|
+
break
|
|
302
|
+
default:
|
|
303
|
+
throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (!nativeBinding) {
|
|
307
|
+
if (loadError) {
|
|
308
|
+
throw loadError
|
|
309
|
+
}
|
|
310
|
+
throw new Error(`Failed to load native binding`)
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const { Bls12381G2KeyPair } = nativeBinding
|
|
314
|
+
|
|
315
|
+
module.exports.Bls12381G2KeyPair = Bls12381G2KeyPair
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nuggetslife/vc",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"types": "index.d.ts",
|
|
6
|
+
"napi": {
|
|
7
|
+
"name": "vc",
|
|
8
|
+
"triples": {
|
|
9
|
+
"additional": [
|
|
10
|
+
"aarch64-apple-darwin",
|
|
11
|
+
"aarch64-linux-android",
|
|
12
|
+
"aarch64-unknown-linux-gnu",
|
|
13
|
+
"aarch64-unknown-linux-musl",
|
|
14
|
+
"aarch64-pc-windows-msvc",
|
|
15
|
+
"armv7-unknown-linux-gnueabihf",
|
|
16
|
+
"armv7-unknown-linux-musleabihf",
|
|
17
|
+
"x86_64-unknown-linux-musl",
|
|
18
|
+
"x86_64-unknown-freebsd",
|
|
19
|
+
"i686-pc-windows-msvc",
|
|
20
|
+
"armv7-linux-androideabi",
|
|
21
|
+
"universal-apple-darwin",
|
|
22
|
+
"riscv64gc-unknown-linux-gnu"
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@napi-rs/cli": "^2.18.3",
|
|
29
|
+
"ava": "^6.0.1"
|
|
30
|
+
},
|
|
31
|
+
"ava": {
|
|
32
|
+
"timeout": "3m"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">= 10"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"artifacts": "napi artifacts",
|
|
39
|
+
"build": "napi build --platform --release",
|
|
40
|
+
"build:debug": "napi build --platform",
|
|
41
|
+
"prepublishOnly": "napi prepublish -t npm",
|
|
42
|
+
"test": "ava",
|
|
43
|
+
"universal": "napi universal",
|
|
44
|
+
"version": "napi version"
|
|
45
|
+
},
|
|
46
|
+
"packageManager": "yarn@4.3.1",
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@mattrglobal/bls12381-key-pair": "^1.2.1",
|
|
49
|
+
"@nuggetslife/ffi-bbs-signatures": "^1.3.6",
|
|
50
|
+
"@types/node": "^20.14.9",
|
|
51
|
+
"bs58": "^6.0.0"
|
|
52
|
+
},
|
|
53
|
+
"optionalDependencies": {
|
|
54
|
+
"@nuggetslife/vc-win32-x64-msvc": "0.0.1",
|
|
55
|
+
"@nuggetslife/vc-darwin-x64": "0.0.1",
|
|
56
|
+
"@nuggetslife/vc-linux-x64-gnu": "0.0.1",
|
|
57
|
+
"@nuggetslife/vc-darwin-arm64": "0.0.1",
|
|
58
|
+
"@nuggetslife/vc-android-arm64": "0.0.1",
|
|
59
|
+
"@nuggetslife/vc-linux-arm64-gnu": "0.0.1",
|
|
60
|
+
"@nuggetslife/vc-linux-arm64-musl": "0.0.1",
|
|
61
|
+
"@nuggetslife/vc-win32-arm64-msvc": "0.0.1",
|
|
62
|
+
"@nuggetslife/vc-linux-arm-gnueabihf": "0.0.1",
|
|
63
|
+
"@nuggetslife/vc-linux-arm-musleabihf": "0.0.1",
|
|
64
|
+
"@nuggetslife/vc-linux-x64-musl": "0.0.1",
|
|
65
|
+
"@nuggetslife/vc-freebsd-x64": "0.0.1",
|
|
66
|
+
"@nuggetslife/vc-win32-ia32-msvc": "0.0.1",
|
|
67
|
+
"@nuggetslife/vc-android-arm-eabi": "0.0.1",
|
|
68
|
+
"@nuggetslife/vc-darwin-universal": "0.0.1",
|
|
69
|
+
"@nuggetslife/vc-linux-riscv64-gnu": "0.0.1"
|
|
70
|
+
}
|
|
71
|
+
}
|
package/src/lib.rs
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
#![deny(clippy::all)]
|
|
2
|
+
|
|
3
|
+
#[macro_use]
|
|
4
|
+
extern crate napi_derive;
|
|
5
|
+
|
|
6
|
+
// use base64::{engine::general_purpose::STANDARD, Engine as _};
|
|
7
|
+
use napi::bindgen_prelude::*;
|
|
8
|
+
use vc::bbs_signatures::bls12381::bls_generate_g2_key;
|
|
9
|
+
|
|
10
|
+
#[napi]
|
|
11
|
+
pub struct Bls12381G2KeyPair {
|
|
12
|
+
pub id: Option<String>,
|
|
13
|
+
pub controller: Option<String>,
|
|
14
|
+
pub private_key_buffer: Option<Vec<u8>>,
|
|
15
|
+
pub public_key_buffer: Option<Vec<u8>>,
|
|
16
|
+
#[napi(js_name = "type")]
|
|
17
|
+
pub type_: String,
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
#[napi(object)]
|
|
21
|
+
pub struct KeyPairOptions {
|
|
22
|
+
pub id: Option<String>,
|
|
23
|
+
pub controller: Option<String>,
|
|
24
|
+
pub private_key_base58: Option<String>,
|
|
25
|
+
pub public_key_base58: Option<String>,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
#[napi(object)]
|
|
29
|
+
pub struct GenerateKeyPairOptions {
|
|
30
|
+
pub id: Option<String>,
|
|
31
|
+
pub controller: Option<String>,
|
|
32
|
+
pub seed: Option<Buffer>,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
#[napi]
|
|
36
|
+
impl Bls12381G2KeyPair {
|
|
37
|
+
#[napi(constructor)]
|
|
38
|
+
pub async fn new(options: Option<KeyPairOptions>) -> Self {
|
|
39
|
+
match options {
|
|
40
|
+
Some(o) => {
|
|
41
|
+
// The provided publicKey needs to be 384 bits / 5.85 = 65.6
|
|
42
|
+
// which means the base58 encoded publicKey can be either 65 or 66 chars
|
|
43
|
+
// 5.85 = log base 2 (58) which is equivalent to the number of bits
|
|
44
|
+
// encoded per character of a base58 encoded string.
|
|
45
|
+
if let Some(ref pubkey_bs58) = o.public_key_base58 {
|
|
46
|
+
if pubkey_bs58.len() != 131 && pubkey_bs58.len() != 132 {
|
|
47
|
+
panic!(
|
|
48
|
+
"public_key_base58 invalid length. expected 131 or 132. got {}",
|
|
49
|
+
pubkey_bs58.len()
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// Validates the size of the private key if one is included
|
|
55
|
+
// This is done by 256 bits / 5.85 = 43.7 which means
|
|
56
|
+
// the base58 encoded privateKey can be either 43 or 44 chars
|
|
57
|
+
if let Some(ref privkey_bs58) = o.private_key_base58 {
|
|
58
|
+
if privkey_bs58.len() != 43 && privkey_bs58.len() != 44 {
|
|
59
|
+
panic!(
|
|
60
|
+
"private_key_base58 invalid length. expected 43 or 44. got {}",
|
|
61
|
+
privkey_bs58.len()
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
let private_key_buffer = o
|
|
67
|
+
.private_key_base58
|
|
68
|
+
.map(|v| bs58::decode(v).into_vec().unwrap());
|
|
69
|
+
let public_key_buffer = o
|
|
70
|
+
.public_key_base58
|
|
71
|
+
.map(|v| bs58::decode(v).into_vec().unwrap());
|
|
72
|
+
|
|
73
|
+
Self {
|
|
74
|
+
id: o.id,
|
|
75
|
+
controller: o.controller,
|
|
76
|
+
private_key_buffer,
|
|
77
|
+
public_key_buffer,
|
|
78
|
+
type_: String::from("Bls12381G2Key2020"),
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
None => Self {
|
|
82
|
+
id: None,
|
|
83
|
+
controller: None,
|
|
84
|
+
private_key_buffer: None,
|
|
85
|
+
public_key_buffer: None,
|
|
86
|
+
type_: String::from("Bls12381G2Key2020"),
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
#[napi(factory)]
|
|
92
|
+
pub async fn generate(options: Option<GenerateKeyPairOptions>) -> Self {
|
|
93
|
+
match options {
|
|
94
|
+
Some(o) => {
|
|
95
|
+
let seed: Option<Vec<u8>> = o.seed.map(|s| s.into());
|
|
96
|
+
let kp = bls_generate_g2_key(seed).await;
|
|
97
|
+
let public_key_base58 = kp.public_key.map(|v| bs58::encode(v).into_string());
|
|
98
|
+
let private_key_base58 = kp.secret_key.map(|v| bs58::encode(v).into_string());
|
|
99
|
+
|
|
100
|
+
Self::new(Some(KeyPairOptions {
|
|
101
|
+
id: o.id,
|
|
102
|
+
controller: o.controller,
|
|
103
|
+
private_key_base58,
|
|
104
|
+
public_key_base58,
|
|
105
|
+
}))
|
|
106
|
+
.await
|
|
107
|
+
}
|
|
108
|
+
None => {
|
|
109
|
+
let kp = bls_generate_g2_key(None).await;
|
|
110
|
+
let public_key_base58 = kp.public_key.map(|v| bs58::encode(v).into_string());
|
|
111
|
+
let private_key_base58 = kp.secret_key.map(|v| bs58::encode(v).into_string());
|
|
112
|
+
|
|
113
|
+
Self::new(Some(KeyPairOptions {
|
|
114
|
+
id: None,
|
|
115
|
+
controller: None,
|
|
116
|
+
private_key_base58,
|
|
117
|
+
public_key_base58,
|
|
118
|
+
}))
|
|
119
|
+
.await
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
#[napi(factory)]
|
|
125
|
+
pub async fn from(options: KeyPairOptions) -> Self {
|
|
126
|
+
Bls12381G2KeyPair::new(Some(options)).await
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
#[napi(getter)]
|
|
130
|
+
pub fn public_key(&self) -> Option<String> {
|
|
131
|
+
self
|
|
132
|
+
.public_key_buffer
|
|
133
|
+
.clone()
|
|
134
|
+
.map(|b| bs58::encode(b).into_string())
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
#[napi(getter)]
|
|
138
|
+
pub fn private_key(&self) -> Option<String> {
|
|
139
|
+
self
|
|
140
|
+
.private_key_buffer
|
|
141
|
+
.clone()
|
|
142
|
+
.map(|b| bs58::encode(b).into_string())
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
pub fn test() {}
|
package/test.mjs
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import test from 'node:test';
|
|
2
|
+
import assert from 'node:assert';
|
|
3
|
+
import { Bls12381G2KeyPair } from '@mattrglobal/bls12381-key-pair';
|
|
4
|
+
import { Bls12381G2KeyPair as NewKeyPairClass } from './index.js'
|
|
5
|
+
import bs58 from 'bs58'
|
|
6
|
+
import bbs from '@nuggetslife/ffi-bbs-signatures'
|
|
7
|
+
|
|
8
|
+
const seed = Buffer.alloc(32, 0)
|
|
9
|
+
const address = '0x581510277Bc56802dE75BA021b66873437e0169f'
|
|
10
|
+
const addressBase58 = bs58.encode(Buffer.from(address.slice(2), 'hex'))
|
|
11
|
+
|
|
12
|
+
test('gen keypair for same seed', async () => {
|
|
13
|
+
const mattr = await Bls12381G2KeyPair.generate({ seed, });
|
|
14
|
+
const harry = await NewKeyPairClass.generate({ seed })
|
|
15
|
+
const ffi = await bbs.generateBls12381G2KeyPair(seed)
|
|
16
|
+
const ffi_sec_key = bs58.encode(Buffer.from(ffi.secretKey))
|
|
17
|
+
const ffi_pub_key = bs58.encode(Buffer.from(ffi.publicKey))
|
|
18
|
+
|
|
19
|
+
// private keys match
|
|
20
|
+
assert.equal(mattr.privateKey, harry.privateKey)
|
|
21
|
+
assert.equal(ffi_sec_key, harry.privateKey)
|
|
22
|
+
|
|
23
|
+
// public keys match
|
|
24
|
+
assert.equal(mattr.publicKey, harry.publicKey)
|
|
25
|
+
assert.equal(ffi_pub_key, harry.publicKey)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
test('keypair from keypair', async () => {
|
|
29
|
+
const mattr = await Bls12381G2KeyPair.generate({
|
|
30
|
+
id: `did:nuggets:${addressBase58}#bls12381g2`,
|
|
31
|
+
controller: `did:nuggets:${addressBase58}`,
|
|
32
|
+
seed,
|
|
33
|
+
});
|
|
34
|
+
const harry = await NewKeyPairClass.from({
|
|
35
|
+
id: mattr.id,
|
|
36
|
+
controller: mattr.controller,
|
|
37
|
+
publicKeyBase58: mattr.publicKey,
|
|
38
|
+
privateKeyBase58: mattr.privateKey,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
assert.equal(mattr.id, harry.id)
|
|
42
|
+
assert.equal(mattr.controller, harry.controller)
|
|
43
|
+
assert.equal(mattr.privateKey, harry.privateKey)
|
|
44
|
+
assert.equal(mattr.publicKey, harry.publicKey)
|
|
45
|
+
})
|
|
46
|
+
|