@ocap/mcrypto 1.16.16 → 1.16.17
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/lib/crypter/aes.d.ts +8 -0
- package/lib/crypter/aes.js +51 -28
- package/lib/encode.d.ts +2 -2
- package/lib/encode.js +22 -25
- package/lib/hasher/keccak.d.ts +13 -0
- package/lib/hasher/keccak.js +36 -78
- package/lib/hasher/sha2.d.ts +13 -0
- package/lib/hasher/sha2.js +40 -82
- package/lib/hasher/sha3.d.ts +13 -0
- package/lib/hasher/sha3.js +36 -78
- package/lib/index.d.ts +203 -3
- package/lib/index.js +171 -201
- package/lib/protocols/crypter.d.ts +2 -0
- package/lib/protocols/crypter.js +9 -3
- package/lib/protocols/hasher.d.ts +2 -0
- package/lib/protocols/hasher.js +9 -3
- package/lib/protocols/signer.d.ts +2 -0
- package/lib/protocols/signer.js +9 -3
- package/lib/signer/ed25519.d.ts +53 -0
- package/lib/signer/ed25519.js +81 -86
- package/lib/signer/ethereum.d.ts +17 -0
- package/lib/signer/ethereum.js +35 -34
- package/lib/signer/secp256k1.d.ts +39 -0
- package/lib/signer/secp256k1.js +82 -107
- package/package.json +23 -16
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,203 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { BytesType, EncodingType, KeyPairType } from '@ocap/util';
|
|
2
|
+
export declare type KeyType = 'ED25519' | 'SECP256K1' | 'ETHEREUM';
|
|
3
|
+
export declare type HashType = 'KECCAK' | 'SHA3' | 'KECCAK_384' | 'SHA3_384' | 'KECCAK_512' | 'SHA3_512' | 'SHA2';
|
|
4
|
+
export declare type RoleType = 'ROLE_ACCOUNT' | 'ROLE_NODE' | 'ROLE_DEVICE' | 'ROLE_APPLICATION' | 'ROLE_SMART_CONTRACT' | 'ROLE_BOT' | 'ROLE_ASSET' | 'ROLE_STAKE' | 'ROLE_VALIDATOR' | 'ROLE_GROUP' | 'ROLE_GROUP' | 'ROLE_TX' | 'ROLE_TETHER' | 'ROLE_SWAP' | 'ROLE_DELEGATION' | 'ROLE_VC' | 'ROLE_BLOCKLET' | 'ROLE_REGISTRY' | 'ROLE_TOKEN' | 'ROLE_FACTORY' | 'ROLE_ROLLUP' | 'ROLE_ANY';
|
|
5
|
+
export declare type AddressType = 'BASE16' | 'BASE58';
|
|
6
|
+
export declare type HasherType = (data: BytesType, round: number, encoding?: EncodingType) => BytesType;
|
|
7
|
+
export declare type SignerType = {
|
|
8
|
+
genKeyPair: (encoding?: EncodingType, seed?: BytesType) => KeyPairType;
|
|
9
|
+
getPublicKey: (sk: BytesType, encoding?: EncodingType) => BytesType;
|
|
10
|
+
sign: (data: BytesType, sk: BytesType, encoding?: EncodingType) => BytesType;
|
|
11
|
+
verify: (data: BytesType, pk: BytesType, signature: BytesType) => boolean;
|
|
12
|
+
ethHash?: (data: string) => string;
|
|
13
|
+
ethSign?: (data: BytesType, sk: BytesType) => string;
|
|
14
|
+
ethVerify?: (data: BytesType, pk: BytesType, signature: BytesType) => boolean;
|
|
15
|
+
ethRecover?: (hash: BytesType, signature: BytesType) => string;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Contains all supported signers, eg: `Ed25519` and `Secp256k1`
|
|
19
|
+
*
|
|
20
|
+
* @readonly
|
|
21
|
+
* @type {object}
|
|
22
|
+
* @name Signer
|
|
23
|
+
* @static
|
|
24
|
+
* @example
|
|
25
|
+
* const { Signer } = require('@ocap/mcrypto');
|
|
26
|
+
* const message = 'some message to sign';
|
|
27
|
+
*
|
|
28
|
+
* // Use Signer directly
|
|
29
|
+
* const keyPair = Signer.Ed25519.genKeyPair();
|
|
30
|
+
* const signature = Signer.Ed25519.sign(message, keyPair.secretKey);
|
|
31
|
+
* const result = Signer.Ed25519.verify(message, signature, keyPair.publicKey);
|
|
32
|
+
* assert.ok(result);
|
|
33
|
+
*/
|
|
34
|
+
export declare const Signer: {
|
|
35
|
+
Ed25519: import("./signer/ed25519").Ed25519Signer;
|
|
36
|
+
Secp256k1: import("./signer/secp256k1").Secp256k1Signer;
|
|
37
|
+
Ethereum: import("./signer/ethereum").EthereumSigner;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Contains all supported hasher, eg: `SHA2`,`SHA3` and `Keccak`, each of them supports `hash224`, `hash256`, `hash384`, `hash512`
|
|
41
|
+
*
|
|
42
|
+
* @readonly
|
|
43
|
+
* @type {object}
|
|
44
|
+
* @name Hasher
|
|
45
|
+
* @static
|
|
46
|
+
* @example
|
|
47
|
+
* const { Hasher } = require('@ocap/mcrypto');
|
|
48
|
+
*
|
|
49
|
+
* const message = 'message to hash';
|
|
50
|
+
* const hash = Hasher.SHA2.hash256(message);
|
|
51
|
+
*/
|
|
52
|
+
export declare const Hasher: {
|
|
53
|
+
SHA2: import("./hasher/sha2").Sha2Hasher;
|
|
54
|
+
SHA3: import("./hasher/sha3").Sha3Hasher;
|
|
55
|
+
Keccak: import("./hasher/keccak").KeccakHasher;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Contains type constants that represent can be used to compose different crypto method, each crypto method consist one of:
|
|
59
|
+
* FIXME: enum definition of forge-abi and abt-did-elixir are not exactly the same
|
|
60
|
+
*
|
|
61
|
+
* @readonly
|
|
62
|
+
* @type {object}
|
|
63
|
+
* @name types
|
|
64
|
+
* @static
|
|
65
|
+
* @example
|
|
66
|
+
* const { types } = require('@ocap/mcrypto');
|
|
67
|
+
*
|
|
68
|
+
* // types.RoleType.ROLE_ACCOUNT
|
|
69
|
+
* // types.KeyType.ED25519
|
|
70
|
+
* // types.HashType.SHA3
|
|
71
|
+
* // types.EncodingType.BASE58
|
|
72
|
+
*/
|
|
73
|
+
export declare const types: {
|
|
74
|
+
/**
|
|
75
|
+
* Key-pair derivation algorithms
|
|
76
|
+
*
|
|
77
|
+
* @readonly
|
|
78
|
+
* @enum {number}
|
|
79
|
+
* @name types.KeyType
|
|
80
|
+
* @memberof types
|
|
81
|
+
* @static
|
|
82
|
+
*/
|
|
83
|
+
KeyType: {
|
|
84
|
+
ED25519: number;
|
|
85
|
+
SECP256K1: number;
|
|
86
|
+
ETHEREUM: number;
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Hashing algorithms
|
|
90
|
+
*
|
|
91
|
+
* @readonly
|
|
92
|
+
* @enum {number}
|
|
93
|
+
* @name types.HashType
|
|
94
|
+
* @memberof types
|
|
95
|
+
* @static
|
|
96
|
+
*/
|
|
97
|
+
HashType: {
|
|
98
|
+
KECCAK: number;
|
|
99
|
+
SHA3: number;
|
|
100
|
+
KECCAK_384: number;
|
|
101
|
+
SHA3_384: number;
|
|
102
|
+
KECCAK_512: number;
|
|
103
|
+
SHA3_512: number;
|
|
104
|
+
SHA2: number;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* DID wallet role type
|
|
108
|
+
*
|
|
109
|
+
* @readonly
|
|
110
|
+
* @enum {number}
|
|
111
|
+
* @name types.RoleType
|
|
112
|
+
* @memberof types
|
|
113
|
+
* @static
|
|
114
|
+
*/
|
|
115
|
+
RoleType: {
|
|
116
|
+
ROLE_ACCOUNT: number;
|
|
117
|
+
ROLE_NODE: number;
|
|
118
|
+
ROLE_DEVICE: number;
|
|
119
|
+
ROLE_APPLICATION: number;
|
|
120
|
+
ROLE_SMART_CONTRACT: number;
|
|
121
|
+
ROLE_BOT: number;
|
|
122
|
+
ROLE_ASSET: number;
|
|
123
|
+
ROLE_STAKE: number;
|
|
124
|
+
ROLE_VALIDATOR: number;
|
|
125
|
+
ROLE_GROUP: number;
|
|
126
|
+
ROLE_TX: number;
|
|
127
|
+
ROLE_TETHER: number;
|
|
128
|
+
ROLE_SWAP: number;
|
|
129
|
+
ROLE_DELEGATION: number;
|
|
130
|
+
ROLE_VC: number;
|
|
131
|
+
ROLE_BLOCKLET: number;
|
|
132
|
+
ROLE_REGISTRY: number;
|
|
133
|
+
ROLE_TOKEN: number;
|
|
134
|
+
ROLE_FACTORY: number;
|
|
135
|
+
ROLE_ROLLUP: number;
|
|
136
|
+
ROLE_ANY: number;
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* Address encoding algorithm, defaults to `base58btc`
|
|
140
|
+
*
|
|
141
|
+
* @readonly
|
|
142
|
+
* @enum {number}
|
|
143
|
+
* @name types.RoleType
|
|
144
|
+
* @memberof types
|
|
145
|
+
* @static
|
|
146
|
+
*/
|
|
147
|
+
EncodingType: {
|
|
148
|
+
BASE16: number;
|
|
149
|
+
BASE58: number;
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* Get signer instance
|
|
154
|
+
*
|
|
155
|
+
* @function
|
|
156
|
+
* @param {number} type - algorithm used to derive key pair, possible values are
|
|
157
|
+
* - types.KeyType.ED25519
|
|
158
|
+
* - types.KeyType.SECP256k1
|
|
159
|
+
* - types.KeyType.ETHEREUM
|
|
160
|
+
* @returns {object} signer instance
|
|
161
|
+
* @example
|
|
162
|
+
* const { getSigner, types } = require('@ocap/mcrypto');
|
|
163
|
+
* const message = 'some message to sign';
|
|
164
|
+
*
|
|
165
|
+
* const signer = getSigner(types.KeyType.ED25519);
|
|
166
|
+
* const keyPair1 = signer.genKeyPair();
|
|
167
|
+
* const signature1 = signer.sign(message, keyPair1.secretKey);
|
|
168
|
+
* const result1 = signer.verify(message, signature1, keyPair1.publicKey);
|
|
169
|
+
* assert.ok(result1);
|
|
170
|
+
*/
|
|
171
|
+
export declare function getSigner(type: number): SignerType;
|
|
172
|
+
/**
|
|
173
|
+
* Get hasher instance
|
|
174
|
+
*
|
|
175
|
+
* @function
|
|
176
|
+
* @param {number} type - algorithm used to hash data, possible values
|
|
177
|
+
* - types.HashType.KECCAK
|
|
178
|
+
* - types.HashType.KECCAK_384
|
|
179
|
+
* - types.HashType.KECCAK_512
|
|
180
|
+
* - types.HashType.SHA3
|
|
181
|
+
* - types.HashType.SHA3_384
|
|
182
|
+
* - types.HashType.SHA3_512
|
|
183
|
+
* @returns {object} hasher instance
|
|
184
|
+
* @example
|
|
185
|
+
* const { getHasher, types } = require('@ocap/mcrypto');
|
|
186
|
+
*
|
|
187
|
+
* const hashFn = getHasher(types.HashType.SHA3);
|
|
188
|
+
* const hash2 = hashFn(message);
|
|
189
|
+
*/
|
|
190
|
+
export declare function getHasher(type: number): HasherType;
|
|
191
|
+
/**
|
|
192
|
+
* Get random bytes in specified encoding
|
|
193
|
+
*
|
|
194
|
+
* @param {number} [length=32]
|
|
195
|
+
* @returns {buffer|hex|uint8|base64}
|
|
196
|
+
*/
|
|
197
|
+
export declare function getRandomBytes(length?: number, encoding?: EncodingType): BytesType;
|
|
198
|
+
export declare const Signers: Readonly<{
|
|
199
|
+
[x: number]: import("./signer/ed25519").Ed25519Signer | import("./signer/secp256k1").Secp256k1Signer;
|
|
200
|
+
}>;
|
|
201
|
+
export declare const Hashers: Readonly<{
|
|
202
|
+
[x: number]: (data: BytesType, round?: number, encoding?: EncodingType) => BytesType;
|
|
203
|
+
}>;
|
package/lib/index.js
CHANGED
|
@@ -1,96 +1,74 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Hashers = exports.Signers = exports.getRandomBytes = exports.getHasher = exports.getSigner = exports.types = exports.Hasher = exports.Signer = void 0;
|
|
7
|
+
const randombytes_1 = __importDefault(require("randombytes"));
|
|
8
|
+
const encode_1 = __importDefault(require("./encode"));
|
|
9
|
+
const keccak_1 = __importDefault(require("./hasher/keccak"));
|
|
10
|
+
const sha2_1 = __importDefault(require("./hasher/sha2"));
|
|
11
|
+
const sha3_1 = __importDefault(require("./hasher/sha3"));
|
|
12
|
+
const ed25519_1 = __importDefault(require("./signer/ed25519"));
|
|
13
|
+
const ethereum_1 = __importDefault(require("./signer/ethereum"));
|
|
14
|
+
const secp256k1_1 = __importDefault(require("./signer/secp256k1"));
|
|
3
15
|
/**
|
|
4
|
-
*
|
|
5
|
-
* Just a wrapper around existing javascript crypto libraries, implementation details can be found:
|
|
16
|
+
* Contains all supported signers, eg: `Ed25519` and `Secp256k1`
|
|
6
17
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
18
|
+
* @readonly
|
|
19
|
+
* @type {object}
|
|
20
|
+
* @name Signer
|
|
21
|
+
* @static
|
|
22
|
+
* @example
|
|
23
|
+
* const { Signer } = require('@ocap/mcrypto');
|
|
24
|
+
* const message = 'some message to sign';
|
|
25
|
+
*
|
|
26
|
+
* // Use Signer directly
|
|
27
|
+
* const keyPair = Signer.Ed25519.genKeyPair();
|
|
28
|
+
* const signature = Signer.Ed25519.sign(message, keyPair.secretKey);
|
|
29
|
+
* const result = Signer.Ed25519.verify(message, signature, keyPair.publicKey);
|
|
30
|
+
* assert.ok(result);
|
|
31
|
+
*/
|
|
32
|
+
exports.Signer = {
|
|
33
|
+
Ed25519: ed25519_1.default,
|
|
34
|
+
Secp256k1: secp256k1_1.default,
|
|
35
|
+
Ethereum: ethereum_1.default,
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Contains all supported hasher, eg: `SHA2`,`SHA3` and `Keccak`, each of them supports `hash224`, `hash256`, `hash384`, `hash512`
|
|
39
|
+
*
|
|
40
|
+
* @readonly
|
|
41
|
+
* @type {object}
|
|
42
|
+
* @name Hasher
|
|
43
|
+
* @static
|
|
44
|
+
* @example
|
|
45
|
+
* const { Hasher } = require('@ocap/mcrypto');
|
|
15
46
|
*
|
|
16
|
-
*
|
|
47
|
+
* const message = 'message to hash';
|
|
48
|
+
* const hash = Hasher.SHA2.hash256(message);
|
|
49
|
+
*/
|
|
50
|
+
exports.Hasher = {
|
|
51
|
+
SHA2: sha2_1.default,
|
|
52
|
+
SHA3: sha3_1.default,
|
|
53
|
+
Keccak: keccak_1.default,
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Contains type constants that represent can be used to compose different crypto method, each crypto method consist one of:
|
|
57
|
+
* FIXME: enum definition of forge-abi and abt-did-elixir are not exactly the same
|
|
58
|
+
*
|
|
59
|
+
* @readonly
|
|
60
|
+
* @type {object}
|
|
61
|
+
* @name types
|
|
62
|
+
* @static
|
|
17
63
|
* @example
|
|
18
|
-
|
|
64
|
+
* const { types } = require('@ocap/mcrypto');
|
|
65
|
+
*
|
|
66
|
+
* // types.RoleType.ROLE_ACCOUNT
|
|
67
|
+
* // types.KeyType.ED25519
|
|
68
|
+
* // types.HashType.SHA3
|
|
69
|
+
* // types.EncodingType.BASE58
|
|
19
70
|
*/
|
|
20
|
-
|
|
21
|
-
const randomBytes = require('randombytes');
|
|
22
|
-
const encode = require('./encode');
|
|
23
|
-
|
|
24
|
-
const Mcrypto = (module.exports = {
|
|
25
|
-
/**
|
|
26
|
-
* Contains all supported signers, eg: `Ed25519` and `Secp256k1`
|
|
27
|
-
*
|
|
28
|
-
* @readonly
|
|
29
|
-
* @type {object}
|
|
30
|
-
* @name Signer
|
|
31
|
-
* @static
|
|
32
|
-
* @example
|
|
33
|
-
* const { Signer } = require('@ocap/mcrypto');
|
|
34
|
-
* const message = 'some message to sign';
|
|
35
|
-
*
|
|
36
|
-
* // Use Signer directly
|
|
37
|
-
* const keyPair = Signer.Ed25519.genKeyPair();
|
|
38
|
-
* const signature = Signer.Ed25519.sign(message, keyPair.secretKey);
|
|
39
|
-
* const result = Signer.Ed25519.verify(message, signature, keyPair.publicKey);
|
|
40
|
-
* assert.ok(result);
|
|
41
|
-
*/
|
|
42
|
-
Signer: {
|
|
43
|
-
Ed25519: require('./signer/ed25519'),
|
|
44
|
-
Secp256k1: require('./signer/secp256k1'),
|
|
45
|
-
Ethereum: require('./signer/ethereum'),
|
|
46
|
-
},
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Contains all supported hasher, eg: `SHA2`,`SHA3` and `Keccak`, each of them supports `hash224`, `hash256`, `hash384`, `hash512`
|
|
50
|
-
*
|
|
51
|
-
* @readonly
|
|
52
|
-
* @type {object}
|
|
53
|
-
* @name Hasher
|
|
54
|
-
* @static
|
|
55
|
-
* @example
|
|
56
|
-
* const { Hasher } = require('@ocap/mcrypto');
|
|
57
|
-
*
|
|
58
|
-
* const message = 'message to hash';
|
|
59
|
-
* const hash = Hasher.SHA2.hash256(message);
|
|
60
|
-
*/
|
|
61
|
-
Hasher: {
|
|
62
|
-
SHA2: require('./hasher/sha2'),
|
|
63
|
-
SHA3: require('./hasher/sha3'),
|
|
64
|
-
Keccak: require('./hasher/keccak'),
|
|
65
|
-
},
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Contains all supported crypter, eg: `AES`, each of them supports `encrypt`, `decrypt`
|
|
69
|
-
*
|
|
70
|
-
* @name Crypter
|
|
71
|
-
* @static
|
|
72
|
-
*/
|
|
73
|
-
// Crypter: {
|
|
74
|
-
// AES: require('./crypter/aes'),
|
|
75
|
-
// },
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Contains type constants that represent can be used to compose different crypto method, each crypto method consist one of:
|
|
79
|
-
* FIXME: enum definition of forge-abi and abt-did-elixir are not exactly the same
|
|
80
|
-
*
|
|
81
|
-
* @readonly
|
|
82
|
-
* @type {object}
|
|
83
|
-
* @name types
|
|
84
|
-
* @static
|
|
85
|
-
* @example
|
|
86
|
-
* const { types } = require('@ocap/mcrypto');
|
|
87
|
-
*
|
|
88
|
-
* // types.RoleType.ROLE_ACCOUNT
|
|
89
|
-
* // types.KeyType.ED25519
|
|
90
|
-
* // types.HashType.SHA3
|
|
91
|
-
* // types.EncodingType.BASE58
|
|
92
|
-
*/
|
|
93
|
-
types: {
|
|
71
|
+
exports.types = {
|
|
94
72
|
/**
|
|
95
73
|
* Key-pair derivation algorithms
|
|
96
74
|
*
|
|
@@ -101,11 +79,10 @@ const Mcrypto = (module.exports = {
|
|
|
101
79
|
* @static
|
|
102
80
|
*/
|
|
103
81
|
KeyType: {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
82
|
+
ED25519: 0,
|
|
83
|
+
SECP256K1: 1,
|
|
84
|
+
ETHEREUM: 2,
|
|
107
85
|
},
|
|
108
|
-
|
|
109
86
|
/**
|
|
110
87
|
* Hashing algorithms
|
|
111
88
|
*
|
|
@@ -116,15 +93,14 @@ const Mcrypto = (module.exports = {
|
|
|
116
93
|
* @static
|
|
117
94
|
*/
|
|
118
95
|
HashType: {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
96
|
+
KECCAK: 0,
|
|
97
|
+
SHA3: 1,
|
|
98
|
+
KECCAK_384: 2,
|
|
99
|
+
SHA3_384: 3,
|
|
100
|
+
KECCAK_512: 4,
|
|
101
|
+
SHA3_512: 5,
|
|
102
|
+
SHA2: 6,
|
|
126
103
|
},
|
|
127
|
-
|
|
128
104
|
/**
|
|
129
105
|
* DID wallet role type
|
|
130
106
|
*
|
|
@@ -135,29 +111,28 @@ const Mcrypto = (module.exports = {
|
|
|
135
111
|
* @static
|
|
136
112
|
*/
|
|
137
113
|
RoleType: {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
114
|
+
ROLE_ACCOUNT: 0,
|
|
115
|
+
ROLE_NODE: 1,
|
|
116
|
+
ROLE_DEVICE: 2,
|
|
117
|
+
ROLE_APPLICATION: 3,
|
|
118
|
+
ROLE_SMART_CONTRACT: 4,
|
|
119
|
+
ROLE_BOT: 5,
|
|
120
|
+
ROLE_ASSET: 6,
|
|
121
|
+
ROLE_STAKE: 7,
|
|
122
|
+
ROLE_VALIDATOR: 8,
|
|
123
|
+
ROLE_GROUP: 9,
|
|
124
|
+
ROLE_TX: 10,
|
|
125
|
+
ROLE_TETHER: 11,
|
|
126
|
+
ROLE_SWAP: 12,
|
|
127
|
+
ROLE_DELEGATION: 13,
|
|
128
|
+
ROLE_VC: 14,
|
|
129
|
+
ROLE_BLOCKLET: 15,
|
|
130
|
+
ROLE_REGISTRY: 16,
|
|
131
|
+
ROLE_TOKEN: 17,
|
|
132
|
+
ROLE_FACTORY: 18,
|
|
133
|
+
ROLE_ROLLUP: 19,
|
|
134
|
+
ROLE_ANY: 63,
|
|
159
135
|
},
|
|
160
|
-
|
|
161
136
|
/**
|
|
162
137
|
* Address encoding algorithm, defaults to `base58btc`
|
|
163
138
|
*
|
|
@@ -168,87 +143,82 @@ const Mcrypto = (module.exports = {
|
|
|
168
143
|
* @static
|
|
169
144
|
*/
|
|
170
145
|
EncodingType: {
|
|
171
|
-
|
|
172
|
-
|
|
146
|
+
BASE16: 0,
|
|
147
|
+
BASE58: 1,
|
|
173
148
|
},
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
throw new Error(`Unsupported signer type: ${type}`);
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* Get signer instance
|
|
152
|
+
*
|
|
153
|
+
* @function
|
|
154
|
+
* @param {number} type - algorithm used to derive key pair, possible values are
|
|
155
|
+
* - types.KeyType.ED25519
|
|
156
|
+
* - types.KeyType.SECP256k1
|
|
157
|
+
* - types.KeyType.ETHEREUM
|
|
158
|
+
* @returns {object} signer instance
|
|
159
|
+
* @example
|
|
160
|
+
* const { getSigner, types } = require('@ocap/mcrypto');
|
|
161
|
+
* const message = 'some message to sign';
|
|
162
|
+
*
|
|
163
|
+
* const signer = getSigner(types.KeyType.ED25519);
|
|
164
|
+
* const keyPair1 = signer.genKeyPair();
|
|
165
|
+
* const signature1 = signer.sign(message, keyPair1.secretKey);
|
|
166
|
+
* const result1 = signer.verify(message, signature1, keyPair1.publicKey);
|
|
167
|
+
* assert.ok(result1);
|
|
168
|
+
*/
|
|
169
|
+
function getSigner(type) {
|
|
170
|
+
if (typeof exports.Signers[type] === 'undefined') {
|
|
171
|
+
throw new Error(`Unsupported signer type: ${type}`);
|
|
198
172
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
throw new Error(`Unsupported hash type: ${type}`);
|
|
173
|
+
return exports.Signers[type];
|
|
174
|
+
}
|
|
175
|
+
exports.getSigner = getSigner;
|
|
176
|
+
/**
|
|
177
|
+
* Get hasher instance
|
|
178
|
+
*
|
|
179
|
+
* @function
|
|
180
|
+
* @param {number} type - algorithm used to hash data, possible values
|
|
181
|
+
* - types.HashType.KECCAK
|
|
182
|
+
* - types.HashType.KECCAK_384
|
|
183
|
+
* - types.HashType.KECCAK_512
|
|
184
|
+
* - types.HashType.SHA3
|
|
185
|
+
* - types.HashType.SHA3_384
|
|
186
|
+
* - types.HashType.SHA3_512
|
|
187
|
+
* @returns {object} hasher instance
|
|
188
|
+
* @example
|
|
189
|
+
* const { getHasher, types } = require('@ocap/mcrypto');
|
|
190
|
+
*
|
|
191
|
+
* const hashFn = getHasher(types.HashType.SHA3);
|
|
192
|
+
* const hash2 = hashFn(message);
|
|
193
|
+
*/
|
|
194
|
+
function getHasher(type) {
|
|
195
|
+
if (typeof exports.Hashers[type] === 'undefined') {
|
|
196
|
+
throw new Error(`Unsupported hash type: ${type}`);
|
|
224
197
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
[Mcrypto.types.KeyType.SECP256K1]: Mcrypto.Signer.Secp256k1,
|
|
243
|
-
[Mcrypto.types.KeyType.ETHEREUM]: Mcrypto.Signer.Ethereum,
|
|
198
|
+
return exports.Hashers[type];
|
|
199
|
+
}
|
|
200
|
+
exports.getHasher = getHasher;
|
|
201
|
+
/**
|
|
202
|
+
* Get random bytes in specified encoding
|
|
203
|
+
*
|
|
204
|
+
* @param {number} [length=32]
|
|
205
|
+
* @returns {buffer|hex|uint8|base64}
|
|
206
|
+
*/
|
|
207
|
+
function getRandomBytes(length = 32, encoding = 'hex') {
|
|
208
|
+
return (0, encode_1.default)((0, randombytes_1.default)(length), encoding);
|
|
209
|
+
}
|
|
210
|
+
exports.getRandomBytes = getRandomBytes;
|
|
211
|
+
exports.Signers = Object.freeze({
|
|
212
|
+
[exports.types.KeyType.ED25519]: exports.Signer.Ed25519,
|
|
213
|
+
[exports.types.KeyType.SECP256K1]: exports.Signer.Secp256k1,
|
|
214
|
+
[exports.types.KeyType.ETHEREUM]: exports.Signer.Ethereum,
|
|
244
215
|
});
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
[Mcrypto.types.HashType.SHA2]: Mcrypto.Hasher.SHA2.hash256,
|
|
216
|
+
exports.Hashers = Object.freeze({
|
|
217
|
+
[exports.types.HashType.KECCAK]: exports.Hasher.Keccak.hash256,
|
|
218
|
+
[exports.types.HashType.KECCAK_384]: exports.Hasher.Keccak.hash384,
|
|
219
|
+
[exports.types.HashType.KECCAK_512]: exports.Hasher.Keccak.hash512,
|
|
220
|
+
[exports.types.HashType.SHA3]: exports.Hasher.SHA3.hash256,
|
|
221
|
+
[exports.types.HashType.SHA3_384]: exports.Hasher.SHA3.hash384,
|
|
222
|
+
[exports.types.HashType.SHA3_512]: exports.Hasher.SHA3.hash512,
|
|
223
|
+
[exports.types.HashType.SHA2]: exports.Hasher.SHA2.hash256,
|
|
254
224
|
});
|
package/lib/protocols/crypter.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
const interface_1 = __importDefault(require("interface"));
|
|
9
|
+
exports.default = interface_1.default.create('encrypt', 'decrypt');
|
package/lib/protocols/hasher.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
const interface_1 = __importDefault(require("interface"));
|
|
9
|
+
exports.default = interface_1.default.create('sha224', 'sha256', 'sha384', 'sha512');
|
package/lib/protocols/signer.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
const interface_1 = __importDefault(require("interface"));
|
|
9
|
+
exports.default = interface_1.default.create('genKeyPair', 'getPublicKey', 'sign', 'verify');
|