@theqrl/wallet.js 0.1.2 → 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 (47) hide show
  1. package/README.md +195 -1
  2. package/dist/cjs/package.json +1 -0
  3. package/dist/cjs/wallet.js +4856 -0
  4. package/dist/mjs/package.json +1 -0
  5. package/dist/mjs/wallet.js +4841 -0
  6. package/package.json +22 -8
  7. package/src/index.js +33 -13
  8. package/src/qrl/wordlist.js +11 -5
  9. package/src/utils/bytes.js +59 -0
  10. package/src/wallet/common/address.js +94 -0
  11. package/src/wallet/common/constants.js +16 -0
  12. package/src/wallet/common/descriptor.js +70 -0
  13. package/src/wallet/common/seed.js +123 -0
  14. package/src/wallet/common/wallettype.js +21 -0
  15. package/src/wallet/factory.js +39 -0
  16. package/src/wallet/misc/mnemonic.js +77 -0
  17. package/src/wallet/ml_dsa_87/crypto.js +90 -0
  18. package/src/wallet/ml_dsa_87/descriptor.js +18 -0
  19. package/src/wallet/ml_dsa_87/wallet.js +158 -0
  20. package/types/index.d.ts +13 -0
  21. package/types/index.d.ts.map +1 -0
  22. package/types/qrl/wordlist.d.ts +11 -0
  23. package/types/qrl/wordlist.d.ts.map +1 -0
  24. package/types/utils/bytes.d.ts +27 -0
  25. package/types/utils/bytes.d.ts.map +1 -0
  26. package/types/wallet/common/address.d.ts +17 -0
  27. package/types/wallet/common/address.d.ts.map +1 -0
  28. package/types/wallet/common/constants.d.ts +13 -0
  29. package/types/wallet/common/constants.d.ts.map +1 -0
  30. package/types/wallet/common/descriptor.d.ts +32 -0
  31. package/types/wallet/common/descriptor.d.ts.map +1 -0
  32. package/types/wallet/common/seed.d.ts +67 -0
  33. package/types/wallet/common/seed.d.ts.map +1 -0
  34. package/types/wallet/common/wallettype.d.ts +19 -0
  35. package/types/wallet/common/wallettype.d.ts.map +1 -0
  36. package/types/wallet/factory.d.ts +9 -0
  37. package/types/wallet/factory.d.ts.map +1 -0
  38. package/types/wallet/misc/mnemonic.d.ts +13 -0
  39. package/types/wallet/misc/mnemonic.d.ts.map +1 -0
  40. package/types/wallet/ml_dsa_87/crypto.d.ts +24 -0
  41. package/types/wallet/ml_dsa_87/crypto.d.ts.map +1 -0
  42. package/types/wallet/ml_dsa_87/descriptor.d.ts +8 -0
  43. package/types/wallet/ml_dsa_87/descriptor.d.ts.map +1 -0
  44. package/types/wallet/ml_dsa_87/wallet.d.ts +74 -0
  45. package/types/wallet/ml_dsa_87/wallet.d.ts.map +1 -0
  46. package/src/dilithium.js +0 -158
  47. package/src/utils/mnemonic.js +0 -93
package/src/dilithium.js DELETED
@@ -1,158 +0,0 @@
1
- const { SHAKE } = require('sha3');
2
- const randomBytes = require('randombytes');
3
-
4
- const {
5
- cryptoSign,
6
- cryptoSignKeypair,
7
- cryptoSignOpen,
8
- cryptoSignVerify,
9
- // cryptoSignSignature,
10
- CryptoPublicKeyBytes,
11
- CryptoSecretKeyBytes,
12
- // SeedBytes,
13
- CryptoBytes,
14
- } = require('@theqrl/dilithium5');
15
- const { SeedBinToMnemonic } = require('./utils/mnemonic.js');
16
-
17
- function getDilithiumDescriptor(address) {
18
- /*
19
- In case of Dilithium address, it doesn't have any choice of hashFunction,
20
- height, addrFormatType. Thus keeping all those values to 0 and assigning
21
- only signatureType in the descriptor.
22
- */
23
- if (!address) {
24
- throw new Error('Address is not defined');
25
- }
26
- return 2 << 4;
27
- }
28
-
29
- function getDilithiumAddressFromPK(pk) {
30
- const addressSize = 20;
31
- const address = new Uint8Array(addressSize);
32
- const descBytes = getDilithiumDescriptor(address);
33
- address[0] = descBytes;
34
- const hashedKey = new SHAKE(256);
35
- hashedKey.update(Buffer.from(pk));
36
- let hashedKeyDigest = hashedKey.digest({ buffer: Buffer.alloc(32), encoding: 'hex' });
37
- hashedKeyDigest = hashedKeyDigest.slice(hashedKeyDigest.length - addressSize + 1);
38
- for (let i = 0; i < hashedKeyDigest.length; i++) {
39
- address[i + 1] = hashedKeyDigest[i];
40
- }
41
- return address;
42
- }
43
-
44
- class Dilithium {
45
- constructor(seed = null) {
46
- this.pk = null;
47
- this.sk = null;
48
- this.seed = seed;
49
- this.randomizedSigning = false;
50
- if (this.seed === null) {
51
- this.create();
52
- } else {
53
- this.fromSeed();
54
- }
55
- }
56
-
57
- create() {
58
- const pk = new Uint8Array(CryptoPublicKeyBytes);
59
- const sk = new Uint8Array(CryptoSecretKeyBytes);
60
- const seed = randomBytes(48);
61
- const hashedSeed = new SHAKE(256);
62
- hashedSeed.update(seed);
63
- const seedBuf = hashedSeed.digest({ buffer: Buffer.alloc(32) });
64
- cryptoSignKeypair(seedBuf, pk, sk);
65
- this.pk = pk;
66
- this.sk = sk;
67
- this.seed = seed;
68
- }
69
-
70
- fromSeed() {
71
- const pk = new Uint8Array(CryptoPublicKeyBytes);
72
- const sk = new Uint8Array(CryptoSecretKeyBytes);
73
- const hashedSeed = new SHAKE(256);
74
- hashedSeed.update(this.seed);
75
- const seedBuf = hashedSeed.digest({ buffer: Buffer.alloc(32) });
76
- cryptoSignKeypair(seedBuf, pk, sk);
77
- this.pk = pk;
78
- this.sk = sk;
79
- }
80
-
81
- getPK() {
82
- return this.pk;
83
- }
84
-
85
- getSK() {
86
- return this.sk;
87
- }
88
-
89
- getSeed() {
90
- return this.seed;
91
- }
92
-
93
- getHexSeed() {
94
- return `0x${this.seed.toString('hex')}`;
95
- }
96
-
97
- getMnemonic() {
98
- return SeedBinToMnemonic(this.seed);
99
- }
100
-
101
- getAddress() {
102
- return getDilithiumAddressFromPK(this.pk);
103
- }
104
-
105
- // Seal the message, returns signature attached with message.
106
- seal(message) {
107
- return cryptoSign(message, this.sk, this.randomizedSigning);
108
- }
109
-
110
- // Sign the message, and return a detached signature. Detached signatures are
111
- // variable sized, but never larger than SIG_SIZE_PACKED.
112
- sign(message) {
113
- const sm = cryptoSign(message, this.sk);
114
- let signature = new Uint8Array(CryptoBytes);
115
- signature = sm.slice(0, CryptoBytes);
116
- return signature;
117
- }
118
- }
119
-
120
- // Open the sealed message m. Returns the original message sealed with signature.
121
- // In case the signature is invalid, nil is returned.
122
- function openMessage(signatureMessage, pk) {
123
- return cryptoSignOpen(signatureMessage, pk);
124
- }
125
-
126
- function verifyMessage(message, signature, pk) {
127
- return cryptoSignVerify(signature, message, pk);
128
- }
129
-
130
- // ExtractMessage extracts message from Signature attached with message.
131
- function extractMessage(signatureMessage) {
132
- return signatureMessage.slice(CryptoBytes, signatureMessage.length);
133
- }
134
-
135
- // ExtractSignature extracts signature from Signature attached with message.
136
- function extractSignature(signatureMessage) {
137
- return signatureMessage.slice(0, CryptoBytes);
138
- }
139
-
140
- function isValidDilithiumAddress(address) {
141
- const d = getDilithiumDescriptor(address);
142
- if (address[0] !== d) {
143
- return false;
144
- }
145
- // TODO: Add checksum
146
- return true;
147
- }
148
-
149
- module.exports = {
150
- Dilithium,
151
- getDilithiumAddressFromPK,
152
- getDilithiumDescriptor,
153
- openMessage,
154
- verifyMessage,
155
- extractMessage,
156
- extractSignature,
157
- isValidDilithiumAddress,
158
- };
@@ -1,93 +0,0 @@
1
- const { WordList } = require('../qrl/wordlist.js');
2
-
3
- function binToMnemonic(input) {
4
- if (input.length % 3 !== 0) {
5
- throw new Error('byte count needs to be a multiple of 3');
6
- }
7
- let mnemonic = '';
8
- let separator = '';
9
- for (let nibble = 0; nibble < input.length * 2; nibble += 3) {
10
- const p = nibble >> 1;
11
- const b1 = input[p];
12
- let b2 = 0;
13
- if (p + 1 < input.length) {
14
- b2 = input[p + 1];
15
- }
16
- let idx = 0;
17
- if (nibble % 2 === 0) {
18
- idx = (b1 << 4) + (b2 >> 4);
19
- } else {
20
- idx = ((b1 & 0x0f) << 8) + b2;
21
- }
22
- mnemonic += separator + WordList[idx];
23
- separator = ' ';
24
- }
25
- return mnemonic;
26
- }
27
-
28
- function SeedBinToMnemonic(input) {
29
- return binToMnemonic(input);
30
- }
31
-
32
- function mnemonicToBin(mnemonic) {
33
- const mnemonicWords = mnemonic.split(' ');
34
- const wordCount = mnemonicWords.length;
35
- if (wordCount % 2 !== 0) {
36
- throw new Error('word count must be even');
37
- }
38
-
39
- const wordLookup = {};
40
- WordList.map((word, i) => {
41
- wordLookup[word] = i;
42
- return word;
43
- });
44
-
45
- const result = new Uint8Array((wordCount * 15) / 10);
46
-
47
- let current = 0;
48
- let buffering = 0;
49
- let resultIndex = 0;
50
-
51
- mnemonicWords.map((w) => {
52
- const value = wordLookup[w];
53
- if (value === undefined || value === null) {
54
- throw new Error('invalid word in mnemonic');
55
- }
56
-
57
- buffering += 3;
58
- current = (current << 12) + value;
59
- let shift;
60
- let mask;
61
- let tmp;
62
- for (; buffering > 2; ) {
63
- shift = 4 * (buffering - 2);
64
- mask = (1 << shift) - 1;
65
- tmp = current >> shift;
66
- buffering -= 2;
67
- current &= mask;
68
- result[resultIndex] = tmp;
69
- resultIndex++;
70
- }
71
- return w;
72
- });
73
-
74
- if (buffering > 0) {
75
- result[resultIndex] = current & 0xff;
76
- resultIndex++;
77
- }
78
- return result;
79
- }
80
-
81
- function MnemonicToSeedBin(mnemonic) {
82
- const output = mnemonicToBin(mnemonic);
83
-
84
- if (output.length !== 48) {
85
- throw new Error('unexpected MnemonicToSeedBin output size');
86
- }
87
-
88
- const sizedOutput = new Uint8Array(48);
89
- sizedOutput.set(output);
90
- return output;
91
- }
92
-
93
- module.exports = { SeedBinToMnemonic, MnemonicToSeedBin };