@peerbit/crypto 1.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/LICENSE +202 -0
- package/README.md +3 -0
- package/lib/esm/bytes.d.ts +5 -0
- package/lib/esm/bytes.js +15 -0
- package/lib/esm/bytes.js.map +1 -0
- package/lib/esm/ed25519-sign-browser.d.ts +5 -0
- package/lib/esm/ed25519-sign-browser.js +27 -0
- package/lib/esm/ed25519-sign-browser.js.map +1 -0
- package/lib/esm/ed25519-sign.d.ts +5 -0
- package/lib/esm/ed25519-sign.js +64 -0
- package/lib/esm/ed25519-sign.js.map +1 -0
- package/lib/esm/ed25519.d.ts +41 -0
- package/lib/esm/ed25519.js +181 -0
- package/lib/esm/ed25519.js.map +1 -0
- package/lib/esm/encryption.d.ts +76 -0
- package/lib/esm/encryption.js +350 -0
- package/lib/esm/encryption.js.map +1 -0
- package/lib/esm/errors.d.ts +3 -0
- package/lib/esm/errors.js +6 -0
- package/lib/esm/errors.js.map +1 -0
- package/lib/esm/hash-browser.d.ts +4 -0
- package/lib/esm/hash-browser.js +7 -0
- package/lib/esm/hash-browser.js.map +1 -0
- package/lib/esm/hash.d.ts +4 -0
- package/lib/esm/hash.js +6 -0
- package/lib/esm/hash.js.map +1 -0
- package/lib/esm/index.d.ts +14 -0
- package/lib/esm/index.js +15 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/key.d.ts +44 -0
- package/lib/esm/key.js +51 -0
- package/lib/esm/key.js.map +1 -0
- package/lib/esm/keychain.d.ts +30 -0
- package/lib/esm/keychain.js +142 -0
- package/lib/esm/keychain.js.map +1 -0
- package/lib/esm/libp2p.d.ts +5 -0
- package/lib/esm/libp2p.js +21 -0
- package/lib/esm/libp2p.js.map +1 -0
- package/lib/esm/prehash.d.ts +6 -0
- package/lib/esm/prehash.js +32 -0
- package/lib/esm/prehash.js.map +1 -0
- package/lib/esm/random-browser.d.ts +1 -0
- package/lib/esm/random-browser.js +2 -0
- package/lib/esm/random-browser.js.map +1 -0
- package/lib/esm/random.d.ts +2 -0
- package/lib/esm/random.js +3 -0
- package/lib/esm/random.js.map +1 -0
- package/lib/esm/sepc256k1.d.ts +42 -0
- package/lib/esm/sepc256k1.js +194 -0
- package/lib/esm/sepc256k1.js.map +1 -0
- package/lib/esm/signature.d.ts +34 -0
- package/lib/esm/signature.js +283 -0
- package/lib/esm/signature.js.map +1 -0
- package/lib/esm/signer.d.ts +10 -0
- package/lib/esm/signer.js +2 -0
- package/lib/esm/signer.js.map +1 -0
- package/lib/esm/utils.d.ts +4 -0
- package/lib/esm/utils.js +10 -0
- package/lib/esm/utils.js.map +1 -0
- package/lib/esm/x25519.d.ts +38 -0
- package/lib/esm/x25519.js +158 -0
- package/lib/esm/x25519.js.map +1 -0
- package/package.json +55 -0
- package/src/bytes.ts +12 -0
- package/src/ed25519-sign-browser.ts +43 -0
- package/src/ed25519-sign.ts +83 -0
- package/src/ed25519.ts +194 -0
- package/src/encryption.ts +376 -0
- package/src/errors.ts +5 -0
- package/src/hash-browser.ts +10 -0
- package/src/hash.ts +10 -0
- package/src/index.ts +14 -0
- package/src/key.ts +80 -0
- package/src/keychain.ts +197 -0
- package/src/libp2p.ts +27 -0
- package/src/prehash.ts +42 -0
- package/src/random-browser.ts +2 -0
- package/src/random.ts +2 -0
- package/src/sepc256k1.ts +229 -0
- package/src/signature.ts +284 -0
- package/src/signer.ts +18 -0
- package/src/utils.ts +15 -0
- package/src/x25519.ts +167 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { deserialize, variant, field, option, serialize, } from "@dao-xyz/borsh";
|
|
11
|
+
import { equals } from "@peerbit/uint8arrays";
|
|
12
|
+
import { verifySignatureEd25519 } from "./ed25519-sign.js";
|
|
13
|
+
import { Ed25519PublicKey } from "./ed25519.js";
|
|
14
|
+
import { PublicSignKey } from "./key.js";
|
|
15
|
+
import { PreHash } from "./prehash.js";
|
|
16
|
+
import { Secp256k1PublicKey, verifySignatureSecp256k1 } from "./sepc256k1.js";
|
|
17
|
+
export let SignatureWithKey = class SignatureWithKey {
|
|
18
|
+
signature;
|
|
19
|
+
publicKey;
|
|
20
|
+
prehash = PreHash.NONE; // 0 no prehash, 1 sha256, 2 blake3?
|
|
21
|
+
constructor(props) {
|
|
22
|
+
if (props) {
|
|
23
|
+
this.signature = props.signature;
|
|
24
|
+
this.publicKey = props.publicKey;
|
|
25
|
+
this.prehash = props.prehash;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
equals(other) {
|
|
29
|
+
if (!equals(this.signature, other.signature)) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
return (Buffer.compare(serialize(this.publicKey), serialize(other.publicKey)) ===
|
|
33
|
+
0 && this.prehash == other.prehash);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
__decorate([
|
|
37
|
+
field({ type: Uint8Array }),
|
|
38
|
+
__metadata("design:type", Uint8Array)
|
|
39
|
+
], SignatureWithKey.prototype, "signature", void 0);
|
|
40
|
+
__decorate([
|
|
41
|
+
field({ type: PublicSignKey }),
|
|
42
|
+
__metadata("design:type", PublicSignKey)
|
|
43
|
+
], SignatureWithKey.prototype, "publicKey", void 0);
|
|
44
|
+
__decorate([
|
|
45
|
+
field({ type: "u8" }),
|
|
46
|
+
__metadata("design:type", Number)
|
|
47
|
+
], SignatureWithKey.prototype, "prehash", void 0);
|
|
48
|
+
SignatureWithKey = __decorate([
|
|
49
|
+
variant(0),
|
|
50
|
+
__metadata("design:paramtypes", [Object])
|
|
51
|
+
], SignatureWithKey);
|
|
52
|
+
export let MaybeSigned = class MaybeSigned {
|
|
53
|
+
data;
|
|
54
|
+
signature;
|
|
55
|
+
constructor(props) {
|
|
56
|
+
if (props) {
|
|
57
|
+
this.data = props.data;
|
|
58
|
+
this.signature = props.signature;
|
|
59
|
+
this._value = props.value;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
_value;
|
|
63
|
+
getValue(constructor) {
|
|
64
|
+
return deserialize(this.data, constructor);
|
|
65
|
+
}
|
|
66
|
+
async verify() {
|
|
67
|
+
if (!this.signature) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
return verify(this.signature, this.data);
|
|
71
|
+
}
|
|
72
|
+
equals(other) {
|
|
73
|
+
if (!equals(this.data, other.data)) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
if (!this.signature !== !other.signature) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
if (this.signature && other.signature) {
|
|
80
|
+
return this.signature.equals(other.signature);
|
|
81
|
+
}
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* In place
|
|
86
|
+
* @param signer
|
|
87
|
+
*/
|
|
88
|
+
async sign(signer) {
|
|
89
|
+
const signatureResult = await signer(this.data);
|
|
90
|
+
this.signature = signatureResult;
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
__decorate([
|
|
95
|
+
field({ type: Uint8Array }),
|
|
96
|
+
__metadata("design:type", Uint8Array)
|
|
97
|
+
], MaybeSigned.prototype, "data", void 0);
|
|
98
|
+
__decorate([
|
|
99
|
+
field({ type: option(SignatureWithKey) }),
|
|
100
|
+
__metadata("design:type", SignatureWithKey)
|
|
101
|
+
], MaybeSigned.prototype, "signature", void 0);
|
|
102
|
+
MaybeSigned = __decorate([
|
|
103
|
+
variant(0),
|
|
104
|
+
__metadata("design:paramtypes", [Object])
|
|
105
|
+
], MaybeSigned);
|
|
106
|
+
export const verify = async (signature, data) => {
|
|
107
|
+
if (signature.publicKey instanceof Ed25519PublicKey) {
|
|
108
|
+
return verifySignatureEd25519(signature, data);
|
|
109
|
+
}
|
|
110
|
+
else if (signature.publicKey instanceof Secp256k1PublicKey) {
|
|
111
|
+
return verifySignatureSecp256k1(signature, data);
|
|
112
|
+
}
|
|
113
|
+
return false;
|
|
114
|
+
};
|
|
115
|
+
/*
|
|
116
|
+
|
|
117
|
+
@variant(0)
|
|
118
|
+
export class SignatureWithKey {
|
|
119
|
+
@field({ type: Uint8Array })
|
|
120
|
+
signature: Uint8Array;
|
|
121
|
+
|
|
122
|
+
@field({ type: PublicSignKey })
|
|
123
|
+
publicKey: PublicSignKey;
|
|
124
|
+
|
|
125
|
+
constructor(props?: { signature: Uint8Array; publicKey: PublicSignKey }) {
|
|
126
|
+
if (props) {
|
|
127
|
+
this.signature = props.signature;
|
|
128
|
+
this.publicKey = props.publicKey;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
equals(other: SignatureWithKey): boolean {
|
|
133
|
+
if (!equals(this.signature, other.signature)) {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
return (
|
|
137
|
+
Buffer.compare(
|
|
138
|
+
serialize(this.publicKey),
|
|
139
|
+
serialize(other.publicKey)
|
|
140
|
+
) === 0
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
@variant(0)
|
|
146
|
+
export abstract class MaybeSigned<T> {
|
|
147
|
+
|
|
148
|
+
@field({ type: Uint8Array })
|
|
149
|
+
data: Uint8Array;
|
|
150
|
+
|
|
151
|
+
constructor(props: {
|
|
152
|
+
data: Uint8Array;
|
|
153
|
+
value?: T;
|
|
154
|
+
}) {
|
|
155
|
+
this.data = props.data;
|
|
156
|
+
this._value = props.value;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
_value?: T;
|
|
160
|
+
|
|
161
|
+
getValue(constructor: AbstractType<T>): T {
|
|
162
|
+
return deserialize(this.data, constructor);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
abstract verify(): Promise<boolean>
|
|
166
|
+
|
|
167
|
+
equals(other: MaybeSigned<T>): boolean {
|
|
168
|
+
if (!equals(this.data, other.data)) {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
@variant(0)
|
|
177
|
+
export class Signed<T> extends MaybeSigned<T> {
|
|
178
|
+
|
|
179
|
+
@field({ type: option(SignatureWithKey) })
|
|
180
|
+
signature: SignatureWithKey;
|
|
181
|
+
|
|
182
|
+
constructor(props: {
|
|
183
|
+
data: Uint8Array;
|
|
184
|
+
value?: T;
|
|
185
|
+
signature: SignatureWithKey;
|
|
186
|
+
}) {
|
|
187
|
+
super(props)
|
|
188
|
+
this.signature = props.signature;
|
|
189
|
+
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async verify(): Promise<boolean> {
|
|
193
|
+
return verify(
|
|
194
|
+
this.signature.signature,
|
|
195
|
+
this.signature.publicKey,
|
|
196
|
+
this.data
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
equals(other: MaybeSigned<T>): boolean {
|
|
201
|
+
if (other instanceof Signed<T>) {
|
|
202
|
+
if (!this.signature !== !other.signature) {
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
if (this.signature && other.signature) {
|
|
206
|
+
return this.signature.equals(other.signature);
|
|
207
|
+
}
|
|
208
|
+
return super.equals(other)
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
async sign(signer: SignWithKey): Promise<MaybeSigned<T>> {
|
|
217
|
+
const signatureResult = await signer(this.data);
|
|
218
|
+
this.signature = new SignatureWithKey({
|
|
219
|
+
publicKey: signatureResult.publicKey,
|
|
220
|
+
signature: signatureResult.signature,
|
|
221
|
+
});
|
|
222
|
+
return this;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
@variant(0)
|
|
228
|
+
export class Unsigned<T> extends MaybeSigned<T> {
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
constructor(props: {
|
|
232
|
+
data: Uint8Array;
|
|
233
|
+
value?: T;
|
|
234
|
+
}) {
|
|
235
|
+
super(props)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
equals(other: MaybeSigned<T>): boolean {
|
|
239
|
+
if (other instanceof Unsigned) {
|
|
240
|
+
return super.equals(other)
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
async sign(signer: SignWithKey): Promise<Signed<T>> {
|
|
249
|
+
const signatureResult = await signer(this.data);
|
|
250
|
+
return new Signed({
|
|
251
|
+
data: this.data,
|
|
252
|
+
signature: new SignatureWithKey({
|
|
253
|
+
publicKey: signatureResult.publicKey,
|
|
254
|
+
signature: signatureResult.signature,
|
|
255
|
+
}),
|
|
256
|
+
value: this._value
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async verify(): Promise<boolean> {
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
export const verify = (
|
|
267
|
+
signature: Uint8Array,
|
|
268
|
+
publicKey: PublicSignKey,
|
|
269
|
+
data: Uint8Array
|
|
270
|
+
) => {
|
|
271
|
+
if (!signature) {
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
274
|
+
if (publicKey instanceof Ed25519PublicKey) {
|
|
275
|
+
return verifySignatureEd25519(signature, publicKey, data);
|
|
276
|
+
} else if (publicKey instanceof Secp256k1PublicKey) {
|
|
277
|
+
return verifySignatureSecp256k1(signature, publicKey, data);
|
|
278
|
+
}
|
|
279
|
+
return false;
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
*/
|
|
283
|
+
//# sourceMappingURL=signature.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signature.js","sourceRoot":"","sources":["../../src/signature.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EACN,WAAW,EACX,OAAO,EACP,KAAK,EACL,MAAM,EACN,SAAS,GAET,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAC3D,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,kBAAkB,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAC;AAIvE,WAAM,gBAAgB,GAAtB,MAAM,gBAAgB;IAE5B,SAAS,CAAa;IAGtB,SAAS,CAAgB;IAGzB,OAAO,GAAY,OAAO,CAAC,IAAI,CAAC,CAAC,oCAAoC;IAErE,YAAY,KAIX;QACA,IAAI,KAAK,EAAE;YACV,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC,SAAS,CAAC;YACjC,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC,SAAS,CAAC;YACjC,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;SAC7B;IACF,CAAC;IAED,MAAM,CAAC,K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AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAuKE"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { PublicSignKey } from "./key.js";
|
|
2
|
+
import { PreHash } from "./prehash.js";
|
|
3
|
+
import { SignatureWithKey } from "./signature.js";
|
|
4
|
+
export interface Signer {
|
|
5
|
+
sign: (bytes: Uint8Array, prehash?: PreHash) => Promise<SignatureWithKey> | SignatureWithKey;
|
|
6
|
+
}
|
|
7
|
+
export type SignWithKey = (bytes: Uint8Array) => Promise<SignatureWithKey> | SignatureWithKey;
|
|
8
|
+
export type Identity<T extends PublicSignKey = PublicSignKey> = Signer & {
|
|
9
|
+
publicKey: T;
|
|
10
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signer.js","sourceRoot":"","sources":["../../src/signer.ts"],"names":[],"mappings":""}
|
package/lib/esm/utils.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import sodium from "libsodium-wrappers";
|
|
2
|
+
export const fromHexString = (hexString) => Uint8Array.from(hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
|
|
3
|
+
export const toHexString = (bytes) => bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
|
|
4
|
+
export const toBase64 = (arr) => {
|
|
5
|
+
return sodium.to_base64(arr, sodium.base64_variants.ORIGINAL);
|
|
6
|
+
};
|
|
7
|
+
export const fromBase64 = (base64) => {
|
|
8
|
+
return sodium.from_base64(base64, sodium.base64_variants.ORIGINAL);
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,SAAiB,EAAE,EAAE,CAClD,UAAU,CAAC,IAAI,CACd,SAAS,CAAC,KAAK,CAAC,SAAS,CAAE,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAC7D,CAAC;AAEH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAiB,EAAE,EAAE,CAChD,KAAK,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC;AAE3E,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,GAAe,EAAE,EAAE;IAC3C,OAAO,MAAM,CAAC,SAAS,CAAC,GAAG,EAAE,MAAM,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAC;AAC/D,CAAC,CAAC;AACF,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,MAAc,EAAE,EAAE;IAC5C,OAAO,MAAM,CAAC,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAC;AACpE,CAAC,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export * from "./errors.js";
|
|
2
|
+
import { Keypair, PrivateEncryptionKey, PublicKeyEncryptionKey } from "./key.js";
|
|
3
|
+
import { Ed25519Keypair, Ed25519PublicKey } from "./ed25519.js";
|
|
4
|
+
import { PeerId } from "@libp2p/interface-peer-id";
|
|
5
|
+
export declare class X25519PublicKey extends PublicKeyEncryptionKey {
|
|
6
|
+
publicKey: Uint8Array;
|
|
7
|
+
constructor(properties: {
|
|
8
|
+
publicKey: Uint8Array;
|
|
9
|
+
});
|
|
10
|
+
equals(other: PublicKeyEncryptionKey): boolean;
|
|
11
|
+
toString(): string;
|
|
12
|
+
static from(ed25119PublicKey: Ed25519PublicKey): Promise<X25519PublicKey>;
|
|
13
|
+
static fromPeerId(peerId: PeerId): Promise<X25519PublicKey>;
|
|
14
|
+
static create(): Promise<X25519PublicKey>;
|
|
15
|
+
}
|
|
16
|
+
export declare class X25519SecretKey extends PrivateEncryptionKey {
|
|
17
|
+
secretKey: Uint8Array;
|
|
18
|
+
constructor(properties: {
|
|
19
|
+
secretKey: Uint8Array;
|
|
20
|
+
});
|
|
21
|
+
equals(other: PublicKeyEncryptionKey): boolean;
|
|
22
|
+
toString(): string;
|
|
23
|
+
publicKey(): Promise<X25519PublicKey>;
|
|
24
|
+
static from(ed25119Keypair: Ed25519Keypair): Promise<X25519SecretKey>;
|
|
25
|
+
static create(): Promise<X25519SecretKey>;
|
|
26
|
+
}
|
|
27
|
+
export declare class X25519Keypair extends Keypair {
|
|
28
|
+
publicKey: X25519PublicKey;
|
|
29
|
+
secretKey: X25519SecretKey;
|
|
30
|
+
constructor(properties: {
|
|
31
|
+
publicKey: X25519PublicKey;
|
|
32
|
+
secretKey: X25519SecretKey;
|
|
33
|
+
});
|
|
34
|
+
static create(): Promise<X25519Keypair>;
|
|
35
|
+
static from(ed25119Keypair: Ed25519Keypair): Promise<X25519Keypair>;
|
|
36
|
+
static fromPeerId(peerId: PeerId): Promise<X25519Keypair>;
|
|
37
|
+
equals(other: Keypair): boolean;
|
|
38
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var X25519PublicKey_1, X25519SecretKey_1, X25519Keypair_1;
|
|
11
|
+
export * from "./errors.js";
|
|
12
|
+
import { field, fixedArray, variant } from "@dao-xyz/borsh";
|
|
13
|
+
import { compare } from "@peerbit/uint8arrays";
|
|
14
|
+
import sodium from "libsodium-wrappers";
|
|
15
|
+
import { Keypair, PrivateEncryptionKey, PublicKeyEncryptionKey, } from "./key.js";
|
|
16
|
+
import { Ed25519Keypair, Ed25519PublicKey, } from "./ed25519.js";
|
|
17
|
+
import { toHexString } from "./utils.js";
|
|
18
|
+
export let X25519PublicKey = X25519PublicKey_1 = class X25519PublicKey extends PublicKeyEncryptionKey {
|
|
19
|
+
publicKey;
|
|
20
|
+
constructor(properties) {
|
|
21
|
+
super();
|
|
22
|
+
if (properties.publicKey.length !== 32) {
|
|
23
|
+
throw new Error("Expecting key to have length 32");
|
|
24
|
+
}
|
|
25
|
+
this.publicKey = properties.publicKey;
|
|
26
|
+
}
|
|
27
|
+
equals(other) {
|
|
28
|
+
if (other instanceof X25519PublicKey_1) {
|
|
29
|
+
return compare(this.publicKey, other.publicKey) === 0;
|
|
30
|
+
}
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
toString() {
|
|
34
|
+
return "x25519p/" + toHexString(this.publicKey);
|
|
35
|
+
}
|
|
36
|
+
static async from(ed25119PublicKey) {
|
|
37
|
+
await sodium.ready;
|
|
38
|
+
return new X25519PublicKey_1({
|
|
39
|
+
publicKey: sodium.crypto_sign_ed25519_pk_to_curve25519(ed25119PublicKey.publicKey),
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
static async fromPeerId(peerId) {
|
|
43
|
+
await sodium.ready;
|
|
44
|
+
const ed = await Ed25519PublicKey.fromPeerId(peerId);
|
|
45
|
+
return X25519PublicKey_1.from(ed);
|
|
46
|
+
}
|
|
47
|
+
static async create() {
|
|
48
|
+
await sodium.ready;
|
|
49
|
+
return new X25519PublicKey_1({
|
|
50
|
+
publicKey: sodium.crypto_box_keypair().publicKey,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
__decorate([
|
|
55
|
+
field({ type: fixedArray("u8", 32) }),
|
|
56
|
+
__metadata("design:type", Uint8Array)
|
|
57
|
+
], X25519PublicKey.prototype, "publicKey", void 0);
|
|
58
|
+
X25519PublicKey = X25519PublicKey_1 = __decorate([
|
|
59
|
+
variant(0),
|
|
60
|
+
__metadata("design:paramtypes", [Object])
|
|
61
|
+
], X25519PublicKey);
|
|
62
|
+
export let X25519SecretKey = X25519SecretKey_1 = class X25519SecretKey extends PrivateEncryptionKey {
|
|
63
|
+
secretKey;
|
|
64
|
+
constructor(properties) {
|
|
65
|
+
super();
|
|
66
|
+
if (properties.secretKey.length !== 32) {
|
|
67
|
+
throw new Error("Expecting key to have length 32");
|
|
68
|
+
}
|
|
69
|
+
this.secretKey = properties.secretKey;
|
|
70
|
+
}
|
|
71
|
+
equals(other) {
|
|
72
|
+
if (other instanceof X25519SecretKey_1) {
|
|
73
|
+
return (compare(this.secretKey, other.secretKey) === 0);
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
toString() {
|
|
78
|
+
return "x25519s" + toHexString(this.secretKey);
|
|
79
|
+
}
|
|
80
|
+
async publicKey() {
|
|
81
|
+
return new X25519PublicKey({
|
|
82
|
+
publicKey: sodium.crypto_scalarmult_base(this.secretKey),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
static async from(ed25119Keypair) {
|
|
86
|
+
await sodium.ready;
|
|
87
|
+
return new X25519SecretKey_1({
|
|
88
|
+
secretKey: sodium.crypto_sign_ed25519_sk_to_curve25519(ed25119Keypair.privateKeyPublicKey),
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
static async create() {
|
|
92
|
+
await sodium.ready;
|
|
93
|
+
return new X25519SecretKey_1({
|
|
94
|
+
secretKey: sodium.crypto_box_keypair().privateKey,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
__decorate([
|
|
99
|
+
field({ type: fixedArray("u8", 32) }),
|
|
100
|
+
__metadata("design:type", Uint8Array)
|
|
101
|
+
], X25519SecretKey.prototype, "secretKey", void 0);
|
|
102
|
+
X25519SecretKey = X25519SecretKey_1 = __decorate([
|
|
103
|
+
variant(0),
|
|
104
|
+
__metadata("design:paramtypes", [Object])
|
|
105
|
+
], X25519SecretKey);
|
|
106
|
+
export let X25519Keypair = X25519Keypair_1 = class X25519Keypair extends Keypair {
|
|
107
|
+
publicKey;
|
|
108
|
+
secretKey;
|
|
109
|
+
constructor(properties) {
|
|
110
|
+
super();
|
|
111
|
+
this.publicKey = properties.publicKey;
|
|
112
|
+
this.secretKey = properties.secretKey;
|
|
113
|
+
}
|
|
114
|
+
static async create() {
|
|
115
|
+
await sodium.ready;
|
|
116
|
+
const generated = sodium.crypto_box_keypair();
|
|
117
|
+
const kp = new X25519Keypair_1({
|
|
118
|
+
publicKey: new X25519PublicKey({
|
|
119
|
+
publicKey: generated.publicKey,
|
|
120
|
+
}),
|
|
121
|
+
secretKey: new X25519SecretKey({
|
|
122
|
+
secretKey: generated.privateKey,
|
|
123
|
+
}),
|
|
124
|
+
});
|
|
125
|
+
return kp;
|
|
126
|
+
}
|
|
127
|
+
static async from(ed25119Keypair) {
|
|
128
|
+
const kp = new X25519Keypair_1({
|
|
129
|
+
publicKey: await X25519PublicKey.from(ed25119Keypair.publicKey),
|
|
130
|
+
secretKey: await X25519SecretKey.from(ed25119Keypair),
|
|
131
|
+
});
|
|
132
|
+
return kp;
|
|
133
|
+
}
|
|
134
|
+
static fromPeerId(peerId) {
|
|
135
|
+
const ed = Ed25519Keypair.fromPeerId(peerId);
|
|
136
|
+
return X25519Keypair_1.from(ed);
|
|
137
|
+
}
|
|
138
|
+
equals(other) {
|
|
139
|
+
if (other instanceof X25519Keypair_1) {
|
|
140
|
+
return (this.publicKey.equals(other.publicKey) &&
|
|
141
|
+
this.secretKey.equals(other.secretKey));
|
|
142
|
+
}
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
__decorate([
|
|
147
|
+
field({ type: X25519PublicKey }),
|
|
148
|
+
__metadata("design:type", X25519PublicKey)
|
|
149
|
+
], X25519Keypair.prototype, "publicKey", void 0);
|
|
150
|
+
__decorate([
|
|
151
|
+
field({ type: X25519SecretKey }),
|
|
152
|
+
__metadata("design:type", X25519SecretKey)
|
|
153
|
+
], X25519Keypair.prototype, "secretKey", void 0);
|
|
154
|
+
X25519Keypair = X25519Keypair_1 = __decorate([
|
|
155
|
+
variant(1),
|
|
156
|
+
__metadata("design:paramtypes", [Object])
|
|
157
|
+
], X25519Keypair);
|
|
158
|
+
//# sourceMappingURL=x25519.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"x25519.js","sourceRoot":"","sources":["../../src/x25519.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,cAAc,aAAa,CAAC;AAC5B,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAC5D,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC/C,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,EACN,OAAO,EACP,oBAAoB,EACpB,sBAAsB,GACtB,MAAM,UAAU,CAAC;AAClB,OAAO,EACN,cAAc,EACd,gBAAgB,GAEhB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAGlC,WAAM,eAAe,uBAArB,MAAM,eAAgB,SAAQ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U,CAAC,MAAM,CAAC,CAAC;QAC7C,OAAO,eAAa,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC/B,CAAC;IACD,MAAM,CAAC,KAAc;QACpB,IAAI,KAAK,YAAY,eAAa,EAAE;YACnC,OAAO,CACN,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC;gBACtC,IAAI,CAAC,SAAS,CAAC,MAAM,CACnB,KAAuB,CAAC,SAAmC,CAC5D,CACD,CAAC;SACF;QACD,OAAO,KAAK,CAAC;IACd,CAAC;CACD,CAAA;AApDA;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,eAAe,EAAE,CAAC;8BACtB,eAAe;gDAAC;AAG3B;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,eAAe,EAAE,CAAC;8BACtB,eAAe;gDAAC;AALf,aAAa;IADzB,OAAO,CAAC,CAAC,CAAC;;GACE,aAAa,CAsDzB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@peerbit/crypto",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Crypto fn",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"module": "lib/esm/index.js",
|
|
8
|
+
"types": "lib/esm/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
"import": "./lib/esm/index.js"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"lib",
|
|
14
|
+
"src",
|
|
15
|
+
"!src/**/__tests__",
|
|
16
|
+
"!lib/**/__tests__",
|
|
17
|
+
"!src/**/__benchmark__",
|
|
18
|
+
"!lib/**/__benchmark__",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"browser": {
|
|
25
|
+
"./lib/esm/hash.js": "./lib/esm/hash-browser.js",
|
|
26
|
+
"./hash.js": "./lib/esm/hash-browser.js",
|
|
27
|
+
"./lib/esm/random.js": "./lib/esm/random-browser.js",
|
|
28
|
+
"./random.js": "./lib/esm/random-browser.js",
|
|
29
|
+
"./lib/esm/ed25519-sign.js": "./lib/esm/ed25519-sign-browser.js",
|
|
30
|
+
"./ed25519-sign.js": "./lib/esm/ed25519-sign-browser.js"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"clean": "shx rm -rf lib/*",
|
|
34
|
+
"build": "yarn clean && tsc -p tsconfig.json",
|
|
35
|
+
"test": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.ts --runInBand --forceExit",
|
|
36
|
+
"test:unit": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.unit.ts --runInBand --forceExit",
|
|
37
|
+
"test:integration": "node ../node_modules/.bin/jest test -c ../../../jest.config.integration.ts --runInBand --forceExit"
|
|
38
|
+
},
|
|
39
|
+
"author": "dao.xyz",
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/libsodium-wrappers": "^0.7.10",
|
|
43
|
+
"uint8arrays": "^4.0.4"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@dao-xyz/borsh": "^5.1.5",
|
|
47
|
+
"@ethersproject/wallet": "^5.7.0",
|
|
48
|
+
"@libp2p/crypto": "^1.0.17",
|
|
49
|
+
"@libp2p/peer-id": "^2.0.3",
|
|
50
|
+
"@peerbit/cache": "^1.0.0",
|
|
51
|
+
"@peerbit/uint8arrays": "2.0.0",
|
|
52
|
+
"libsodium-wrappers": "^0.7.11"
|
|
53
|
+
},
|
|
54
|
+
"gitHead": "069ce2f62a76c342875a1cc695c6f210beff13fd"
|
|
55
|
+
}
|
package/src/bytes.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {ArrayBufferView|ArrayBuffer|Uint8Array} o
|
|
3
|
+
* @returns {Uint8Array}
|
|
4
|
+
*/
|
|
5
|
+
export const coerce = (o) => {
|
|
6
|
+
if (o instanceof Uint8Array && o.constructor.name === "Uint8Array") return o;
|
|
7
|
+
if (o instanceof ArrayBuffer) return new Uint8Array(o);
|
|
8
|
+
if (ArrayBuffer.isView(o)) {
|
|
9
|
+
return new Uint8Array(o.buffer, o.byteOffset, o.byteLength);
|
|
10
|
+
}
|
|
11
|
+
throw new Error("Unknown type, must be binary type");
|
|
12
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Ed25519Keypair, Ed25519PublicKey } from "./ed25519.js";
|
|
2
|
+
import sodium from "libsodium-wrappers";
|
|
3
|
+
import { PreHash, prehashFn } from "./prehash.js";
|
|
4
|
+
import { SignatureWithKey } from "./signature.js";
|
|
5
|
+
|
|
6
|
+
export const sign = async (
|
|
7
|
+
data: Uint8Array,
|
|
8
|
+
keypair: Ed25519Keypair,
|
|
9
|
+
prehash: PreHash
|
|
10
|
+
) => {
|
|
11
|
+
const hashedData = await prehashFn(data, prehash);
|
|
12
|
+
/* const init = sodium.crypto_sign_init()
|
|
13
|
+
sodium.crypto_sign_update(init, hashedData)
|
|
14
|
+
const signature = sodium.crypto_sign_final_create(init, keypair.privateKey.privateKey, 'uint8array') */
|
|
15
|
+
return new SignatureWithKey({
|
|
16
|
+
prehash,
|
|
17
|
+
publicKey: keypair.publicKey,
|
|
18
|
+
signature: sodium.crypto_sign_detached(
|
|
19
|
+
hashedData,
|
|
20
|
+
keypair.privateKeyPublicKey
|
|
21
|
+
),
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const verifySignatureEd25519 = async (
|
|
26
|
+
signature: SignatureWithKey,
|
|
27
|
+
data: Uint8Array
|
|
28
|
+
) => {
|
|
29
|
+
let res = false;
|
|
30
|
+
try {
|
|
31
|
+
const hashedData = await prehashFn(data, signature.prehash);
|
|
32
|
+
|
|
33
|
+
const verified = sodium.crypto_sign_verify_detached(
|
|
34
|
+
signature.signature,
|
|
35
|
+
hashedData,
|
|
36
|
+
(signature.publicKey as Ed25519PublicKey).publicKey
|
|
37
|
+
);
|
|
38
|
+
res = verified;
|
|
39
|
+
} catch (error) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return res;
|
|
43
|
+
};
|