jcc_wallet 4.0.1 → 4.0.3
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/dist/jcc-wallet.min.js +21 -26
- package/lib/constant.js +0 -1
- package/lib/eccrypto.d.ts +12 -0
- package/lib/eccrypto.js +116 -0
- package/lib/hd/constant.js +0 -1
- package/lib/hd/eos.plugin.d.ts +1 -0
- package/lib/hd/eos.plugin.js +36 -25
- package/lib/hd/ethereum.plugin.d.ts +1 -1
- package/lib/hd/ethereum.plugin.js +21 -26
- package/lib/hd/index.js +0 -1
- package/lib/hd/plugins.js +1 -1
- package/lib/hd/swtc.plugin.js +0 -1
- package/lib/hd/tron.plugin.js +0 -1
- package/lib/index.js +0 -1
- package/lib/jingchangWallet.d.ts +1 -1
- package/lib/jingchangWallet.js +70 -54
- package/lib/minify-eosjs/PrivateKey.d.ts +27 -0
- package/lib/minify-eosjs/PrivateKey.js +77 -0
- package/lib/minify-eosjs/PublicKey.d.ts +25 -0
- package/lib/minify-eosjs/PublicKey.js +61 -0
- package/lib/minify-eosjs/Signature.d.ts +34 -0
- package/lib/minify-eosjs/Signature.js +100 -0
- package/lib/minify-eosjs/bn-utils.d.ts +2 -0
- package/lib/minify-eosjs/bn-utils.js +17 -0
- package/lib/minify-eosjs/eosjs-key-conversions.d.ts +12 -0
- package/lib/minify-eosjs/eosjs-key-conversions.js +25 -0
- package/lib/minify-eosjs/eosjs-numeric.d.ts +48 -0
- package/lib/minify-eosjs/eosjs-numeric.js +303 -0
- package/lib/minify-ethereumjs-util/account.js +0 -1
- package/lib/minify-ethereumjs-util/bytes.js +0 -1
- package/lib/minify-ethereumjs-util/constants.js +0 -1
- package/lib/minify-ethereumjs-util/helper.js +0 -1
- package/lib/minify-ethereumjs-util/internal.js +0 -1
- package/lib/minify-ethereumjs-util/signature.js +0 -1
- package/lib/minify-swtc-keypair/address-codec.js +0 -1
- package/lib/minify-swtc-keypair/index.js +11 -22
- package/lib/minify-swtc-keypair/sha512.js +0 -1
- package/lib/minify-swtc-keypair/utils.js +0 -1
- package/lib/minify-tron/address.js +0 -1
- package/lib/minify-tron/base58.js +0 -1
- package/lib/minify-tron/bytes.js +0 -1
- package/lib/minify-tron/code.d.ts +1 -1
- package/lib/minify-tron/code.js +0 -1
- package/lib/minify-tron/crypto.js +3 -4
- package/lib/minify-tron/message.d.ts +2 -2
- package/lib/minify-tron/message.js +8 -9
- package/lib/types.js +0 -1
- package/lib/util/index.d.ts +5 -4
- package/lib/util/index.js +28 -21
- package/package.json +23 -41
- package/lib/constant.js.map +0 -1
- package/lib/hd/constant.js.map +0 -1
- package/lib/hd/eos.plugin.js.map +0 -1
- package/lib/hd/ethereum.plugin.js.map +0 -1
- package/lib/hd/index.js.map +0 -1
- package/lib/hd/plugins.js.map +0 -1
- package/lib/hd/swtc.plugin.js.map +0 -1
- package/lib/hd/tron.plugin.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/jingchangWallet.js.map +0 -1
- package/lib/minify-ethereumjs-util/account.js.map +0 -1
- package/lib/minify-ethereumjs-util/bytes.js.map +0 -1
- package/lib/minify-ethereumjs-util/constants.js.map +0 -1
- package/lib/minify-ethereumjs-util/helper.js.map +0 -1
- package/lib/minify-ethereumjs-util/internal.js.map +0 -1
- package/lib/minify-ethereumjs-util/signature.js.map +0 -1
- package/lib/minify-swtc-keypair/address-codec.js.map +0 -1
- package/lib/minify-swtc-keypair/index.js.map +0 -1
- package/lib/minify-swtc-keypair/sha512.js.map +0 -1
- package/lib/minify-swtc-keypair/utils.js.map +0 -1
- package/lib/minify-tron/address.js.map +0 -1
- package/lib/minify-tron/base58.js.map +0 -1
- package/lib/minify-tron/bytes.js.map +0 -1
- package/lib/minify-tron/code.js.map +0 -1
- package/lib/minify-tron/crypto.js.map +0 -1
- package/lib/minify-tron/message.js.map +0 -1
- package/lib/types.js.map +0 -1
- package/lib/util/index.js.map +0 -1
package/lib/constant.js
CHANGED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* forked from https://github.com/bitchan/eccrypto/blob/master/index.js
|
|
3
|
+
*
|
|
4
|
+
* rewrite curves & hashes
|
|
5
|
+
*/
|
|
6
|
+
export declare const encrypt: (publicKeyTo: any, msg: any, opts?: any) => Promise<{
|
|
7
|
+
iv: any;
|
|
8
|
+
ephemPublicKey: any;
|
|
9
|
+
ciphertext: Uint8Array;
|
|
10
|
+
mac: Buffer;
|
|
11
|
+
}>;
|
|
12
|
+
export declare const decrypt: (privateKey: any, opts: any) => Promise<Uint8Array>;
|
package/lib/eccrypto.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* forked from https://github.com/bitchan/eccrypto/blob/master/index.js
|
|
4
|
+
*
|
|
5
|
+
* rewrite curves & hashes
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.decrypt = exports.encrypt = void 0;
|
|
9
|
+
const EC_GROUP_ORDER = Buffer.from("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", "hex");
|
|
10
|
+
const ZERO32 = Buffer.alloc(32, 0);
|
|
11
|
+
const utils_1 = require("@noble/hashes/utils");
|
|
12
|
+
const sha512_1 = require("@noble/hashes/sha512");
|
|
13
|
+
const hmac_1 = require("@noble/hashes/hmac");
|
|
14
|
+
const sha2_1 = require("@noble/hashes/sha2");
|
|
15
|
+
const aes_1 = require("ethereum-cryptography/aes");
|
|
16
|
+
const secp256k1_1 = require("@noble/curves/secp256k1");
|
|
17
|
+
const Point = secp256k1_1.secp256k1.ProjectivePoint;
|
|
18
|
+
function isScalar(x) {
|
|
19
|
+
return Buffer.isBuffer(x) && x.length === 32;
|
|
20
|
+
}
|
|
21
|
+
function isValidPrivateKey(privateKey) {
|
|
22
|
+
if (!isScalar(privateKey)) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
return (privateKey.compare(ZERO32) > 0 && privateKey.compare(EC_GROUP_ORDER) < 0 // > 0
|
|
26
|
+
); // < G
|
|
27
|
+
}
|
|
28
|
+
function assert(condition, message) {
|
|
29
|
+
if (!condition) {
|
|
30
|
+
throw new Error(message || "Assertion failed");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function sha512(msg) {
|
|
34
|
+
return sha512_1.sha512.create()
|
|
35
|
+
.update(msg)
|
|
36
|
+
.digest();
|
|
37
|
+
}
|
|
38
|
+
async function aes256CbcEncrypt(iv, key, plaintext) {
|
|
39
|
+
const buf = await (0, aes_1.encrypt)(plaintext, key, iv, "aes-256-cbc");
|
|
40
|
+
return buf;
|
|
41
|
+
}
|
|
42
|
+
async function aes256CbcDecrypt(iv, key, ciphertext) {
|
|
43
|
+
return await (0, aes_1.decrypt)(ciphertext, key, iv, "aes-256-cbc");
|
|
44
|
+
}
|
|
45
|
+
function hmacSha256(key, msg) {
|
|
46
|
+
return hmac_1.hmac
|
|
47
|
+
.create(sha2_1.sha256, key)
|
|
48
|
+
.update(msg)
|
|
49
|
+
.digest();
|
|
50
|
+
}
|
|
51
|
+
// Compare two buffers in constant time to prevent timing attacks.
|
|
52
|
+
function equalConstTime(b1, b2) {
|
|
53
|
+
if (b1.length !== b2.length) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
let res = 0;
|
|
57
|
+
for (let i = 0; i < b1.length; i++) {
|
|
58
|
+
res |= b1[i] ^ b2[i]; // jshint ignore:line
|
|
59
|
+
}
|
|
60
|
+
return res === 0;
|
|
61
|
+
}
|
|
62
|
+
const getPublic = (privateKey) => {
|
|
63
|
+
assert(privateKey.length === 32, "Bad private key");
|
|
64
|
+
assert(isValidPrivateKey(privateKey), "Bad private key");
|
|
65
|
+
return Point.fromPrivateKey(privateKey).toRawBytes(false);
|
|
66
|
+
};
|
|
67
|
+
const derive = async (privateKeyA, publicKeyB) => {
|
|
68
|
+
return new Promise((resolve) => {
|
|
69
|
+
assert(privateKeyA.length === 32, "Bad private key");
|
|
70
|
+
assert(isValidPrivateKey(privateKeyA), "Bad private key");
|
|
71
|
+
resolve(secp256k1_1.secp256k1.getSharedSecret(privateKeyA, publicKeyB));
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
const encrypt = (publicKeyTo, msg, opts) => {
|
|
75
|
+
opts = opts || {};
|
|
76
|
+
// Tmp variable to save context from flat promises;
|
|
77
|
+
let ephemPublicKey;
|
|
78
|
+
return new Promise(function (resolve) {
|
|
79
|
+
let ephemPrivateKey = opts.ephemPrivateKey || Buffer.from((0, utils_1.randomBytes)(32));
|
|
80
|
+
// There is a very unlikely possibility that it is not a valid key
|
|
81
|
+
while (!isValidPrivateKey(ephemPrivateKey)) {
|
|
82
|
+
ephemPrivateKey = opts.ephemPrivateKey || Buffer.from((0, utils_1.randomBytes)(32));
|
|
83
|
+
}
|
|
84
|
+
ephemPublicKey = getPublic(ephemPrivateKey);
|
|
85
|
+
resolve(derive(ephemPrivateKey, publicKeyTo));
|
|
86
|
+
}).then(async function (Px) {
|
|
87
|
+
const hash = sha512(Px);
|
|
88
|
+
const iv = opts.iv || (0, utils_1.randomBytes)(16);
|
|
89
|
+
const encryptionKey = hash.slice(0, 32);
|
|
90
|
+
const macKey = hash.slice(32);
|
|
91
|
+
const ciphertext = await aes256CbcEncrypt(iv, encryptionKey, msg);
|
|
92
|
+
const dataToMac = Buffer.concat([iv, ephemPublicKey, ciphertext]);
|
|
93
|
+
const mac = Buffer.from(hmacSha256(macKey, dataToMac));
|
|
94
|
+
return {
|
|
95
|
+
iv: iv,
|
|
96
|
+
ephemPublicKey: ephemPublicKey,
|
|
97
|
+
ciphertext: ciphertext,
|
|
98
|
+
mac: mac
|
|
99
|
+
};
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
exports.encrypt = encrypt;
|
|
103
|
+
const decrypt = (privateKey, opts) => {
|
|
104
|
+
return derive(privateKey, opts.ephemPublicKey).then(function (Px) {
|
|
105
|
+
assert(privateKey.length === 32, "Bad private key");
|
|
106
|
+
assert(isValidPrivateKey(privateKey), "Bad private key");
|
|
107
|
+
const hash = sha512(Px);
|
|
108
|
+
const encryptionKey = hash.slice(0, 32);
|
|
109
|
+
const macKey = hash.slice(32);
|
|
110
|
+
const dataToMac = Buffer.concat([opts.iv, opts.ephemPublicKey, opts.ciphertext]);
|
|
111
|
+
const realMac = hmacSha256(macKey, dataToMac);
|
|
112
|
+
assert(equalConstTime(opts.mac, realMac), "Bad MAC");
|
|
113
|
+
return aes256CbcDecrypt(opts.iv, encryptionKey, opts.ciphertext);
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
exports.decrypt = decrypt;
|
package/lib/hd/constant.js
CHANGED
package/lib/hd/eos.plugin.d.ts
CHANGED
package/lib/hd/eos.plugin.js
CHANGED
|
@@ -1,42 +1,54 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.plugin = void 0;
|
|
4
|
-
const PublicKey = require("eosjs-ecc/lib/key_public");
|
|
5
|
-
const PrivateKey = require("eosjs-ecc/lib/key_private");
|
|
6
|
-
const Signature = require("eosjs-ecc/lib/signature");
|
|
7
|
-
const wif = require("wif");
|
|
8
|
-
const secp256k1_1 = require("@noble/curves/secp256k1");
|
|
9
|
-
const jcc_common_1 = require("jcc_common");
|
|
10
4
|
const sha256_1 = require("@noble/hashes/sha256");
|
|
5
|
+
const PublicKey_1 = require("../minify-eosjs/PublicKey");
|
|
6
|
+
const PrivateKey_1 = require("../minify-eosjs/PrivateKey");
|
|
7
|
+
const Signature_1 = require("../minify-eosjs/Signature");
|
|
8
|
+
const eosjs_numeric_1 = require("../minify-eosjs/eosjs-numeric");
|
|
9
|
+
const internal_1 = require("../minify-ethereumjs-util/internal");
|
|
11
10
|
exports.plugin = {
|
|
12
11
|
checkPrivateKey(privateKey) {
|
|
13
12
|
// check and cut swtc keypair lib add prefix 00
|
|
14
13
|
return privateKey.length === 66 ? privateKey.substring(2) : privateKey;
|
|
15
14
|
},
|
|
15
|
+
privateKeyToLegacyString(privateKey) {
|
|
16
|
+
const priv = exports.plugin.checkPrivateKey((0, internal_1.stripHexPrefix)(privateKey));
|
|
17
|
+
const buffer = Buffer.from(priv, "hex");
|
|
18
|
+
const eosPrivateKey = (0, eosjs_numeric_1.privateKeyToLegacyString)({
|
|
19
|
+
data: Uint8Array.from(buffer),
|
|
20
|
+
type: eosjs_numeric_1.KeyType.k1
|
|
21
|
+
});
|
|
22
|
+
return eosPrivateKey;
|
|
23
|
+
},
|
|
16
24
|
address(key) {
|
|
17
25
|
if (key.privateKey) {
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return eosPublicKey;
|
|
26
|
+
const eosPrivateKey = exports.plugin.privateKeyToLegacyString(key.privateKey);
|
|
27
|
+
return PrivateKey_1.PrivateKey.fromString(eosPrivateKey)
|
|
28
|
+
.getPublicKey()
|
|
29
|
+
.toLegacyString();
|
|
23
30
|
}
|
|
24
31
|
if (key.publicKey) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
32
|
+
return (0, eosjs_numeric_1.publicKeyToLegacyString)({
|
|
33
|
+
data: Uint8Array.from(Buffer.from(key.publicKey, "hex")),
|
|
34
|
+
type: eosjs_numeric_1.KeyType.k1
|
|
35
|
+
});
|
|
28
36
|
}
|
|
29
37
|
return null;
|
|
30
38
|
},
|
|
31
39
|
isValidAddress(address) {
|
|
32
|
-
|
|
40
|
+
try {
|
|
41
|
+
const pk = PublicKey_1.PublicKey.fromString(address);
|
|
42
|
+
return pk.isValid();
|
|
43
|
+
}
|
|
44
|
+
catch (_) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
33
47
|
},
|
|
34
48
|
isValidSecret(secret) {
|
|
35
49
|
try {
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
const eosPrivateKey = wif.encode(128, buffer, false);
|
|
39
|
-
return PrivateKey.isValid(eosPrivateKey);
|
|
50
|
+
const eosPrivateKey = exports.plugin.privateKeyToLegacyString(secret);
|
|
51
|
+
return PrivateKey_1.PrivateKey.fromString(eosPrivateKey).isValid();
|
|
40
52
|
}
|
|
41
53
|
catch (_) {
|
|
42
54
|
return false;
|
|
@@ -55,16 +67,15 @@ exports.plugin = {
|
|
|
55
67
|
* @returns signature string
|
|
56
68
|
*/
|
|
57
69
|
sign(message, privateKey) {
|
|
58
|
-
const
|
|
59
|
-
const
|
|
60
|
-
return
|
|
70
|
+
const eosPrivateKey = exports.plugin.privateKeyToLegacyString(privateKey);
|
|
71
|
+
const pk = PrivateKey_1.PrivateKey.fromString(eosPrivateKey);
|
|
72
|
+
return pk.sign(message).toString();
|
|
61
73
|
},
|
|
62
74
|
verify(message, signature, address) {
|
|
63
75
|
return exports.plugin.recover(message, signature) === address;
|
|
64
76
|
},
|
|
65
77
|
recover(message, signature) {
|
|
66
|
-
const s = Signature.
|
|
67
|
-
return s.recover(message
|
|
78
|
+
const s = Signature_1.Signature.fromString(signature);
|
|
79
|
+
return s.recover(message).toLegacyString();
|
|
68
80
|
}
|
|
69
81
|
};
|
|
70
|
-
//# sourceMappingURL=eos.plugin.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { IHDPlugin } from "../types";
|
|
2
2
|
export interface IEthereumPlugin extends IHDPlugin {
|
|
3
3
|
checkPrivateKey(privateKey: string): string;
|
|
4
|
-
decryptKeystore(password: string, encryptData: any): string
|
|
4
|
+
decryptKeystore(password: string, encryptData: any): Promise<string>;
|
|
5
5
|
}
|
|
6
6
|
export declare const plugin: IEthereumPlugin;
|
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.plugin = void 0;
|
|
7
4
|
const secp256k1_1 = require("@noble/curves/secp256k1");
|
|
@@ -10,10 +7,8 @@ const bytes_1 = require("../minify-ethereumjs-util/bytes");
|
|
|
10
7
|
const internal_1 = require("../minify-ethereumjs-util/internal");
|
|
11
8
|
const signature_1 = require("../minify-ethereumjs-util/signature");
|
|
12
9
|
const account_1 = require("../minify-ethereumjs-util/account");
|
|
13
|
-
const
|
|
10
|
+
const util_1 = require("../util");
|
|
14
11
|
const constant_1 = require("../constant");
|
|
15
|
-
const scrypt_1 = require("@noble/hashes/scrypt");
|
|
16
|
-
const crypto_1 = __importDefault(require("crypto"));
|
|
17
12
|
const utils_1 = require("@noble/hashes/utils");
|
|
18
13
|
const isObject = (obj) => {
|
|
19
14
|
return Object.prototype.toString.call(obj) === "[object Object]";
|
|
@@ -83,33 +78,34 @@ exports.plugin = {
|
|
|
83
78
|
const address = (0, account_1.pubToAddress)(Buffer.from(pk));
|
|
84
79
|
return (0, bytes_1.bytesToHex)(address);
|
|
85
80
|
},
|
|
86
|
-
decryptKeystore(password, encryptData) {
|
|
81
|
+
async decryptKeystore(password, encryptData) {
|
|
87
82
|
if (!isObject(encryptData)) {
|
|
88
83
|
throw new Error(constant_1.KEYSTORE_IS_INVALID);
|
|
89
84
|
}
|
|
90
85
|
const cryptoData = encryptData.Crypto || encryptData.crypto;
|
|
91
|
-
if ((0,
|
|
86
|
+
if ((0, util_1.isEmptyPlainObject)(cryptoData) ||
|
|
87
|
+
(0, util_1.isEmptyPlainObject)(cryptoData.cipherparams) ||
|
|
88
|
+
(0, util_1.isEmptyPlainObject)(cryptoData.kdfparams)) {
|
|
92
89
|
throw new Error(constant_1.KEYSTORE_IS_INVALID);
|
|
93
90
|
}
|
|
94
|
-
const iv = Buffer.from(cryptoData.cipherparams.iv, "hex");
|
|
95
91
|
const kdfparams = cryptoData.kdfparams;
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
92
|
+
const buf = await (0, util_1.decrypt)(password, {
|
|
93
|
+
crypto: {
|
|
94
|
+
iv: cryptoData.cipherparams.iv,
|
|
95
|
+
cipher: cryptoData.cipher,
|
|
96
|
+
kdf: cryptoData.kdf,
|
|
97
|
+
kdfparams: {
|
|
98
|
+
dklen: kdfparams.dklen,
|
|
99
|
+
n: kdfparams.n,
|
|
100
|
+
p: kdfparams.p,
|
|
101
|
+
r: kdfparams.r,
|
|
102
|
+
salt: kdfparams.salt
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
mac: cryptoData.mac,
|
|
106
|
+
ciphertext: cryptoData.ciphertext
|
|
101
107
|
});
|
|
102
|
-
|
|
103
|
-
const mac = keccak_js_1.keccak256
|
|
104
|
-
.create()
|
|
105
|
-
.update(Buffer.concat([derivedKey.slice(16, 32), ciphertext]))
|
|
106
|
-
.digest();
|
|
107
|
-
if (Buffer.from(mac).toString("hex") !== cryptoData.mac) {
|
|
108
|
-
throw new Error(constant_1.ETH_PASSWORD_IS_WRONG);
|
|
109
|
-
}
|
|
110
|
-
const decipher = crypto_1.default.createDecipheriv("aes-128-ctr", derivedKey.slice(0, 16), iv);
|
|
111
|
-
const seed = Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
112
|
-
return seed.toString("hex");
|
|
108
|
+
return buf.toString("hex");
|
|
113
109
|
},
|
|
114
110
|
createWallet() {
|
|
115
111
|
const priv = (0, utils_1.randomBytes)(32);
|
|
@@ -117,4 +113,3 @@ exports.plugin = {
|
|
|
117
113
|
return { address, secret: (0, bytes_1.bytesToHex)(priv) };
|
|
118
114
|
}
|
|
119
115
|
};
|
|
120
|
-
//# sourceMappingURL=ethereum.plugin.js.map
|
package/lib/hd/index.js
CHANGED
package/lib/hd/plugins.js
CHANGED
|
@@ -23,6 +23,7 @@ const pluginMap = {
|
|
|
23
23
|
ethereum: ethereum_plugin_1.plugin,
|
|
24
24
|
bsc: ethereum_plugin_2.plugin,
|
|
25
25
|
heco: ethereum_plugin_3.plugin,
|
|
26
|
+
moac: ethereum_plugin_1.plugin,
|
|
26
27
|
polygon: ethereum_plugin_4.plugin,
|
|
27
28
|
tron: tron_plugin_1.plugin,
|
|
28
29
|
eos: eos_plugin_1.plugin,
|
|
@@ -36,4 +37,3 @@ const pluginMap = {
|
|
|
36
37
|
function getPluginByType(type) {
|
|
37
38
|
return pluginMap[type];
|
|
38
39
|
}
|
|
39
|
-
//# sourceMappingURL=plugins.js.map
|
package/lib/hd/swtc.plugin.js
CHANGED
package/lib/hd/tron.plugin.js
CHANGED
package/lib/index.js
CHANGED
package/lib/jingchangWallet.d.ts
CHANGED
|
@@ -259,7 +259,7 @@ export default class JingchangWallet {
|
|
|
259
259
|
* @returns {IKeystoreModel}
|
|
260
260
|
* @memberof JingchangWallet
|
|
261
261
|
*/
|
|
262
|
-
protected getEncryptData(password: string, keypairs: IKeypairsModel): IKeystoreModel
|
|
262
|
+
protected getEncryptData(password: string, keypairs: IKeypairsModel): Promise<IKeystoreModel>;
|
|
263
263
|
/**
|
|
264
264
|
* save wallet keystore to jingchang wallet
|
|
265
265
|
*
|
package/lib/jingchangWallet.js
CHANGED
|
@@ -1,18 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
27
|
};
|
|
5
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const assert_1 = __importDefault(require("assert"));
|
|
7
29
|
const clone_deep_1 = __importDefault(require("clone-deep"));
|
|
8
30
|
const sha256_1 = require("@noble/hashes/sha256");
|
|
9
|
-
const
|
|
10
|
-
const
|
|
31
|
+
const eccrypto = __importStar(require("./eccrypto"));
|
|
32
|
+
const util_1 = require("./util");
|
|
11
33
|
const lockr_1 = __importDefault(require("lockr"));
|
|
12
34
|
const minify_swtc_keypair_1 = require("./minify-swtc-keypair");
|
|
13
35
|
const constant_1 = require("./constant");
|
|
14
36
|
const plugins_1 = require("./hd/plugins");
|
|
15
|
-
const
|
|
37
|
+
const util_2 = require("./util");
|
|
16
38
|
const types_1 = require("./types");
|
|
17
39
|
lockr_1.default.prefix = "jingchang_";
|
|
18
40
|
/**
|
|
@@ -48,7 +70,7 @@ class JingchangWallet {
|
|
|
48
70
|
if (typeof wallet === "string") {
|
|
49
71
|
wallet = JSON.parse(wallet);
|
|
50
72
|
}
|
|
51
|
-
const walletsNotEmpty = !(0,
|
|
73
|
+
const walletsNotEmpty = !(0, util_1.isEmptyPlainObject)(wallet) && Array.isArray(wallet.wallets) && wallet.wallets.length > 0;
|
|
52
74
|
return Boolean(walletsNotEmpty && wallet.contact && wallet.id && wallet.version);
|
|
53
75
|
}
|
|
54
76
|
catch (error) {
|
|
@@ -65,33 +87,31 @@ class JingchangWallet {
|
|
|
65
87
|
* @returns {Promise<IJingchangWalletModel>} resolve jingchang wallet if success.
|
|
66
88
|
* @memberof JingchangWallet
|
|
67
89
|
*/
|
|
68
|
-
static generate(password, secret, alias) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return reject(new Error(constant_1.SECRET_IS_INVALID));
|
|
79
|
-
}
|
|
80
|
-
keypairs.address = plugins_1.jtWallet.getAddress(secret);
|
|
90
|
+
static async generate(password, secret, alias) {
|
|
91
|
+
const keypairs = {};
|
|
92
|
+
if (secret === undefined) {
|
|
93
|
+
const wallet = plugins_1.jtWallet.createWallet();
|
|
94
|
+
secret = wallet.secret;
|
|
95
|
+
keypairs.address = wallet.address;
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
if (!plugins_1.jtWallet.isValidSecret(secret)) {
|
|
99
|
+
throw new Error(constant_1.SECRET_IS_INVALID);
|
|
81
100
|
}
|
|
82
|
-
keypairs.
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
101
|
+
keypairs.address = plugins_1.jtWallet.getAddress(secret);
|
|
102
|
+
}
|
|
103
|
+
keypairs.secret = secret;
|
|
104
|
+
keypairs.type = "swt";
|
|
105
|
+
keypairs.default = true;
|
|
106
|
+
keypairs.alias = alias || "swt wallet";
|
|
107
|
+
const jcWallet = {};
|
|
108
|
+
const walletObj = await (0, util_2.encryptWallet)(password, keypairs);
|
|
109
|
+
jcWallet.version = JingchangWallet.version;
|
|
110
|
+
jcWallet.id = JingchangWallet._walletID;
|
|
111
|
+
jcWallet.contact = {};
|
|
112
|
+
jcWallet.wallets = [];
|
|
113
|
+
jcWallet.wallets.push(walletObj);
|
|
114
|
+
return jcWallet;
|
|
95
115
|
}
|
|
96
116
|
/**
|
|
97
117
|
* get jingchang wallet from local storage
|
|
@@ -155,12 +175,12 @@ class JingchangWallet {
|
|
|
155
175
|
* @memberof JingchangWallet
|
|
156
176
|
*/
|
|
157
177
|
static async encryptWithPublicKey(message, publicKey) {
|
|
158
|
-
const encode = await
|
|
178
|
+
const encode = await eccrypto.encrypt(Buffer.from(publicKey, "hex"), Buffer.from(message));
|
|
159
179
|
return {
|
|
160
|
-
ciphertext: encode.ciphertext.toString("hex"),
|
|
161
|
-
ephemPublicKey: encode.ephemPublicKey.toString("hex"),
|
|
162
|
-
iv: encode.iv.toString("hex"),
|
|
163
|
-
mac: encode.mac.toString("hex")
|
|
180
|
+
ciphertext: Buffer.from(encode.ciphertext).toString("hex"),
|
|
181
|
+
ephemPublicKey: Buffer.from(encode.ephemPublicKey).toString("hex"),
|
|
182
|
+
iv: Buffer.from(encode.iv).toString("hex"),
|
|
183
|
+
mac: Buffer.from(encode.mac).toString("hex")
|
|
164
184
|
};
|
|
165
185
|
}
|
|
166
186
|
/**
|
|
@@ -179,8 +199,8 @@ class JingchangWallet {
|
|
|
179
199
|
iv: Buffer.from(message.iv, "hex"),
|
|
180
200
|
mac: Buffer.from(message.mac, "hex")
|
|
181
201
|
};
|
|
182
|
-
const decode = await
|
|
183
|
-
return decode.toString();
|
|
202
|
+
const decode = await eccrypto.decrypt(Buffer.from(privateKey, "hex"), encode);
|
|
203
|
+
return Buffer.from(decode).toString();
|
|
184
204
|
}
|
|
185
205
|
/**
|
|
186
206
|
* get wallets from jingchang wallet
|
|
@@ -252,7 +272,7 @@ class JingchangWallet {
|
|
|
252
272
|
hasDefault(type = "swt") {
|
|
253
273
|
try {
|
|
254
274
|
const wallet = this.findWallet((w) => w.type === type && w.default);
|
|
255
|
-
return !(0,
|
|
275
|
+
return !(0, util_1.isEmptyPlainObject)(wallet);
|
|
256
276
|
}
|
|
257
277
|
catch (error) {
|
|
258
278
|
return false;
|
|
@@ -268,8 +288,8 @@ class JingchangWallet {
|
|
|
268
288
|
*/
|
|
269
289
|
async getSecretWithType(password, type = "swt") {
|
|
270
290
|
const wallet = await this.findWallet((w) => w.type === type && w.default);
|
|
271
|
-
const secret = (0,
|
|
272
|
-
return secret;
|
|
291
|
+
const secret = await (0, util_2.decrypt)(password, wallet);
|
|
292
|
+
return secret.toString();
|
|
273
293
|
}
|
|
274
294
|
/**
|
|
275
295
|
* get the wallet keystore's secret with address.
|
|
@@ -281,8 +301,8 @@ class JingchangWallet {
|
|
|
281
301
|
*/
|
|
282
302
|
async getSecretWithAddress(password, address) {
|
|
283
303
|
const wallet = await this.findWallet((w) => w.address === address);
|
|
284
|
-
const secret = (0,
|
|
285
|
-
return secret;
|
|
304
|
+
const secret = await (0, util_2.decrypt)(password, wallet);
|
|
305
|
+
return secret.toString();
|
|
286
306
|
}
|
|
287
307
|
/**
|
|
288
308
|
* change the whole jingchang wallet password, if you set property of _samePassword is false, will throw an error
|
|
@@ -297,7 +317,6 @@ class JingchangWallet {
|
|
|
297
317
|
throw new Error("the property of _samePassword is false, so please don't call this function!");
|
|
298
318
|
}
|
|
299
319
|
const jcWallet = (0, clone_deep_1.default)(this._jingchangWallet);
|
|
300
|
-
assert_1.default.notEqual(jcWallet, this._jingchangWallet);
|
|
301
320
|
const wallets = JingchangWallet.getWallets(jcWallet);
|
|
302
321
|
const arr = [];
|
|
303
322
|
for (const wallet of wallets) {
|
|
@@ -310,7 +329,7 @@ class JingchangWallet {
|
|
|
310
329
|
secret,
|
|
311
330
|
type: wallet.type
|
|
312
331
|
};
|
|
313
|
-
const newWallet = this.getEncryptData(newPassword, keypairs);
|
|
332
|
+
const newWallet = await this.getEncryptData(newPassword, keypairs);
|
|
314
333
|
arr.push(newWallet);
|
|
315
334
|
}
|
|
316
335
|
jcWallet.wallets = arr;
|
|
@@ -339,7 +358,7 @@ class JingchangWallet {
|
|
|
339
358
|
secret,
|
|
340
359
|
type: wallet.type
|
|
341
360
|
};
|
|
342
|
-
const newWallet = this.getEncryptData(newPassword, keypairs);
|
|
361
|
+
const newWallet = await this.getEncryptData(newPassword, keypairs);
|
|
343
362
|
// shadow copy
|
|
344
363
|
wallet.ciphertext = newWallet.ciphertext;
|
|
345
364
|
wallet.crypto = newWallet.crypto;
|
|
@@ -365,7 +384,7 @@ class JingchangWallet {
|
|
|
365
384
|
secret,
|
|
366
385
|
type: wallet.type
|
|
367
386
|
};
|
|
368
|
-
const newWallet = this.getEncryptData(password, keypairs);
|
|
387
|
+
const newWallet = await this.getEncryptData(password, keypairs);
|
|
369
388
|
// shadow copy
|
|
370
389
|
wallet.ciphertext = newWallet.ciphertext;
|
|
371
390
|
wallet.crypto = newWallet.crypto;
|
|
@@ -393,7 +412,6 @@ class JingchangWallet {
|
|
|
393
412
|
*/
|
|
394
413
|
async removeWalletWithAddress(address) {
|
|
395
414
|
const jcWallet = (0, clone_deep_1.default)(this._jingchangWallet);
|
|
396
|
-
assert_1.default.notEqual(this._jingchangWallet, jcWallet);
|
|
397
415
|
const wallets = JingchangWallet.getWallets(jcWallet);
|
|
398
416
|
const wallet = this.findWallet((w) => w.address === address);
|
|
399
417
|
const index = wallets.findIndex((w) => w.address === wallet.address);
|
|
@@ -472,7 +490,7 @@ class JingchangWallet {
|
|
|
472
490
|
}
|
|
473
491
|
const { wallets } = jingchangWallet;
|
|
474
492
|
const wallet = wallets.find(filter);
|
|
475
|
-
if ((0,
|
|
493
|
+
if ((0, util_1.isEmptyPlainObject)(wallet)) {
|
|
476
494
|
throw new Error(constant_1.WALLET_IS_EMPTY);
|
|
477
495
|
}
|
|
478
496
|
return wallet;
|
|
@@ -486,8 +504,8 @@ class JingchangWallet {
|
|
|
486
504
|
* @returns {IKeystoreModel}
|
|
487
505
|
* @memberof JingchangWallet
|
|
488
506
|
*/
|
|
489
|
-
getEncryptData(password, keypairs) {
|
|
490
|
-
const encryptData = (0,
|
|
507
|
+
async getEncryptData(password, keypairs) {
|
|
508
|
+
const encryptData = await (0, util_2.encryptWallet)(password, keypairs, {});
|
|
491
509
|
return encryptData;
|
|
492
510
|
}
|
|
493
511
|
/**
|
|
@@ -502,9 +520,8 @@ class JingchangWallet {
|
|
|
502
520
|
async saveWallet(password, keypairs) {
|
|
503
521
|
// support type: ethereum, stream, jingtum, call and moac
|
|
504
522
|
keypairs.default = this._multiple ? !this.hasDefault(keypairs.type) : true;
|
|
505
|
-
const encryptData = this.getEncryptData(password, keypairs);
|
|
523
|
+
const encryptData = await this.getEncryptData(password, keypairs);
|
|
506
524
|
const jcWallet = (0, clone_deep_1.default)(this._jingchangWallet);
|
|
507
|
-
assert_1.default.notEqual(this._jingchangWallet, jcWallet);
|
|
508
525
|
const wallets = jcWallet.wallets;
|
|
509
526
|
const pre = wallets.findIndex((w) => w.type.toLowerCase() === keypairs.type.toLowerCase());
|
|
510
527
|
if (this._multiple) {
|
|
@@ -528,4 +545,3 @@ JingchangWallet._walletID = Buffer.from(sha256_1.sha256
|
|
|
528
545
|
.update(JingchangWallet._name.toLowerCase())
|
|
529
546
|
.digest()).toString("hex");
|
|
530
547
|
exports.default = JingchangWallet;
|
|
531
|
-
//# sourceMappingURL=jingchangWallet.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* forked from https://github.com/EOSIO/eosjs/blob/master/src/eosjs-key-conversions.ts
|
|
3
|
+
*
|
|
4
|
+
* rewrite curves
|
|
5
|
+
*/
|
|
6
|
+
import BN from "bn.js";
|
|
7
|
+
import { Key, KeyType } from "./eosjs-numeric";
|
|
8
|
+
import { ProjPointType, CurveFn } from "@noble/curves/abstract/weierstrass";
|
|
9
|
+
import { PublicKey, Signature } from "./eosjs-key-conversions";
|
|
10
|
+
/** Represents/stores a private key and provides easy conversion for use with `elliptic` lib */
|
|
11
|
+
export declare class PrivateKey {
|
|
12
|
+
private key;
|
|
13
|
+
private ec;
|
|
14
|
+
constructor(key: Key, ec: CurveFn);
|
|
15
|
+
/** Instantiate private key from an EOSIO-format private key */
|
|
16
|
+
static fromString(keyString: string, ec?: CurveFn): PrivateKey;
|
|
17
|
+
/** Export private key as `elliptic`-format private key */
|
|
18
|
+
toPoint(): ProjPointType<bigint>;
|
|
19
|
+
/** Get key type from key */
|
|
20
|
+
getType(): KeyType;
|
|
21
|
+
/** Retrieve the public key from a private key */
|
|
22
|
+
getPublicKey(): PublicKey;
|
|
23
|
+
/** Sign a message or hashed message digest with private key */
|
|
24
|
+
sign(data: BN, shouldHash?: boolean, encoding?: BufferEncoding): Signature;
|
|
25
|
+
/** Validate a private key */
|
|
26
|
+
isValid(): boolean;
|
|
27
|
+
}
|