@atproto/crypto 0.1.0 → 0.2.0

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.
Files changed (61) hide show
  1. package/dist/did.d.ts +0 -1
  2. package/dist/index.d.ts +0 -1
  3. package/dist/index.js +1885 -2533
  4. package/dist/index.js.map +4 -4
  5. package/dist/multibase.d.ts +2 -0
  6. package/dist/p256/keypair.d.ts +9 -9
  7. package/dist/p256/operations.d.ts +1 -3
  8. package/dist/random.d.ts +3 -2
  9. package/dist/secp256k1/operations.d.ts +1 -0
  10. package/dist/sha.d.ts +0 -2
  11. package/dist/types.d.ts +1 -0
  12. package/package.json +10 -7
  13. package/src/did.ts +4 -8
  14. package/src/index.ts +0 -1
  15. package/src/multibase.ts +27 -0
  16. package/src/p256/encoding.ts +5 -72
  17. package/src/p256/keypair.ts +25 -41
  18. package/src/p256/operations.ts +6 -45
  19. package/src/random.ts +15 -8
  20. package/src/secp256k1/encoding.ts +3 -5
  21. package/src/secp256k1/keypair.ts +7 -5
  22. package/src/secp256k1/operations.ts +12 -3
  23. package/src/sha.ts +2 -19
  24. package/src/types.ts +1 -0
  25. package/tests/did.test.ts +8 -22
  26. package/tests/key-compression.test.ts +3 -3
  27. package/tests/keypairs.test.ts +71 -0
  28. package/tests/signature-fixtures.json +34 -0
  29. package/tests/signatures.test.ts +161 -0
  30. package/tsconfig.build.tsbuildinfo +1 -0
  31. package/tsconfig.json +1 -4
  32. package/dist/const.d.ts +0 -5
  33. package/dist/p256/encoding.d.ts +0 -2
  34. package/dist/p256/plugin.d.ts +0 -3
  35. package/dist/plugins.d.ts +0 -2
  36. package/dist/secp256k1/encoding.d.ts +0 -2
  37. package/dist/secp256k1/keypair.d.ts +0 -20
  38. package/dist/secp256k1/plugin.d.ts +0 -3
  39. package/dist/src/aes.d.ts +0 -8
  40. package/dist/src/const.d.ts +0 -5
  41. package/dist/src/did.d.ts +0 -7
  42. package/dist/src/index.d.ts +0 -12
  43. package/dist/src/multibase.d.ts +0 -1
  44. package/dist/src/p256/ecdh.d.ts +0 -11
  45. package/dist/src/p256/ecdsa.d.ts +0 -19
  46. package/dist/src/p256/encoding.d.ts +0 -2
  47. package/dist/src/p256/keypair.d.ts +0 -19
  48. package/dist/src/p256/operations.d.ts +0 -4
  49. package/dist/src/p256/plugin.d.ts +0 -3
  50. package/dist/src/plugins.d.ts +0 -2
  51. package/dist/src/random.d.ts +0 -4
  52. package/dist/src/secp256k1/encoding.d.ts +0 -2
  53. package/dist/src/secp256k1/keypair.d.ts +0 -19
  54. package/dist/src/secp256k1/operations.d.ts +0 -1
  55. package/dist/src/secp256k1/plugin.d.ts +0 -3
  56. package/dist/src/sha.d.ts +0 -3
  57. package/dist/src/types.d.ts +0 -13
  58. package/dist/src/verify.d.ts +0 -1
  59. package/dist/verify.d.ts +0 -2
  60. package/src/aes.ts +0 -64
  61. package/tests/export.test.ts +0 -50
package/dist/verify.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export declare const verifySignature: (didKey: string, data: Uint8Array, sig: Uint8Array) => Promise<boolean>;
2
- export declare const verifySignatureUtf8: (didKey: string, data: string, sig: string) => Promise<boolean>;
package/src/aes.ts DELETED
@@ -1,64 +0,0 @@
1
- import { webcrypto } from 'one-webcrypto'
2
- import * as uint8arrays from 'uint8arrays'
3
-
4
- import * as random from './random'
5
-
6
- export class AesKey {
7
- private key: CryptoKey
8
- constructor(key: CryptoKey) {
9
- this.key = key
10
- }
11
-
12
- static async create(): Promise<AesKey> {
13
- const key = await webcrypto.subtle.generateKey(
14
- {
15
- name: 'AES-GCM',
16
- length: 256,
17
- },
18
- true,
19
- ['encrypt', 'decrypt'],
20
- )
21
- return new AesKey(key)
22
- }
23
-
24
- // utf8 data -> base64pad cipher
25
- // returns base64 encrypted data with iv prepended
26
- async encrypt(data: string): Promise<string> {
27
- const iv = random.randomIV()
28
- const dataBytes = uint8arrays.fromString(data, 'utf8')
29
- const buf = await webcrypto.subtle.encrypt(
30
- {
31
- name: 'AES-GCM',
32
- iv,
33
- },
34
- this.key,
35
- dataBytes,
36
- )
37
- const encryptedBytes = new Uint8Array(buf)
38
- const encrypted = uint8arrays.toString(
39
- uint8arrays.concat([iv, encryptedBytes]),
40
- 'base64pad',
41
- )
42
- return encrypted
43
- }
44
-
45
- // base64pad cipher -> utf8 data
46
- // expects base64 encrypted data with iv prepended
47
- async decrypt(data: string): Promise<string> {
48
- const dataBytes = uint8arrays.fromString(data, 'base64pad')
49
- const iv = dataBytes.slice(0, 12)
50
- const encrypted = dataBytes.slice(12)
51
- const buf = await webcrypto.subtle.decrypt(
52
- {
53
- name: 'AES-GCM',
54
- iv,
55
- },
56
- this.key,
57
- encrypted,
58
- )
59
- const decryptedBytes = new Uint8Array(buf)
60
- return uint8arrays.toString(decryptedBytes, 'utf8')
61
- }
62
- }
63
-
64
- export default AesKey
@@ -1,50 +0,0 @@
1
- import EcdsaKeypair from '../src/p256/keypair'
2
- import Secp256k1Keypair from '../src/secp256k1/keypair'
3
- import * as p256 from '../src/p256/operations'
4
- import * as secp from '../src/secp256k1/operations'
5
-
6
- describe('exports and reimports keys', () => {
7
- describe('secp256k1', () => {
8
- let keypair: Secp256k1Keypair
9
- let imported: Secp256k1Keypair
10
-
11
- it('has the same DID', async () => {
12
- keypair = await Secp256k1Keypair.create({ exportable: true })
13
- const exported = await keypair.export()
14
- imported = await Secp256k1Keypair.import(exported, { exportable: true })
15
-
16
- expect(keypair.did()).toBe(imported.did())
17
- })
18
-
19
- it('produces a valid signature', async () => {
20
- const data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8])
21
- const sig = await imported.sign(data)
22
-
23
- const validSig = await secp.verifyDidSig(keypair.did(), data, sig)
24
-
25
- expect(validSig).toBeTruthy()
26
- })
27
- })
28
-
29
- describe('P-256', () => {
30
- let keypair: EcdsaKeypair
31
- let imported: EcdsaKeypair
32
-
33
- it('has the same DID', async () => {
34
- keypair = await EcdsaKeypair.create({ exportable: true })
35
- const exported = await keypair.export()
36
- imported = await EcdsaKeypair.import(exported, { exportable: true })
37
-
38
- expect(keypair.did()).toBe(imported.did())
39
- })
40
-
41
- it('produces a valid signature', async () => {
42
- const data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8])
43
- const sig = await imported.sign(data)
44
-
45
- const validSig = await p256.verifyDidSig(keypair.did(), data, sig)
46
-
47
- expect(validSig).toBeTruthy()
48
- })
49
- })
50
- })