-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
106 lines (95 loc) · 3.02 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
// import * as ed from '@noble/ed25519'
// import base64Pad from '@nichoth/base64pad'
// import { fromString } from 'uint8arrays/from-string'
import { webcrypto } from "one-webcrypto"
import * as utils from 'keystore-idb/lib/utils.js'
export function create () {
const uses = ['sign', 'verify']
return webcrypto.subtle.generateKey({
name: 'ECDSA',
namedCurve: 'P-256'
}, true, uses)
.then(keys => {
return exportKeys(keys)
})
}
function exportKeys (keypair) {
return Promise.all([
webcrypto.subtle.exportKey('raw', keypair.publicKey),
webcrypto.subtle.exportKey('pkcs8', keypair.privateKey)
])
.then(([pub, priv]) => {
return {
publicKey: utils.arrBufToBase64(pub),
privateKey: utils.arrBufToBase64(priv)
}
})
}
export function importKeys (keypair) {
return Promise.all([
webcrypto.subtle.importKey(
'raw',
utils.base64ToArrBuf(keypair.publicKey),
{ name: 'ECDSA', namedCurve: 'P-256' },
true,
['verify']
),
webcrypto.subtle.importKey(
'pkcs8',
utils.base64ToArrBuf(keypair.privateKey),
// buf,
{ name: 'ECDSA', namedCurve: 'P-256' },
true,
['sign']
)
])
.then(([pub, priv]) => {
return { publicKey: pub, privateKey: priv }
})
}
// // export function create () {
// // const privateKeyRaw = ed.utils.randomPrivateKey()
// // return ed.getPublicKey(privateKeyRaw).then(pubKey => {
// // return {
// // privateKey: privateKeyRaw,
// // publicKey: pubKey
// // }
// // })
// // }
// export function exportPrivateKey (kp) {
// return base64Pad.encode(kp.privateKey)
// }
// /**
// * Create a signature of `msg` using the private signing key.
// *
// * @param {String} msg - a message to sign
// * @returns {Promise<base64 string>} a Promise that resolves to the signature
// * (as a base64 string)
// */
// export function sign (msg, privateKey) {
// return ed.sign(fromString(msg), privateKey)
// .then(sig => {
// // sig here is a uint8array
// return base64Pad.encode(sig)
// })
// }
// // sig -- base64 string
// // msg -- string
// // pubKey -- uint8array
// export function verify (sig, msg, pubKey) {
// const _sig = base64Pad.decode(sig)
// return ed.verify(_sig, fromString(msg), pubKey)
// }
// // /**
// // * Create a KeyPair from exported private key.
// // *
// // * @param {string} key - a private key, as encoded by `exportPrivateKey`
// // * @returns {Promise<KeyPair>} a Promise that resolves to the loaded KeyPair object
// // */
// // export function fromExportedKey (key) {
// // const privateKey = base64Pad.decode(key)
// // return ed.getPublicKey(privateKey)
// // .then(publicKey => {
// // return { privateKey, publicKey }
// // })
// // }