@twin.org/crypto 0.0.1-next.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.
Files changed (57) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +31 -0
  3. package/dist/cjs/index.cjs +1572 -0
  4. package/dist/esm/index.mjs +1528 -0
  5. package/dist/types/address/bech32.d.ts +28 -0
  6. package/dist/types/address/bip44.d.ts +53 -0
  7. package/dist/types/ciphers/chaCha20Poly1305.d.ts +24 -0
  8. package/dist/types/curves/ed25519.d.ts +37 -0
  9. package/dist/types/curves/secp256k1.d.ts +37 -0
  10. package/dist/types/curves/x25519.d.ts +18 -0
  11. package/dist/types/curves/zip215.d.ts +15 -0
  12. package/dist/types/hashes/blake2b.d.ts +55 -0
  13. package/dist/types/hashes/hmacSha1.d.ts +28 -0
  14. package/dist/types/hashes/hmacSha256.d.ts +44 -0
  15. package/dist/types/hashes/hmacSha512.d.ts +66 -0
  16. package/dist/types/hashes/pbkdf2.d.ts +23 -0
  17. package/dist/types/hashes/sha1.d.ts +26 -0
  18. package/dist/types/hashes/sha256.d.ts +41 -0
  19. package/dist/types/hashes/sha512.d.ts +61 -0
  20. package/dist/types/index.d.ts +23 -0
  21. package/dist/types/keys/bip32Path.d.ts +40 -0
  22. package/dist/types/keys/bip39.d.ts +36 -0
  23. package/dist/types/keys/slip0010.d.ts +38 -0
  24. package/dist/types/models/keyType.d.ts +17 -0
  25. package/dist/types/otp/hotp.d.ts +14 -0
  26. package/dist/types/otp/totp.d.ts +46 -0
  27. package/dist/types/passwords/passwordGenerator.d.ts +11 -0
  28. package/dist/types/passwords/passwordValidator.d.ts +22 -0
  29. package/docs/changelog.md +5 -0
  30. package/docs/examples.md +1 -0
  31. package/docs/reference/classes/Bech32.md +89 -0
  32. package/docs/reference/classes/Bip32Path.md +117 -0
  33. package/docs/reference/classes/Bip39.md +121 -0
  34. package/docs/reference/classes/Bip44.md +173 -0
  35. package/docs/reference/classes/Blake2b.md +155 -0
  36. package/docs/reference/classes/ChaCha20Poly1305.md +69 -0
  37. package/docs/reference/classes/Ed25519.md +113 -0
  38. package/docs/reference/classes/HmacSha1.md +79 -0
  39. package/docs/reference/classes/HmacSha256.md +123 -0
  40. package/docs/reference/classes/HmacSha512.md +187 -0
  41. package/docs/reference/classes/Hotp.md +39 -0
  42. package/docs/reference/classes/PasswordGenerator.md +33 -0
  43. package/docs/reference/classes/PasswordValidator.md +56 -0
  44. package/docs/reference/classes/Pbkdf2.md +77 -0
  45. package/docs/reference/classes/Secp256k1.md +113 -0
  46. package/docs/reference/classes/Sha1.md +69 -0
  47. package/docs/reference/classes/Sha256.md +111 -0
  48. package/docs/reference/classes/Sha512.md +167 -0
  49. package/docs/reference/classes/Slip0010.md +114 -0
  50. package/docs/reference/classes/Totp.md +148 -0
  51. package/docs/reference/classes/X25519.md +57 -0
  52. package/docs/reference/classes/Zip215.md +43 -0
  53. package/docs/reference/index.md +34 -0
  54. package/docs/reference/type-aliases/KeyType.md +5 -0
  55. package/docs/reference/variables/KeyType.md +19 -0
  56. package/locales/en.json +71 -0
  57. package/package.json +71 -0
@@ -0,0 +1,38 @@
1
+ import type { Bip32Path } from "./bip32Path";
2
+ import { KeyType } from "../models/keyType";
3
+ /**
4
+ * Class to help with slip0010 key derivation
5
+ * https://github.com/satoshilabs/slips/blob/master/slip-0010.md.
6
+ */
7
+ export declare class Slip0010 {
8
+ /**
9
+ * Get the master key from the seed.
10
+ * @param seed The seed to generate the master key from.
11
+ * @param keyType The key type.
12
+ * @returns The key and chain code.
13
+ * @throws If the seed is invalid.
14
+ */
15
+ static getMasterKeyFromSeed(seed: Uint8Array, keyType?: KeyType): {
16
+ privateKey: Uint8Array;
17
+ chainCode: Uint8Array;
18
+ };
19
+ /**
20
+ * Derive a key from the path.
21
+ * @param seed The seed.
22
+ * @param path The path.
23
+ * @param keyType The key type.
24
+ * @returns The key and chain code.
25
+ */
26
+ static derivePath(seed: Uint8Array, path: Bip32Path, keyType?: KeyType): {
27
+ privateKey: Uint8Array;
28
+ chainCode: Uint8Array;
29
+ };
30
+ /**
31
+ * Get the public key from the private key.
32
+ * @param privateKey The private key.
33
+ * @param keyType The key type.
34
+ * @param withZeroByte Include a zero bute prefix.
35
+ * @returns The public key.
36
+ */
37
+ static getPublicKey(privateKey: Uint8Array, keyType?: KeyType, withZeroByte?: boolean): Uint8Array;
38
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * The names of the key types.
3
+ */
4
+ export declare const KeyType: {
5
+ /**
6
+ * Ed25519.
7
+ */
8
+ readonly Ed25519: 0;
9
+ /**
10
+ * Secp256k1.
11
+ */
12
+ readonly Secp256k1: 1;
13
+ };
14
+ /**
15
+ * Key types.
16
+ */
17
+ export type KeyType = (typeof KeyType)[keyof typeof KeyType];
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Perform HOTP.
3
+ * Implementation of https://datatracker.ietf.org/doc/html/rfc4226 .
4
+ */
5
+ export declare class Hotp {
6
+ /**
7
+ * Generate a counter based One Time Password.
8
+ * @param key Key for the one time password.
9
+ * @param counter This should be stored by the application,
10
+ * must be user specific, and be incremented for each request.
11
+ * @returns The one time password.
12
+ */
13
+ static generate(key: Uint8Array, counter: number): string;
14
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Perform TOTP.
3
+ * Implementation of https://datatracker.ietf.org/doc/html/rfc4226 .
4
+ */
5
+ export declare class Totp {
6
+ /**
7
+ * Generate a time based One Time Password.
8
+ * @param key Key for the one time password.
9
+ * @param interval The time step of the counter.
10
+ * @param timestamp The timestamp.
11
+ * @returns The one time password.
12
+ */
13
+ static generate(key: Uint8Array, interval?: number, timestamp?: number): string;
14
+ /**
15
+ * Check a One Time Password based on a timer.
16
+ * @param token Passcode to validate.
17
+ * @param key Key for the one time password. This should be unique and secret for
18
+ * every user as it is the seed used to calculate the HMAC.
19
+ * @param window The allowable margin for the counter.
20
+ * @param interval The time step of the counter.
21
+ * @param timestamp The timestamp now.
22
+ * @returns Undefined if failure, delta on success
23
+ */
24
+ static verify(token: string, key: Uint8Array, window?: number, interval?: number, timestamp?: number): number | undefined;
25
+ /**
26
+ * Generate a secret.
27
+ * @param length The length of the secret to generate.
28
+ * @returns The secret encoded as base32.
29
+ */
30
+ static generateSecret(length: number): string;
31
+ /**
32
+ * Convert the secret back to bytes.
33
+ * @param secretBase32 The secret encoded as base32.
34
+ * @returns The bytes of the secret.
35
+ */
36
+ static secretToBytes(secretBase32: string): Uint8Array;
37
+ /**
38
+ * Generate a url for use with authenticator apps.
39
+ * See https://github.com/google/google-authenticator/wiki/Key-Uri-Format .
40
+ * @param issuer The issuer of the totp.
41
+ * @param label The label that will show in auth apps.
42
+ * @param secretBase32 The secret as base 32.
43
+ * @returns The url.
44
+ */
45
+ static generateAuthUrl(issuer: string, label: string, secretBase32: string): string;
46
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Generate random passwords.
3
+ */
4
+ export declare class PasswordGenerator {
5
+ /**
6
+ * Generate a password of given length.
7
+ * @param length The length of the password to generate.
8
+ * @returns The random password.
9
+ */
10
+ static generate(length: number): string;
11
+ }
@@ -0,0 +1,22 @@
1
+ import { type IValidationFailure } from "@twin.org/core";
2
+ /**
3
+ * Test password strength.
4
+ * Ref https://www.owasp.org/index.php/Authentication_Cheat_Sheet#Implement_Proper_Password_Strength_Controls .
5
+ */
6
+ export declare class PasswordValidator {
7
+ /**
8
+ * Test the strength of the password.
9
+ * @param property The name of the property.
10
+ * @param password The password to test.
11
+ * @param failures The list of failures to add to.
12
+ * @param options Options to configure the testing.
13
+ * @param options.minLength The minimum length of the password, defaults to 8.
14
+ * @param options.maxLength The minimum length of the password, defaults to 128.
15
+ * @param options.minPhraseLength The minimum length of the password for it to be considered a pass phrase.
16
+ */
17
+ static validate(property: string, password: string, failures: IValidationFailure[], options?: {
18
+ minLength?: number;
19
+ maxLength?: number;
20
+ minPhraseLength?: number;
21
+ }): void;
22
+ }
@@ -0,0 +1,5 @@
1
+ # @twin.org/crypto - Changelog
2
+
3
+ ## v0.0.1
4
+
5
+ - Added: Bip44
@@ -0,0 +1 @@
1
+ # @twin.org/crypto - Examples
@@ -0,0 +1,89 @@
1
+ # Class: Bech32
2
+
3
+ Bech32 encoding and decoding.
4
+
5
+ ## Constructors
6
+
7
+ ### new Bech32()
8
+
9
+ > **new Bech32**(): [`Bech32`](Bech32.md)
10
+
11
+ #### Returns
12
+
13
+ [`Bech32`](Bech32.md)
14
+
15
+ ## Methods
16
+
17
+ ### encode()
18
+
19
+ > `static` **encode**(`humanReadablePart`, `data`): `string`
20
+
21
+ Encode the buffer.
22
+
23
+ #### Parameters
24
+
25
+ • **humanReadablePart**: `string`
26
+
27
+ The header.
28
+
29
+ • **data**: `Uint8Array`
30
+
31
+ The data to encode.
32
+
33
+ #### Returns
34
+
35
+ `string`
36
+
37
+ The encoded data.
38
+
39
+ ***
40
+
41
+ ### decode()
42
+
43
+ > `static` **decode**(`bech`): `object`
44
+
45
+ Decode a bech32 string.
46
+
47
+ #### Parameters
48
+
49
+ • **bech**: `string`
50
+
51
+ The text to decode.
52
+
53
+ #### Returns
54
+
55
+ `object`
56
+
57
+ The decoded data or undefined if it could not be decoded.
58
+
59
+ ##### humanReadablePart
60
+
61
+ > **humanReadablePart**: `string`
62
+
63
+ ##### data
64
+
65
+ > **data**: `Uint8Array`
66
+
67
+ #### Throws
68
+
69
+ An error if the decoding fails.
70
+
71
+ ***
72
+
73
+ ### isBech32()
74
+
75
+ > `static` **isBech32**(`bech`): `bech is string`
76
+
77
+ Is the input a bech 32 address.
78
+
79
+ #### Parameters
80
+
81
+ • **bech**: `unknown`
82
+
83
+ The value to test.
84
+
85
+ #### Returns
86
+
87
+ `bech is string`
88
+
89
+ True if this is potentially a match.
@@ -0,0 +1,117 @@
1
+ # Class: Bip32Path
2
+
3
+ Class to help with bip32 paths.
4
+
5
+ ## Constructors
6
+
7
+ ### new Bip32Path()
8
+
9
+ > **new Bip32Path**(`initialPath`?): [`Bip32Path`](Bip32Path.md)
10
+
11
+ Create a new instance of Bip32Path.
12
+
13
+ #### Parameters
14
+
15
+ • **initialPath?**: `string`
16
+
17
+ Initial path to create.
18
+
19
+ #### Returns
20
+
21
+ [`Bip32Path`](Bip32Path.md)
22
+
23
+ ## Methods
24
+
25
+ ### fromPath()
26
+
27
+ > `static` **fromPath**(`bip32Path`): [`Bip32Path`](Bip32Path.md)
28
+
29
+ Construct a new path by cloning an existing one.
30
+
31
+ #### Parameters
32
+
33
+ • **bip32Path**: [`Bip32Path`](Bip32Path.md)
34
+
35
+ The path to clone.
36
+
37
+ #### Returns
38
+
39
+ [`Bip32Path`](Bip32Path.md)
40
+
41
+ A new instance of Bip32Path.
42
+
43
+ ***
44
+
45
+ ### toString()
46
+
47
+ > **toString**(): `string`
48
+
49
+ Converts the path to a string.
50
+
51
+ #### Returns
52
+
53
+ `string`
54
+
55
+ The path as a string.
56
+
57
+ ***
58
+
59
+ ### push()
60
+
61
+ > **push**(`index`): `void`
62
+
63
+ Push a new index on to the path.
64
+
65
+ #### Parameters
66
+
67
+ • **index**: `number`
68
+
69
+ The index to add to the path.
70
+
71
+ #### Returns
72
+
73
+ `void`
74
+
75
+ ***
76
+
77
+ ### pushHardened()
78
+
79
+ > **pushHardened**(`index`): `void`
80
+
81
+ Push a new hardened index on to the path.
82
+
83
+ #### Parameters
84
+
85
+ • **index**: `number`
86
+
87
+ The index to add to the path.
88
+
89
+ #### Returns
90
+
91
+ `void`
92
+
93
+ ***
94
+
95
+ ### pop()
96
+
97
+ > **pop**(): `void`
98
+
99
+ Pop an index from the path.
100
+
101
+ #### Returns
102
+
103
+ `void`
104
+
105
+ ***
106
+
107
+ ### numberSegments()
108
+
109
+ > **numberSegments**(): `number`[]
110
+
111
+ Get the segments.
112
+
113
+ #### Returns
114
+
115
+ `number`[]
116
+
117
+ The segments as numbers.
@@ -0,0 +1,121 @@
1
+ # Class: Bip39
2
+
3
+ Implementation of Bip39 for mnemonic generation.
4
+
5
+ ## Constructors
6
+
7
+ ### new Bip39()
8
+
9
+ > **new Bip39**(): [`Bip39`](Bip39.md)
10
+
11
+ #### Returns
12
+
13
+ [`Bip39`](Bip39.md)
14
+
15
+ ## Methods
16
+
17
+ ### randomMnemonic()
18
+
19
+ > `static` **randomMnemonic**(`strength`, `words`): `string`
20
+
21
+ Generate a random mnemonic.
22
+
23
+ #### Parameters
24
+
25
+ • **strength**: `number` = `256`
26
+
27
+ The strength of the mnemonic to generate, defaults to 256.
28
+
29
+ • **words**: `string`[] = `wordlist`
30
+
31
+ The wordlist to use, defaults to the English wordlist.
32
+
33
+ #### Returns
34
+
35
+ `string`
36
+
37
+ The random mnemonic.
38
+
39
+ #### Throws
40
+
41
+ Error if the length is not a multiple of 32.
42
+
43
+ ***
44
+
45
+ ### entropyToMnemonic()
46
+
47
+ > `static` **entropyToMnemonic**(`entropy`, `words`): `string`
48
+
49
+ Generate a mnemonic from the entropy.
50
+
51
+ #### Parameters
52
+
53
+ • **entropy**: `Uint8Array`
54
+
55
+ The entropy to generate.
56
+
57
+ • **words**: `string`[] = `wordlist`
58
+
59
+ The wordlist to use, defaults to the English wordlist.
60
+
61
+ #### Returns
62
+
63
+ `string`
64
+
65
+ The mnemonic.
66
+
67
+ #### Throws
68
+
69
+ Error if the length of the entropy is not a multiple of 4, or is less than 16 or greater than 32.
70
+
71
+ ***
72
+
73
+ ### mnemonicToSeed()
74
+
75
+ > `static` **mnemonicToSeed**(`mnemonic`, `password`?): `Uint8Array`
76
+
77
+ Convert a mnemonic to a seed.
78
+
79
+ #### Parameters
80
+
81
+ • **mnemonic**: `string`
82
+
83
+ The mnemonic to convert.
84
+
85
+ • **password?**: `string`
86
+
87
+ The password to apply to the seed generation.
88
+
89
+ #### Returns
90
+
91
+ `Uint8Array`
92
+
93
+ The seed.
94
+
95
+ ***
96
+
97
+ ### mnemonicToEntropy()
98
+
99
+ > `static` **mnemonicToEntropy**(`mnemonic`, `words`): `Uint8Array`
100
+
101
+ Convert the mnemonic back to entropy.
102
+
103
+ #### Parameters
104
+
105
+ • **mnemonic**: `string`
106
+
107
+ The mnemonic to convert.
108
+
109
+ • **words**: `string`[] = `wordlist`
110
+
111
+ The wordlist to use, defaults to the English wordlist.
112
+
113
+ #### Returns
114
+
115
+ `Uint8Array`
116
+
117
+ The entropy.
118
+
119
+ #### Throws
120
+
121
+ Error if the number of words is not a multiple of 3.
@@ -0,0 +1,173 @@
1
+ # Class: Bip44
2
+
3
+ Implementation of Bip44 for address generation.
4
+
5
+ ## Constructors
6
+
7
+ ### new Bip44()
8
+
9
+ > **new Bip44**(): [`Bip44`](Bip44.md)
10
+
11
+ #### Returns
12
+
13
+ [`Bip44`](Bip44.md)
14
+
15
+ ## Methods
16
+
17
+ ### keyPair()
18
+
19
+ > `static` **keyPair**(`seed`, `keyType`, `coinType`, `accountIndex`, `isInternal`, `addressIndex`): `object`
20
+
21
+ Generate a bip44 key pair from the seed and parts.
22
+
23
+ #### Parameters
24
+
25
+ • **seed**: `Uint8Array`
26
+
27
+ The account seed.
28
+
29
+ • **keyType**: [`KeyType`](../type-aliases/KeyType.md)
30
+
31
+ The key type.
32
+
33
+ • **coinType**: `number`
34
+
35
+ The coin type.
36
+
37
+ • **accountIndex**: `number`
38
+
39
+ The account index.
40
+
41
+ • **isInternal**: `boolean`
42
+
43
+ Is this an internal address.
44
+
45
+ • **addressIndex**: `number`
46
+
47
+ The address index.
48
+
49
+ #### Returns
50
+
51
+ `object`
52
+
53
+ The key pair.
54
+
55
+ ##### privateKey
56
+
57
+ > **privateKey**: `Uint8Array`
58
+
59
+ ##### publicKey
60
+
61
+ > **publicKey**: `Uint8Array`
62
+
63
+ #### Throws
64
+
65
+ Error if the address type is not supported.
66
+
67
+ ***
68
+
69
+ ### path()
70
+
71
+ > `static` **path**(`coinType`, `accountIndex`, `isInternal`, `addressIndex`): [`Bip32Path`](Bip32Path.md)
72
+
73
+ Generate a bip44 path based on all its parts.
74
+
75
+ #### Parameters
76
+
77
+ • **coinType**: `number`
78
+
79
+ The coin type.
80
+
81
+ • **accountIndex**: `number`
82
+
83
+ The account index.
84
+
85
+ • **isInternal**: `boolean`
86
+
87
+ Is this an internal address.
88
+
89
+ • **addressIndex**: `number`
90
+
91
+ The address index.
92
+
93
+ #### Returns
94
+
95
+ [`Bip32Path`](Bip32Path.md)
96
+
97
+ The generated path.
98
+
99
+ ***
100
+
101
+ ### basePath()
102
+
103
+ > `static` **basePath**(`coinType`): `string`
104
+
105
+ Create a bip44 base path for the provided coin type.
106
+
107
+ #### Parameters
108
+
109
+ • **coinType**: `number`
110
+
111
+ The coin type.
112
+
113
+ #### Returns
114
+
115
+ `string`
116
+
117
+ The bip44 address base path.
118
+
119
+ ***
120
+
121
+ ### addressBech32()
122
+
123
+ > `static` **addressBech32**(`seed`, `keyType`, `hrp`, `coinType`, `accountIndex`, `isInternal`, `addressIndex`): `object`
124
+
125
+ Generate a bech32 address from the seed and parts.
126
+
127
+ #### Parameters
128
+
129
+ • **seed**: `Uint8Array`
130
+
131
+ The account seed.
132
+
133
+ • **keyType**: [`KeyType`](../type-aliases/KeyType.md)
134
+
135
+ The key type.
136
+
137
+ • **hrp**: `string`
138
+
139
+ The human readable part of the address.
140
+
141
+ • **coinType**: `number`
142
+
143
+ The coin type.
144
+
145
+ • **accountIndex**: `number`
146
+
147
+ The account index.
148
+
149
+ • **isInternal**: `boolean`
150
+
151
+ Is this an internal address.
152
+
153
+ • **addressIndex**: `number`
154
+
155
+ The address index.
156
+
157
+ #### Returns
158
+
159
+ `object`
160
+
161
+ The generated path and the associated keypair.
162
+
163
+ ##### address
164
+
165
+ > **address**: `string`
166
+
167
+ ##### privateKey
168
+
169
+ > **privateKey**: `Uint8Array`
170
+
171
+ ##### publicKey
172
+
173
+ > **publicKey**: `Uint8Array`