@ocap/mcrypto 1.18.166 → 1.19.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/README.md +7 -7
- package/esm/crypter/aes-legacy.d.ts +8 -0
- package/esm/crypter/aes-legacy.js +26 -0
- package/esm/crypter/aes.d.ts +9 -0
- package/esm/crypter/aes.js +25 -0
- package/esm/crypter/rsa-browserify.d.ts +9 -0
- package/esm/crypter/rsa-browserify.js +33 -0
- package/esm/crypter/rsa.d.ts +11 -0
- package/esm/crypter/rsa.js +28 -0
- package/esm/encode.d.ts +8 -0
- package/esm/encode.js +19 -0
- package/esm/hasher/keccak.d.ts +13 -0
- package/esm/hasher/keccak.js +37 -0
- package/esm/hasher/sha2.d.ts +13 -0
- package/esm/hasher/sha2.js +43 -0
- package/esm/hasher/sha3.d.ts +13 -0
- package/esm/hasher/sha3.js +37 -0
- package/esm/index.d.ts +233 -0
- package/esm/index.js +219 -0
- package/esm/protocols/crypter.d.ts +2 -0
- package/esm/protocols/crypter.js +4 -0
- package/esm/protocols/hasher.d.ts +2 -0
- package/esm/protocols/hasher.js +4 -0
- package/esm/protocols/signer.d.ts +2 -0
- package/esm/protocols/signer.js +4 -0
- package/esm/signer/ed25519.d.ts +53 -0
- package/esm/signer/ed25519.js +82 -0
- package/esm/signer/ethereum.d.ts +16 -0
- package/esm/signer/ethereum.js +35 -0
- package/esm/signer/passkey.d.ts +27 -0
- package/esm/signer/passkey.js +59 -0
- package/esm/signer/secp256k1.d.ts +39 -0
- package/esm/signer/secp256k1.js +95 -0
- package/lib/crypter/rsa-browserify.js +1 -1
- package/lib/crypter/rsa.d.ts +0 -1
- package/lib/encode.d.ts +0 -1
- package/lib/encode.js +1 -2
- package/lib/index.d.ts +9 -7
- package/lib/index.js +9 -4
- package/lib/signer/passkey.d.ts +27 -0
- package/lib/signer/passkey.js +65 -0
- package/package.json +29 -14
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://github.com/prettier/prettier)
|
|
4
4
|
[](https://docs.arcblock.io)
|
|
5
|
-
[](https://gitter.im/ArcBlock/community?utm_source=badge
|
|
5
|
+
[](https://gitter.im/ArcBlock/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
|
6
6
|
|
|
7
7
|
> Forge [mcrypto](https://github.com/ArcBlock/mcrypto) implementation for javascript, just a wrapper around existing javascript crypto libraries.
|
|
8
8
|
|
|
@@ -43,15 +43,15 @@ For full documentation, checkout [https://asset-chain.netlify.com](https://asset
|
|
|
43
43
|
|
|
44
44
|
### Hasher
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
- keccakf1600: js-sha3
|
|
47
|
+
- sha2: hash.js
|
|
48
|
+
- sha3: js-sha3
|
|
49
49
|
|
|
50
50
|
### Signer
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
- [`ed25519`](https://github.com/ArcBlock/blockchain/commit/ed25519): tweetnacl
|
|
53
|
+
- secp256k1: elliptic
|
|
54
54
|
|
|
55
55
|
### Crypter
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
- aes-cbc-256: crypto-js
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import Crypter from '../protocols/crypter';
|
|
2
|
+
declare class AesCrypter extends Crypter {
|
|
3
|
+
encrypt(message: string | object, secret: string): string;
|
|
4
|
+
decrypt(cipher: string, secret: string, outputEncoding?: string): string;
|
|
5
|
+
}
|
|
6
|
+
declare const _default: AesCrypter;
|
|
7
|
+
export default _default;
|
|
8
|
+
export { AesCrypter };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as AES from 'crypto-js/aes';
|
|
2
|
+
import encLatin1 from 'crypto-js/enc-latin1';
|
|
3
|
+
import encUtf8 from 'crypto-js/enc-utf8';
|
|
4
|
+
import encUtf16 from 'crypto-js/enc-utf16';
|
|
5
|
+
import encBase64 from 'crypto-js/enc-base64';
|
|
6
|
+
import encHex from 'crypto-js/enc-hex';
|
|
7
|
+
import Crypter from '../protocols/crypter';
|
|
8
|
+
const encoders = {
|
|
9
|
+
latin1: encLatin1,
|
|
10
|
+
utf8: encUtf8,
|
|
11
|
+
hex: encHex,
|
|
12
|
+
utf16: encUtf16,
|
|
13
|
+
base64: encBase64,
|
|
14
|
+
};
|
|
15
|
+
// AES-CBC-256
|
|
16
|
+
class AesCrypter extends Crypter {
|
|
17
|
+
encrypt(message, secret) {
|
|
18
|
+
const text = typeof message === 'string' ? message : JSON.stringify(message);
|
|
19
|
+
return AES.encrypt(text, secret).toString();
|
|
20
|
+
}
|
|
21
|
+
decrypt(cipher, secret, outputEncoding = 'utf8') {
|
|
22
|
+
return AES.decrypt(cipher, secret).toString(encoders[outputEncoding]);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export default new AesCrypter();
|
|
26
|
+
export { AesCrypter };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BytesType, EncodingType } from '@ocap/util';
|
|
2
|
+
import Crypter from '../protocols/crypter';
|
|
3
|
+
declare class AesCrypter extends Crypter {
|
|
4
|
+
encrypt(message: BytesType, secret: BytesType, encoding?: EncodingType): BytesType;
|
|
5
|
+
decrypt(message: BytesType, secret: BytesType, encoding?: EncodingType): BytesType;
|
|
6
|
+
}
|
|
7
|
+
declare const _default: AesCrypter;
|
|
8
|
+
export default _default;
|
|
9
|
+
export { AesCrypter };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// For browsers, may need: https://www.npmjs.com/package/crypto-browserify
|
|
2
|
+
import crypto from 'crypto';
|
|
3
|
+
import { toBuffer } from '@ocap/util';
|
|
4
|
+
import Crypter from '../protocols/crypter';
|
|
5
|
+
import SHA3 from '../hasher/sha3';
|
|
6
|
+
import { encode } from '../encode';
|
|
7
|
+
// AES-ECB-256
|
|
8
|
+
class AesCrypter extends Crypter {
|
|
9
|
+
encrypt(message, secret, encoding = 'hex') {
|
|
10
|
+
const key = SHA3.hash256(secret, 1, 'buffer');
|
|
11
|
+
const cipher = crypto.createCipheriv('aes-256-ecb', key, '');
|
|
12
|
+
cipher.setAutoPadding(true);
|
|
13
|
+
const output = cipher.update(toBuffer(message));
|
|
14
|
+
return encode(Buffer.concat([output, cipher.final()]), encoding);
|
|
15
|
+
}
|
|
16
|
+
decrypt(message, secret, encoding = 'hex') {
|
|
17
|
+
const key = SHA3.hash256(secret, 1, 'buffer');
|
|
18
|
+
const decipher = crypto.createDecipheriv('aes-256-ecb', key, '');
|
|
19
|
+
decipher.setAutoPadding(true);
|
|
20
|
+
const output = decipher.update(toBuffer(message));
|
|
21
|
+
return encode(Buffer.concat([output, decipher.final()]), encoding);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export default new AesCrypter();
|
|
25
|
+
export { AesCrypter };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
declare class RSABrowserCrypter {
|
|
2
|
+
genKeyPair(length?: number): Promise<CryptoKeyPair>;
|
|
3
|
+
formatPublicKey(key: CryptoKey): Promise<string>;
|
|
4
|
+
encrypt(message: string, key: CryptoKey): Promise<string>;
|
|
5
|
+
decrypt(message: string, key: CryptoKey): Promise<string>;
|
|
6
|
+
}
|
|
7
|
+
declare const _default: RSABrowserCrypter;
|
|
8
|
+
export default _default;
|
|
9
|
+
export { RSABrowserCrypter };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// https://stackoverflow.com/questions/70056340/how-can-i-generate-an-rsa-pair-that-works-both-in-node-js-and-browser
|
|
2
|
+
// https://stackoverflow.com/questions/62948516/using-native-javascript-subtlecrypto-to-encrypt-using-rsa
|
|
3
|
+
import { fromBase58, toBase58 } from '@ocap/util';
|
|
4
|
+
const crypto = window.crypto.subtle;
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
const ab2str = (buffer) => String.fromCharCode.apply(null, new Uint8Array(buffer));
|
|
7
|
+
const RSA_ALGORITHM = 'RSA-OAEP';
|
|
8
|
+
// RSA-OAEP
|
|
9
|
+
class RSABrowserCrypter {
|
|
10
|
+
genKeyPair(length = 2048) {
|
|
11
|
+
return crypto.generateKey({
|
|
12
|
+
name: RSA_ALGORITHM,
|
|
13
|
+
modulusLength: length,
|
|
14
|
+
publicExponent: new Uint8Array([1, 0, 1]),
|
|
15
|
+
hash: 'SHA-256',
|
|
16
|
+
}, true, ['encrypt', 'decrypt']);
|
|
17
|
+
}
|
|
18
|
+
async formatPublicKey(key) {
|
|
19
|
+
const exported = await crypto.exportKey('spki', key);
|
|
20
|
+
const base64 = window.btoa(ab2str(exported));
|
|
21
|
+
return `-----BEGIN PUBLIC KEY-----\n${base64}\n-----END PUBLIC KEY-----`;
|
|
22
|
+
}
|
|
23
|
+
async encrypt(message, key) {
|
|
24
|
+
const encrypted = await crypto.encrypt({ name: RSA_ALGORITHM }, key, new TextEncoder().encode(message));
|
|
25
|
+
return toBase58(new Uint8Array(encrypted));
|
|
26
|
+
}
|
|
27
|
+
async decrypt(message, key) {
|
|
28
|
+
const decrypted = await crypto.decrypt({ name: RSA_ALGORITHM }, key, fromBase58(message));
|
|
29
|
+
return Buffer.from(new Uint8Array(decrypted)).toString('utf8');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export default new RSABrowserCrypter();
|
|
33
|
+
export { RSABrowserCrypter };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import crypto from 'crypto';
|
|
2
|
+
import { BytesType, EncodingType } from '@ocap/util';
|
|
3
|
+
import Crypter from '../protocols/crypter';
|
|
4
|
+
declare class RSACrypter extends Crypter {
|
|
5
|
+
genKeyPair(length?: number): crypto.KeyPairSyncResult<string, string>;
|
|
6
|
+
encrypt(message: BytesType, key: string, encoding?: EncodingType): BytesType;
|
|
7
|
+
decrypt(message: BytesType, key: string, encoding?: EncodingType): BytesType;
|
|
8
|
+
}
|
|
9
|
+
declare const _default: RSACrypter;
|
|
10
|
+
export default _default;
|
|
11
|
+
export { RSACrypter };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import crypto from 'crypto';
|
|
2
|
+
import { toBuffer } from '@ocap/util';
|
|
3
|
+
import Crypter from '../protocols/crypter';
|
|
4
|
+
import { encode } from '../encode';
|
|
5
|
+
// RSA-OAEP
|
|
6
|
+
class RSACrypter extends Crypter {
|
|
7
|
+
genKeyPair(length = 2048) {
|
|
8
|
+
return crypto.generateKeyPairSync('rsa', {
|
|
9
|
+
modulusLength: length,
|
|
10
|
+
publicKeyEncoding: {
|
|
11
|
+
type: 'spki',
|
|
12
|
+
format: 'pem',
|
|
13
|
+
},
|
|
14
|
+
privateKeyEncoding: {
|
|
15
|
+
type: 'pkcs8',
|
|
16
|
+
format: 'pem',
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
encrypt(message, key, encoding = 'hex') {
|
|
21
|
+
return encode(crypto.publicEncrypt(key, toBuffer(message)), encoding);
|
|
22
|
+
}
|
|
23
|
+
decrypt(message, key, encoding = 'hex') {
|
|
24
|
+
return encode(crypto.privateDecrypt(key, toBuffer(message)), encoding);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export default new RSACrypter();
|
|
28
|
+
export { RSACrypter };
|
package/esm/encode.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { BytesType, EncodingType } from '@ocap/util';
|
|
2
|
+
export declare function encode(data: BytesType, encoding?: 'hex'): string;
|
|
3
|
+
export declare function encode(data: BytesType, encoding?: 'base16'): string;
|
|
4
|
+
export declare function encode(data: BytesType, encoding?: 'base58'): string;
|
|
5
|
+
export declare function encode(data: BytesType, encoding?: 'base64'): string;
|
|
6
|
+
export declare function encode(data: BytesType, encoding?: 'buffer'): Buffer;
|
|
7
|
+
export declare function encode(data: BytesType, encoding?: 'Uint8Array'): Uint8Array;
|
|
8
|
+
export declare function encode(data: BytesType, encoding?: EncodingType): BytesType;
|
package/esm/encode.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { toHex, toUint8Array, toBase58, toBase64, toBuffer } from '@ocap/util';
|
|
2
|
+
export function encode(data, encoding = 'hex') {
|
|
3
|
+
if (['hex', 'base16'].includes(encoding)) {
|
|
4
|
+
return toHex(data);
|
|
5
|
+
}
|
|
6
|
+
if (encoding === 'base58') {
|
|
7
|
+
return toBase58(data);
|
|
8
|
+
}
|
|
9
|
+
if (encoding === 'base64') {
|
|
10
|
+
return toBase64(data);
|
|
11
|
+
}
|
|
12
|
+
if (encoding === 'Uint8Array') {
|
|
13
|
+
return toUint8Array(data);
|
|
14
|
+
}
|
|
15
|
+
if (encoding === 'buffer') {
|
|
16
|
+
return toBuffer(data);
|
|
17
|
+
}
|
|
18
|
+
return data;
|
|
19
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BytesType, EncodingType } from '@ocap/util';
|
|
2
|
+
/**
|
|
3
|
+
* Keccak support with different hash length
|
|
4
|
+
*
|
|
5
|
+
* @class KeccakHasher
|
|
6
|
+
*/
|
|
7
|
+
declare class KeccakHasher {
|
|
8
|
+
[x: string]: (data: BytesType, round?: number, encoding?: EncodingType) => BytesType;
|
|
9
|
+
constructor();
|
|
10
|
+
}
|
|
11
|
+
declare const _default: KeccakHasher;
|
|
12
|
+
export default _default;
|
|
13
|
+
export { KeccakHasher };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
2
|
+
import sha3 from 'js-sha3';
|
|
3
|
+
import { toUint8Array } from '@ocap/util';
|
|
4
|
+
import { encode } from '../encode';
|
|
5
|
+
/**
|
|
6
|
+
* Keccak support with different hash length
|
|
7
|
+
*
|
|
8
|
+
* @class KeccakHasher
|
|
9
|
+
*/
|
|
10
|
+
class KeccakHasher {
|
|
11
|
+
constructor() {
|
|
12
|
+
[224, 256, 384, 512].forEach((x) => {
|
|
13
|
+
const name = `hash${x}`;
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
const hasher = (data) => sha3[`keccak${x}`](data);
|
|
16
|
+
const hashFn = (input, round) => {
|
|
17
|
+
if (round === 1) {
|
|
18
|
+
return hasher(input);
|
|
19
|
+
}
|
|
20
|
+
return hashFn(hasher(input), round - 1);
|
|
21
|
+
};
|
|
22
|
+
this[name] = (data, round = 1, encoding = 'hex') => {
|
|
23
|
+
let input = data;
|
|
24
|
+
try {
|
|
25
|
+
input = toUint8Array(data);
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
// Do nothing
|
|
29
|
+
}
|
|
30
|
+
const res = hashFn(input, round);
|
|
31
|
+
return encode(`0x${res}`, encoding);
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export default new KeccakHasher();
|
|
37
|
+
export { KeccakHasher };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BytesType, EncodingType } from '@ocap/util';
|
|
2
|
+
/**
|
|
3
|
+
* Sha2 support with different hash length
|
|
4
|
+
*
|
|
5
|
+
* @class
|
|
6
|
+
*/
|
|
7
|
+
declare class Sha2Hasher {
|
|
8
|
+
[x: string]: (data: BytesType, round?: number, encoding?: EncodingType) => BytesType;
|
|
9
|
+
constructor();
|
|
10
|
+
}
|
|
11
|
+
declare const _default: Sha2Hasher;
|
|
12
|
+
export default _default;
|
|
13
|
+
export { Sha2Hasher };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
2
|
+
import { toUint8Array } from '@ocap/util';
|
|
3
|
+
import { sha224, sha256, sha384, sha512 } from 'hash.js';
|
|
4
|
+
import { encode } from '../encode';
|
|
5
|
+
const hashFns = {
|
|
6
|
+
sha224,
|
|
7
|
+
sha256,
|
|
8
|
+
sha384,
|
|
9
|
+
sha512,
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Sha2 support with different hash length
|
|
13
|
+
*
|
|
14
|
+
* @class
|
|
15
|
+
*/
|
|
16
|
+
class Sha2Hasher {
|
|
17
|
+
constructor() {
|
|
18
|
+
[224, 256, 384, 512].forEach((x) => {
|
|
19
|
+
const name = `hash${x}`;
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
const hasher = hashFns[`sha${x}`];
|
|
22
|
+
const hashFn = (input, round) => {
|
|
23
|
+
let inputBytes = input;
|
|
24
|
+
try {
|
|
25
|
+
inputBytes = toUint8Array(input);
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
// Do nothing
|
|
29
|
+
}
|
|
30
|
+
if (round === 1) {
|
|
31
|
+
return `0x${hasher().update(inputBytes).digest('hex')}`;
|
|
32
|
+
}
|
|
33
|
+
return hashFn(hashFn(inputBytes, 1), round - 1);
|
|
34
|
+
};
|
|
35
|
+
this[name] = (data, round = 2, encoding = 'hex') => {
|
|
36
|
+
const res = hashFn(data, round);
|
|
37
|
+
return encode(res, encoding);
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export default new Sha2Hasher();
|
|
43
|
+
export { Sha2Hasher };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BytesType, EncodingType } from '@ocap/util';
|
|
2
|
+
/**
|
|
3
|
+
* Sha3 support with different hash length
|
|
4
|
+
*
|
|
5
|
+
* @class Sha3Hasher
|
|
6
|
+
*/
|
|
7
|
+
declare class Sha3Hasher {
|
|
8
|
+
[x: string]: (data: BytesType, round?: number, encoding?: EncodingType) => BytesType;
|
|
9
|
+
constructor();
|
|
10
|
+
}
|
|
11
|
+
declare const _default: Sha3Hasher;
|
|
12
|
+
export default _default;
|
|
13
|
+
export { Sha3Hasher };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import sha3 from 'js-sha3';
|
|
2
|
+
import { toUint8Array } from '@ocap/util';
|
|
3
|
+
import { encode } from '../encode';
|
|
4
|
+
/**
|
|
5
|
+
* Sha3 support with different hash length
|
|
6
|
+
*
|
|
7
|
+
* @class Sha3Hasher
|
|
8
|
+
*/
|
|
9
|
+
class Sha3Hasher {
|
|
10
|
+
constructor() {
|
|
11
|
+
[224, 256, 384, 512].forEach((x) => {
|
|
12
|
+
const name = `hash${x}`;
|
|
13
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
const hasher = sha3[`sha3_${x}`];
|
|
16
|
+
const hashFn = (input, round) => {
|
|
17
|
+
if (round === 1) {
|
|
18
|
+
return hasher(input);
|
|
19
|
+
}
|
|
20
|
+
return hashFn(hasher(input), round - 1);
|
|
21
|
+
};
|
|
22
|
+
this[name] = (data, round = 1, encoding = 'hex') => {
|
|
23
|
+
let input = data;
|
|
24
|
+
try {
|
|
25
|
+
input = toUint8Array(data);
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
// Do nothing
|
|
29
|
+
}
|
|
30
|
+
const res = hashFn(input, round);
|
|
31
|
+
return encode(`0x${res}`, encoding);
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export default new Sha3Hasher();
|
|
37
|
+
export { Sha3Hasher };
|
package/esm/index.d.ts
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import type { BytesType, EncodingType, KeyPairType } from '@ocap/util';
|
|
2
|
+
import type { LiteralUnion } from 'type-fest';
|
|
3
|
+
export type KeyType = LiteralUnion<'ED25519' | 'SECP256K1' | 'ETHEREUM', string>;
|
|
4
|
+
export type HashType = LiteralUnion<'KECCAK' | 'SHA3' | 'KECCAK_384' | 'SHA3_384' | 'KECCAK_512' | 'SHA3_512' | 'SHA2', string>;
|
|
5
|
+
export type RoleType = LiteralUnion<'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_STORE' | 'ROLE_TOKEN' | 'ROLE_FACTORY' | 'ROLE_ROLLUP' | 'ROLE_STORAGE' | 'ROLE_PROFILE' | 'ROLE_PASSKEY' | 'ROLE_ANY', string>;
|
|
6
|
+
export type AddressType = LiteralUnion<'BASE16' | 'BASE58', string>;
|
|
7
|
+
export interface HashFnType {
|
|
8
|
+
(data: BytesType, round: number, encoding?: 'hex'): string;
|
|
9
|
+
(data: BytesType, round: number, encoding?: 'base16'): string;
|
|
10
|
+
(data: BytesType, round: number, encoding?: 'base58'): string;
|
|
11
|
+
(data: BytesType, round: number, encoding?: 'base64'): string;
|
|
12
|
+
(data: BytesType, round: number, encoding?: 'buffer'): Buffer;
|
|
13
|
+
(data: BytesType, round: number, encoding?: 'Uint8Array'): Uint8Array;
|
|
14
|
+
(data: BytesType, round: number, encoding?: EncodingType): BytesType;
|
|
15
|
+
}
|
|
16
|
+
export interface SignerType {
|
|
17
|
+
genKeyPair(encoding?: EncodingType, seed?: BytesType): KeyPairType;
|
|
18
|
+
getPublicKey(sk: BytesType, encoding?: 'hex'): string;
|
|
19
|
+
getPublicKey(sk: BytesType, encoding?: 'base16'): string;
|
|
20
|
+
getPublicKey(sk: BytesType, encoding?: 'base58'): string;
|
|
21
|
+
getPublicKey(sk: BytesType, encoding?: 'base64'): string;
|
|
22
|
+
getPublicKey(sk: BytesType, encoding?: 'buffer'): Buffer;
|
|
23
|
+
getPublicKey(sk: BytesType, encoding?: 'Uint8Array'): Uint8Array;
|
|
24
|
+
getPublicKey(sk: BytesType, encoding?: EncodingType): BytesType;
|
|
25
|
+
sign(data: BytesType, sk: BytesType, encoding?: 'hex'): string;
|
|
26
|
+
sign(data: BytesType, sk: BytesType, encoding?: 'base16'): string;
|
|
27
|
+
sign(data: BytesType, sk: BytesType, encoding?: 'base58'): string;
|
|
28
|
+
sign(data: BytesType, sk: BytesType, encoding?: 'base64'): string;
|
|
29
|
+
sign(data: BytesType, sk: BytesType, encoding?: 'buffer'): Buffer;
|
|
30
|
+
sign(data: BytesType, sk: BytesType, encoding?: 'Uint8Array'): Uint8Array;
|
|
31
|
+
sign(data: BytesType, sk: BytesType, encoding?: EncodingType): BytesType;
|
|
32
|
+
verify(data: BytesType, pk: BytesType, signature: BytesType, extra?: any): boolean;
|
|
33
|
+
ethHash?(data: string): string;
|
|
34
|
+
ethSign?(data: string, sk: string): string;
|
|
35
|
+
ethVerify?(data: string, pk: string, signature: BytesType): boolean;
|
|
36
|
+
ethRecover?(hash: string, signature: string): string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Contains all supported signers, eg: `Ed25519` and `Secp256k1`
|
|
40
|
+
*
|
|
41
|
+
* @readonly
|
|
42
|
+
* @type {object}
|
|
43
|
+
* @name Signer
|
|
44
|
+
* @static
|
|
45
|
+
* @example
|
|
46
|
+
* const { Signer } = require('@ocap/mcrypto');
|
|
47
|
+
* const message = 'some message to sign';
|
|
48
|
+
*
|
|
49
|
+
* // Use Signer directly
|
|
50
|
+
* const keyPair = Signer.Ed25519.genKeyPair();
|
|
51
|
+
* const signature = Signer.Ed25519.sign(message, keyPair.secretKey);
|
|
52
|
+
* const result = Signer.Ed25519.verify(message, signature, keyPair.publicKey);
|
|
53
|
+
* assert.ok(result);
|
|
54
|
+
*/
|
|
55
|
+
export declare const Signer: {
|
|
56
|
+
Ed25519: import("./signer/ed25519").Ed25519Signer;
|
|
57
|
+
Secp256k1: import("./signer/secp256k1").Secp256k1Signer;
|
|
58
|
+
Ethereum: import("./signer/ethereum").EthereumSigner;
|
|
59
|
+
Passkey: import("./signer/passkey").PasskeySigner;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Contains all supported hasher, eg: `SHA2`,`SHA3` and `Keccak`, each of them supports `hash224`, `hash256`, `hash384`, `hash512`
|
|
63
|
+
*
|
|
64
|
+
* @readonly
|
|
65
|
+
* @type {object}
|
|
66
|
+
* @name Hasher
|
|
67
|
+
* @static
|
|
68
|
+
* @example
|
|
69
|
+
* const { Hasher } = require('@ocap/mcrypto');
|
|
70
|
+
*
|
|
71
|
+
* const message = 'message to hash';
|
|
72
|
+
* const hash = Hasher.SHA2.hash256(message);
|
|
73
|
+
*/
|
|
74
|
+
export declare const Hasher: {
|
|
75
|
+
SHA2: import("./hasher/sha2").Sha2Hasher;
|
|
76
|
+
SHA3: import("./hasher/sha3").Sha3Hasher;
|
|
77
|
+
Keccak: import("./hasher/keccak").KeccakHasher;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Contains type constants that represent can be used to compose different crypto method, each crypto method consist one of:
|
|
81
|
+
* FIXME: enum definition of forge-abi and abt-did-elixir are not exactly the same
|
|
82
|
+
*
|
|
83
|
+
* @readonly
|
|
84
|
+
* @type {object}
|
|
85
|
+
* @name types
|
|
86
|
+
* @static
|
|
87
|
+
* @example
|
|
88
|
+
* const { types } = require('@ocap/mcrypto');
|
|
89
|
+
*
|
|
90
|
+
* // types.RoleType.ROLE_ACCOUNT
|
|
91
|
+
* // types.KeyType.ED25519
|
|
92
|
+
* // types.HashType.SHA3
|
|
93
|
+
* // types.EncodingType.BASE58
|
|
94
|
+
*/
|
|
95
|
+
export declare const types: {
|
|
96
|
+
/**
|
|
97
|
+
* Key-pair derivation algorithms
|
|
98
|
+
*
|
|
99
|
+
* @readonly
|
|
100
|
+
* @enum {number}
|
|
101
|
+
* @name types.KeyType
|
|
102
|
+
* @memberof types
|
|
103
|
+
* @static
|
|
104
|
+
*/
|
|
105
|
+
KeyType: {
|
|
106
|
+
ED25519: number;
|
|
107
|
+
SECP256K1: number;
|
|
108
|
+
ETHEREUM: number;
|
|
109
|
+
PASSKEY: number;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Hashing algorithms
|
|
113
|
+
*
|
|
114
|
+
* @readonly
|
|
115
|
+
* @enum {number}
|
|
116
|
+
* @name types.HashType
|
|
117
|
+
* @memberof types
|
|
118
|
+
* @static
|
|
119
|
+
*/
|
|
120
|
+
HashType: {
|
|
121
|
+
KECCAK: number;
|
|
122
|
+
SHA3: number;
|
|
123
|
+
KECCAK_384: number;
|
|
124
|
+
SHA3_384: number;
|
|
125
|
+
KECCAK_512: number;
|
|
126
|
+
SHA3_512: number;
|
|
127
|
+
SHA2: number;
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* DID wallet role type
|
|
131
|
+
*
|
|
132
|
+
* @readonly
|
|
133
|
+
* @enum {number}
|
|
134
|
+
* @name types.RoleType
|
|
135
|
+
* @memberof types
|
|
136
|
+
* @static
|
|
137
|
+
*/
|
|
138
|
+
RoleType: {
|
|
139
|
+
ROLE_ACCOUNT: number;
|
|
140
|
+
ROLE_NODE: number;
|
|
141
|
+
ROLE_DEVICE: number;
|
|
142
|
+
ROLE_APPLICATION: number;
|
|
143
|
+
ROLE_SMART_CONTRACT: number;
|
|
144
|
+
ROLE_BOT: number;
|
|
145
|
+
ROLE_ASSET: number;
|
|
146
|
+
ROLE_STAKE: number;
|
|
147
|
+
ROLE_VALIDATOR: number;
|
|
148
|
+
ROLE_GROUP: number;
|
|
149
|
+
ROLE_TX: number;
|
|
150
|
+
ROLE_TETHER: number;
|
|
151
|
+
ROLE_SWAP: number;
|
|
152
|
+
ROLE_DELEGATION: number;
|
|
153
|
+
ROLE_VC: number;
|
|
154
|
+
ROLE_BLOCKLET: number;
|
|
155
|
+
ROLE_STORE: number;
|
|
156
|
+
ROLE_TOKEN: number;
|
|
157
|
+
ROLE_FACTORY: number;
|
|
158
|
+
ROLE_ROLLUP: number;
|
|
159
|
+
ROLE_STORAGE: number;
|
|
160
|
+
ROLE_PROFILE: number;
|
|
161
|
+
ROLE_PASSKEY: number;
|
|
162
|
+
ROLE_ANY: number;
|
|
163
|
+
};
|
|
164
|
+
/**
|
|
165
|
+
* Address encoding algorithm, defaults to `base58btc`
|
|
166
|
+
*
|
|
167
|
+
* @readonly
|
|
168
|
+
* @enum {number}
|
|
169
|
+
* @name types.RoleType
|
|
170
|
+
* @memberof types
|
|
171
|
+
* @static
|
|
172
|
+
*/
|
|
173
|
+
EncodingType: {
|
|
174
|
+
BASE16: number;
|
|
175
|
+
BASE58: number;
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* Get signer instance
|
|
180
|
+
*
|
|
181
|
+
* @function
|
|
182
|
+
* @param {number} type - algorithm used to derive key pair, possible values are
|
|
183
|
+
* - types.KeyType.ED25519
|
|
184
|
+
* - types.KeyType.SECP256k1
|
|
185
|
+
* - types.KeyType.ETHEREUM
|
|
186
|
+
* @returns {object} signer instance
|
|
187
|
+
* @example
|
|
188
|
+
* const { getSigner, types } = require('@ocap/mcrypto');
|
|
189
|
+
* const message = 'some message to sign';
|
|
190
|
+
*
|
|
191
|
+
* const signer = getSigner(types.KeyType.ED25519);
|
|
192
|
+
* const keyPair1 = signer.genKeyPair();
|
|
193
|
+
* const signature1 = signer.sign(message, keyPair1.secretKey);
|
|
194
|
+
* const result1 = signer.verify(message, signature1, keyPair1.publicKey);
|
|
195
|
+
* assert.ok(result1);
|
|
196
|
+
*/
|
|
197
|
+
export declare function getSigner(type: number): SignerType;
|
|
198
|
+
/**
|
|
199
|
+
* Get hasher instance
|
|
200
|
+
*
|
|
201
|
+
* @function
|
|
202
|
+
* @param {number} type - algorithm used to hash data, possible values
|
|
203
|
+
* - types.HashType.KECCAK
|
|
204
|
+
* - types.HashType.KECCAK_384
|
|
205
|
+
* - types.HashType.KECCAK_512
|
|
206
|
+
* - types.HashType.SHA3
|
|
207
|
+
* - types.HashType.SHA3_384
|
|
208
|
+
* - types.HashType.SHA3_512
|
|
209
|
+
* @returns {object} hasher instance
|
|
210
|
+
* @example
|
|
211
|
+
* const { getHasher, types } = require('@ocap/mcrypto');
|
|
212
|
+
*
|
|
213
|
+
* const hashFn = getHasher(types.HashType.SHA3);
|
|
214
|
+
* const hash2 = hashFn(message);
|
|
215
|
+
*/
|
|
216
|
+
export declare function getHasher(type: number): HashFnType;
|
|
217
|
+
/**
|
|
218
|
+
* Get random bytes in specified encoding
|
|
219
|
+
*/
|
|
220
|
+
export declare function getRandomBytes(length: number, encoding?: 'hex'): string;
|
|
221
|
+
export declare function getRandomBytes(length: number, encoding?: 'base16'): string;
|
|
222
|
+
export declare function getRandomBytes(length: number, encoding?: 'base58'): string;
|
|
223
|
+
export declare function getRandomBytes(length: number, encoding?: 'base64'): string;
|
|
224
|
+
export declare function getRandomBytes(length: number, encoding?: 'buffer'): Buffer;
|
|
225
|
+
export declare function getRandomBytes(length: number, encoding?: 'Uint8Array'): Uint8Array;
|
|
226
|
+
export declare function getRandomBytes(length: number, encoding?: EncodingType): BytesType;
|
|
227
|
+
export declare const Signers: Readonly<{
|
|
228
|
+
[x: number]: import("./signer/ed25519").Ed25519Signer | import("./signer/secp256k1").Secp256k1Signer | import("./signer/passkey").PasskeySigner;
|
|
229
|
+
}>;
|
|
230
|
+
export declare const Hashers: Readonly<{
|
|
231
|
+
[x: number]: (data: BytesType, round?: number, encoding?: EncodingType) => BytesType;
|
|
232
|
+
}>;
|
|
233
|
+
export declare const toTxHash: (buf: Buffer | Uint8Array) => string;
|