@transia/ripple-keypairs 1.1.6-alpha.3 → 2.0.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.
- package/README.md +1 -1
- package/dist/index.d.ts +12 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +42 -102
- package/dist/index.js.map +1 -1
- package/dist/signing-schemes/ed25519/index.d.ts +4 -0
- package/dist/signing-schemes/ed25519/index.d.ts.map +1 -0
- package/dist/signing-schemes/ed25519/index.js +38 -0
- package/dist/signing-schemes/ed25519/index.js.map +1 -0
- package/dist/signing-schemes/secp256k1/index.d.ts +4 -0
- package/dist/signing-schemes/secp256k1/index.d.ts.map +1 -0
- package/dist/signing-schemes/secp256k1/index.js +43 -0
- package/dist/signing-schemes/secp256k1/index.js.map +1 -0
- package/dist/{secp256k1.d.ts → signing-schemes/secp256k1/utils.d.ts} +5 -6
- package/dist/signing-schemes/secp256k1/utils.d.ts.map +1 -0
- package/dist/{secp256k1.js → signing-schemes/secp256k1/utils.js} +17 -43
- package/dist/signing-schemes/secp256k1/utils.js.map +1 -0
- package/dist/src/index.d.ts +17 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +73 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/signing-schemes/ed25519/index.d.ts +4 -0
- package/dist/src/signing-schemes/ed25519/index.d.ts.map +1 -0
- package/dist/src/signing-schemes/ed25519/index.js +38 -0
- package/dist/src/signing-schemes/ed25519/index.js.map +1 -0
- package/dist/src/signing-schemes/secp256k1/index.d.ts +4 -0
- package/dist/src/signing-schemes/secp256k1/index.d.ts.map +1 -0
- package/dist/src/signing-schemes/secp256k1/index.js +43 -0
- package/dist/src/signing-schemes/secp256k1/index.js.map +1 -0
- package/dist/src/signing-schemes/secp256k1/utils.d.ts +14 -0
- package/dist/src/signing-schemes/secp256k1/utils.d.ts.map +1 -0
- package/dist/src/signing-schemes/secp256k1/utils.js +71 -0
- package/dist/src/signing-schemes/secp256k1/utils.js.map +1 -0
- package/dist/src/types.d.ts +17 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +3 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/utils/Sha512.d.ts +12 -0
- package/dist/src/utils/Sha512.d.ts.map +1 -0
- package/dist/src/utils/Sha512.js +33 -0
- package/dist/src/utils/Sha512.js.map +1 -0
- package/dist/src/utils/assert.d.ts +5 -0
- package/dist/src/utils/assert.d.ts.map +1 -0
- package/dist/src/utils/assert.js +11 -0
- package/dist/src/utils/assert.js.map +1 -0
- package/dist/src/utils/getAlgorithmFromKey.d.ts +13 -0
- package/dist/src/utils/getAlgorithmFromKey.d.ts.map +1 -0
- package/dist/src/utils/getAlgorithmFromKey.js +104 -0
- package/dist/src/utils/getAlgorithmFromKey.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types.d.ts +17 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/Sha512.d.ts +12 -0
- package/dist/utils/Sha512.d.ts.map +1 -0
- package/dist/utils/Sha512.js +33 -0
- package/dist/utils/Sha512.js.map +1 -0
- package/dist/utils/assert.d.ts +5 -0
- package/dist/utils/assert.d.ts.map +1 -0
- package/dist/utils/assert.js +11 -0
- package/dist/utils/assert.js.map +1 -0
- package/dist/utils/getAlgorithmFromKey.d.ts +13 -0
- package/dist/utils/getAlgorithmFromKey.d.ts.map +1 -0
- package/dist/utils/getAlgorithmFromKey.js +104 -0
- package/dist/utils/getAlgorithmFromKey.js.map +1 -0
- package/package.json +19 -16
- package/src/index.ts +112 -0
- package/src/signing-schemes/ed25519/index.ts +56 -0
- package/src/signing-schemes/secp256k1/index.ts +64 -0
- package/src/signing-schemes/secp256k1/utils.ts +75 -0
- package/src/types.ts +35 -0
- package/src/utils/Sha512.ts +36 -0
- package/src/utils/assert.ts +11 -0
- package/src/utils/getAlgorithmFromKey.ts +121 -0
- package/dist/Sha512.d.ts +0 -10
- package/dist/Sha512.d.ts.map +0 -1
- package/dist/Sha512.js +0 -57
- package/dist/Sha512.js.map +0 -1
- package/dist/secp256k1.d.ts.map +0 -1
- package/dist/secp256k1.js.map +0 -1
- package/dist/utils.d.ts +0 -6
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js +0 -51
- package/dist/utils.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# ripple-keypairs [](https://npmjs.org/package/ripple-keypairs) [](https://travis-ci.org/ripple/ripple-keypairs) 
|
|
2
2
|
|
|
3
3
|
An implementation of XRP Ledger keypairs & wallet generation using
|
|
4
|
-
[
|
|
4
|
+
[noble](https://paulmillr.com/noble) which supports rfc6979 and
|
|
5
5
|
eddsa deterministic signatures.
|
|
6
6
|
|
|
7
7
|
[](https://www.npmjs.org/package/ripple-keypairs)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { decodeSeed } from '@transia/ripple-address-codec';
|
|
2
|
+
import type { Algorithm, HexString, KeyPair } from './types';
|
|
2
3
|
declare function generateSeed(options?: {
|
|
3
4
|
entropy?: Uint8Array;
|
|
4
|
-
algorithm?:
|
|
5
|
+
algorithm?: Algorithm;
|
|
5
6
|
}): string;
|
|
6
|
-
declare function deriveKeypair(seed: string, options?:
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
declare function
|
|
12
|
-
declare function
|
|
13
|
-
declare function
|
|
14
|
-
declare
|
|
7
|
+
declare function deriveKeypair(seed: string, options?: {
|
|
8
|
+
algorithm?: Algorithm;
|
|
9
|
+
validator?: boolean;
|
|
10
|
+
accountIndex?: number;
|
|
11
|
+
}): KeyPair;
|
|
12
|
+
declare function sign(messageHex: HexString, privateKey: HexString): HexString;
|
|
13
|
+
declare function verify(messageHex: HexString, signature: HexString, publicKey: HexString): boolean;
|
|
14
|
+
declare function deriveAddress(publicKey: string): string;
|
|
15
|
+
declare function deriveNodeAddress(publicKey: string): string;
|
|
15
16
|
export { generateSeed, deriveKeypair, sign, verify, deriveAddress, deriveNodeAddress, decodeSeed, };
|
|
16
17
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,UAAU,EAGX,MAAM,+BAA+B,CAAA;AAQtC,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAiB,MAAM,SAAS,CAAA;AAc3E,iBAAS,YAAY,CACnB,OAAO,GAAE;IACP,OAAO,CAAC,EAAE,UAAU,CAAA;IACpB,SAAS,CAAC,EAAE,SAAS,CAAA;CACjB,GACL,MAAM,CAUR;AAED,iBAAS,aAAa,CACpB,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE;IACR,SAAS,CAAC,EAAE,SAAS,CAAA;IACrB,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB,GACA,OAAO,CAcT;AAED,iBAAS,IAAI,CAAC,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,GAAG,SAAS,CAGrE;AAED,iBAAS,MAAM,CACb,UAAU,EAAE,SAAS,EACrB,SAAS,EAAE,SAAS,EACpB,SAAS,EAAE,SAAS,GACnB,OAAO,CAOT;AAUD,iBAAS,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED,iBAAS,iBAAiB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAIpD;AAED,OAAO,EACL,YAAY,EACZ,aAAa,EACb,IAAI,EACJ,MAAM,EACN,aAAa,EACb,iBAAiB,EACjB,UAAU,GACX,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,133 +1,73 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
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;
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
4
|
};
|
|
25
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
6
|
exports.decodeSeed = exports.deriveNodeAddress = exports.deriveAddress = exports.verify = exports.sign = exports.deriveKeypair = exports.generateSeed = void 0;
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
const
|
|
37
|
-
const
|
|
7
|
+
const ripple_address_codec_1 = require("@transia/ripple-address-codec");
|
|
8
|
+
Object.defineProperty(exports, "decodeSeed", { enumerable: true, get: function () { return ripple_address_codec_1.decodeSeed; } });
|
|
9
|
+
const ripemd160_1 = require("@transia/isomorphic/ripemd160");
|
|
10
|
+
const sha256_1 = require("@transia/isomorphic/sha256");
|
|
11
|
+
const utils_1 = require("@transia/isomorphic/utils");
|
|
12
|
+
const utils_2 = require("./signing-schemes/secp256k1/utils");
|
|
13
|
+
const Sha512_1 = __importDefault(require("./utils/Sha512"));
|
|
14
|
+
const assert_1 = __importDefault(require("./utils/assert"));
|
|
15
|
+
const getAlgorithmFromKey_1 = require("./utils/getAlgorithmFromKey");
|
|
16
|
+
const secp256k1_1 = __importDefault(require("./signing-schemes/secp256k1"));
|
|
17
|
+
const ed25519_1 = __importDefault(require("./signing-schemes/ed25519"));
|
|
18
|
+
function getSigningScheme(algorithm) {
|
|
19
|
+
const schemes = { 'ecdsa-secp256k1': secp256k1_1.default, ed25519: ed25519_1.default };
|
|
20
|
+
return schemes[algorithm];
|
|
21
|
+
}
|
|
38
22
|
function generateSeed(options = {}) {
|
|
39
|
-
|
|
40
|
-
const entropy = options.entropy
|
|
23
|
+
assert_1.default.ok(!options.entropy || options.entropy.length >= 16, 'entropy too short');
|
|
24
|
+
const entropy = options.entropy
|
|
25
|
+
? options.entropy.slice(0, 16)
|
|
26
|
+
: (0, utils_1.randomBytes)(16);
|
|
41
27
|
const type = options.algorithm === 'ed25519' ? 'ed25519' : 'secp256k1';
|
|
42
|
-
return
|
|
28
|
+
return (0, ripple_address_codec_1.encodeSeed)(entropy, type);
|
|
43
29
|
}
|
|
44
30
|
exports.generateSeed = generateSeed;
|
|
45
|
-
function hash(message) {
|
|
46
|
-
return hashjs.sha512().update(message).digest().slice(0, 32);
|
|
47
|
-
}
|
|
48
|
-
const secp256k1 = {
|
|
49
|
-
deriveKeypair(entropy, options) {
|
|
50
|
-
const prefix = '00';
|
|
51
|
-
const privateKey = prefix + (0, secp256k1_1.derivePrivateKey)(entropy, options).toString(16, 64).toUpperCase();
|
|
52
|
-
const publicKey = bytesToHex(Secp256k1.keyFromPrivate(privateKey.slice(2))
|
|
53
|
-
.getPublic()
|
|
54
|
-
.encodeCompressed());
|
|
55
|
-
return { privateKey, publicKey };
|
|
56
|
-
},
|
|
57
|
-
sign(message, privateKey) {
|
|
58
|
-
return bytesToHex(Secp256k1.sign(hash(message), hexToBytes(privateKey), {
|
|
59
|
-
canonical: true,
|
|
60
|
-
}).toDER());
|
|
61
|
-
},
|
|
62
|
-
verify(message, signature, publicKey) {
|
|
63
|
-
return Secp256k1.verify(hash(message), signature, hexToBytes(publicKey));
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
|
-
const ed25519 = {
|
|
67
|
-
deriveKeypair(entropy) {
|
|
68
|
-
const prefix = 'ED';
|
|
69
|
-
const rawPrivateKey = hash(entropy);
|
|
70
|
-
const privateKey = prefix + bytesToHex(rawPrivateKey);
|
|
71
|
-
const publicKey = prefix + bytesToHex(Ed25519.keyFromSecret(rawPrivateKey).pubBytes());
|
|
72
|
-
return { privateKey, publicKey };
|
|
73
|
-
},
|
|
74
|
-
sign(message, privateKey) {
|
|
75
|
-
// caution: Ed25519.sign interprets all strings as hex, stripping
|
|
76
|
-
// any non-hex characters without warning
|
|
77
|
-
assert.ok(Array.isArray(message), 'message must be array of octets');
|
|
78
|
-
return bytesToHex(Ed25519.sign(message, hexToBytes(privateKey).slice(1)).toBytes());
|
|
79
|
-
},
|
|
80
|
-
verify(message, signature, publicKey) {
|
|
81
|
-
return Ed25519.verify(message, hexToBytes(signature), hexToBytes(publicKey).slice(1));
|
|
82
|
-
},
|
|
83
|
-
};
|
|
84
|
-
function select(algorithm) {
|
|
85
|
-
const methods = { 'ecdsa-secp256k1': secp256k1, ed25519 };
|
|
86
|
-
return methods[algorithm];
|
|
87
|
-
}
|
|
88
31
|
function deriveKeypair(seed, options) {
|
|
89
|
-
|
|
90
|
-
const
|
|
91
|
-
const
|
|
92
|
-
const
|
|
93
|
-
const
|
|
94
|
-
const
|
|
32
|
+
var _a;
|
|
33
|
+
const decoded = (0, ripple_address_codec_1.decodeSeed)(seed);
|
|
34
|
+
const proposedAlgorithm = (_a = options === null || options === void 0 ? void 0 : options.algorithm) !== null && _a !== void 0 ? _a : decoded.type;
|
|
35
|
+
const algorithm = proposedAlgorithm === 'ed25519' ? 'ed25519' : 'ecdsa-secp256k1';
|
|
36
|
+
const scheme = getSigningScheme(algorithm);
|
|
37
|
+
const keypair = scheme.deriveKeypair(decoded.bytes, options);
|
|
38
|
+
const messageToVerify = Sha512_1.default.half('This test message should verify.');
|
|
39
|
+
const signature = scheme.sign(messageToVerify, keypair.privateKey);
|
|
95
40
|
/* istanbul ignore if */
|
|
96
|
-
if (
|
|
41
|
+
if (!scheme.verify(messageToVerify, signature, keypair.publicKey)) {
|
|
97
42
|
throw new Error('derived keypair did not generate verifiable signature');
|
|
98
43
|
}
|
|
99
44
|
return keypair;
|
|
100
45
|
}
|
|
101
46
|
exports.deriveKeypair = deriveKeypair;
|
|
102
|
-
function getAlgorithmFromKey(key) {
|
|
103
|
-
const bytes = hexToBytes(key);
|
|
104
|
-
return bytes.length === 33 && bytes[0] === 0xed
|
|
105
|
-
? 'ed25519'
|
|
106
|
-
: 'ecdsa-secp256k1';
|
|
107
|
-
}
|
|
108
47
|
function sign(messageHex, privateKey) {
|
|
109
|
-
const algorithm =
|
|
110
|
-
return
|
|
48
|
+
const algorithm = (0, getAlgorithmFromKey_1.getAlgorithmFromPrivateKey)(privateKey);
|
|
49
|
+
return getSigningScheme(algorithm).sign((0, utils_1.hexToBytes)(messageHex), privateKey);
|
|
111
50
|
}
|
|
112
51
|
exports.sign = sign;
|
|
113
52
|
function verify(messageHex, signature, publicKey) {
|
|
114
|
-
const algorithm =
|
|
115
|
-
return
|
|
53
|
+
const algorithm = (0, getAlgorithmFromKey_1.getAlgorithmFromPublicKey)(publicKey);
|
|
54
|
+
return getSigningScheme(algorithm).verify((0, utils_1.hexToBytes)(messageHex), signature, publicKey);
|
|
116
55
|
}
|
|
117
56
|
exports.verify = verify;
|
|
57
|
+
function computePublicKeyHash(publicKeyBytes) {
|
|
58
|
+
return (0, ripemd160_1.ripemd160)((0, sha256_1.sha256)(publicKeyBytes));
|
|
59
|
+
}
|
|
118
60
|
function deriveAddressFromBytes(publicKeyBytes) {
|
|
119
|
-
return
|
|
61
|
+
return (0, ripple_address_codec_1.encodeAccountID)(computePublicKeyHash(publicKeyBytes));
|
|
120
62
|
}
|
|
121
63
|
function deriveAddress(publicKey) {
|
|
122
|
-
return deriveAddressFromBytes(
|
|
64
|
+
return deriveAddressFromBytes((0, utils_1.hexToBytes)(publicKey));
|
|
123
65
|
}
|
|
124
66
|
exports.deriveAddress = deriveAddress;
|
|
125
67
|
function deriveNodeAddress(publicKey) {
|
|
126
|
-
const generatorBytes =
|
|
127
|
-
const accountPublicBytes = (0,
|
|
68
|
+
const generatorBytes = (0, ripple_address_codec_1.decodeNodePublic)(publicKey);
|
|
69
|
+
const accountPublicBytes = (0, utils_2.accountPublicFromPublicGenerator)(generatorBytes);
|
|
128
70
|
return deriveAddressFromBytes(accountPublicBytes);
|
|
129
71
|
}
|
|
130
72
|
exports.deriveNodeAddress = deriveNodeAddress;
|
|
131
|
-
const { decodeSeed } = addressCodec;
|
|
132
|
-
exports.decodeSeed = decodeSeed;
|
|
133
73
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,wEAKsC;AAyGpC,2FA5GA,iCAAU,OA4GA;AAxGZ,6DAAyD;AACzD,uDAAmD;AACnD,qDAAmE;AAEnE,6DAAoF;AACpF,4DAAmC;AACnC,4DAAmC;AAEnC,qEAGoC;AAEpC,4EAAmD;AACnD,wEAA+C;AAE/C,SAAS,gBAAgB,CAAC,SA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IAAA,uCAAgB,EAAC,SAAS,CAAC,CAAA;IAClD,MAAM,kBAAkB,GAAG,IAAA,wCAAgC,EAAC,cAAc,CAAC,CAAA;IAC3E,OAAO,sBAAsB,CAAC,kBAAkB,CAAC,CAAA;AACnD,CAAC;AAQC,8CAAiB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/signing-schemes/ed25519/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAa,aAAa,EAAE,MAAM,aAAa,CAAA;AAM3D,QAAA,MAAM,OAAO,EAAE,aA4Cd,CAAA;AAED,eAAe,OAAO,CAAA"}
|
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
const ed25519_1 = require("@noble/curves/ed25519");
|
|
7
|
+
const utils_1 = require("@transia/isomorphic/utils");
|
|
8
|
+
const assert_1 = __importDefault(require("../../utils/assert"));
|
|
9
|
+
const Sha512_1 = __importDefault(require("../../utils/Sha512"));
|
|
10
|
+
const ED_PREFIX = 'ED';
|
|
11
|
+
const ed25519 = {
|
|
12
|
+
deriveKeypair(entropy) {
|
|
13
|
+
const rawPrivateKey = Sha512_1.default.half(entropy);
|
|
14
|
+
const privateKey = ED_PREFIX + (0, utils_1.bytesToHex)(rawPrivateKey);
|
|
15
|
+
const publicKey = ED_PREFIX + (0, utils_1.bytesToHex)(ed25519_1.ed25519.getPublicKey(rawPrivateKey));
|
|
16
|
+
return { privateKey, publicKey };
|
|
17
|
+
},
|
|
18
|
+
sign(message, privateKey) {
|
|
19
|
+
assert_1.default.ok(message instanceof Uint8Array, 'message must be array of octets');
|
|
20
|
+
assert_1.default.ok(privateKey.length === 66, 'private key must be 33 bytes including prefix');
|
|
21
|
+
return (0, utils_1.bytesToHex)(ed25519_1.ed25519.sign(message, privateKey.slice(2)));
|
|
22
|
+
},
|
|
23
|
+
verify(message, signature, publicKey) {
|
|
24
|
+
// Unlikely to be triggered as these are internal and guarded by getAlgorithmFromKey
|
|
25
|
+
assert_1.default.ok(publicKey.length === 66, 'public key must be 33 bytes including prefix');
|
|
26
|
+
return ed25519_1.ed25519.verify(signature, message,
|
|
27
|
+
// Remove the 0xED prefix
|
|
28
|
+
publicKey.slice(2),
|
|
29
|
+
// By default, set zip215 to false for compatibility reasons.
|
|
30
|
+
// ZIP 215 is a stricter Ed25519 signature verification scheme.
|
|
31
|
+
// However, setting it to false adheres to the more commonly used
|
|
32
|
+
// RFC8032 / NIST186-5 standards, making it compatible with systems
|
|
33
|
+
// like the XRP Ledger.
|
|
34
|
+
{ zip215: false });
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
exports.default = ed25519;
|
|
38
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/signing-schemes/ed25519/index.ts"],"names":[],"mappings":";;;;;AAAA,mDAA+D;AAC/D,qDAAsD;AAGtD,gEAAuC;AACvC,gEAAuC;AAEvC,MAAM,SAAS,GAAG,IAAI,CAAA;AAEtB,MAAM,OAAO,GAAkB;IAC7B,aAAa,CAAC,OAAmB;QAI/B,MAAM,aAAa,GAAG,gBAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QAC1C,MAAM,UAAU,GAAG,SAAS,GAAG,IAAA,kBAAU,EAAC,aAAa,CAAC,CAAA;QACxD,MAAM,SAAS,GACb,SAAS,GAAG,IAAA,kBAAU,EAAC,iBAAY,CAAC,YAAY,CAAC,aAAa,CAAC,CAAC,CAAA;QAClE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,CAAA;IAClC,CAAC;IAED,IAAI,CAAC,OAAmB,EAAE,UAAqB;QAC7C,gBAAM,CAAC,EAAE,CAAC,OAAO,YAAY,UAAU,EAAE,iCAAiC,CAAC,CAAA;QAC3E,gBAAM,CAAC,EAAE,CACP,UAAU,CAAC,MAAM,KAAK,EAAE,EACxB,+CAA+C,CAChD,CAAA;QACD,OAAO,IAAA,kBAAU,EAAC,iBAAY,CAAC,IAAI,CAAC,OAAO,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IACpE,CAAC;IAED,MAAM,CACJ,OAAmB,EACnB,SAAoB,EACpB,SAAiB;QAEjB,oFAAoF;QACpF,gBAAM,CAAC,EAAE,CACP,SAAS,CAAC,MAAM,KAAK,EAAE,EACvB,8CAA8C,CAC/C,CAAA;QACD,OAAO,iBAAY,CAAC,MAAM,CACxB,SAAS,EACT,OAAO;QACP,yBAAyB;QACzB,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;QAClB,6DAA6D;QAC7D,+DAA+D;QAC/D,iEAAiE;QACjE,mEAAmE;QACnE,uBAAuB;QACvB,EAAE,MAAM,EAAE,KAAK,EAAE,CAClB,CAAA;IACH,CAAC;CACF,CAAA;AAED,kBAAe,OAAO,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/signing-schemes/secp256k1/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAGV,aAAa,EACd,MAAM,aAAa,CAAA;AAQpB,QAAA,MAAM,SAAS,EAAE,aA6ChB,CAAA;AAED,eAAe,SAAS,CAAA"}
|
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
const utils_1 = require("@noble/curves/abstract/utils");
|
|
7
|
+
const secp256k1_1 = require("@noble/curves/secp256k1");
|
|
8
|
+
const utils_2 = require("@transia/isomorphic/utils");
|
|
9
|
+
const utils_3 = require("./utils");
|
|
10
|
+
const assert_1 = __importDefault(require("../../utils/assert"));
|
|
11
|
+
const Sha512_1 = __importDefault(require("../../utils/Sha512"));
|
|
12
|
+
const SECP256K1_PREFIX = '00';
|
|
13
|
+
const secp256k1 = {
|
|
14
|
+
deriveKeypair(entropy, options) {
|
|
15
|
+
const derived = (0, utils_3.derivePrivateKey)(entropy, options);
|
|
16
|
+
const privateKey = SECP256K1_PREFIX + (0, utils_2.bytesToHex)((0, utils_1.numberToBytesBE)(derived, 32));
|
|
17
|
+
const publicKey = (0, utils_2.bytesToHex)(secp256k1_1.secp256k1.getPublicKey(derived, true));
|
|
18
|
+
return { privateKey, publicKey };
|
|
19
|
+
},
|
|
20
|
+
sign(message, privateKey) {
|
|
21
|
+
// Some callers pass the privateKey with the prefix, others without.
|
|
22
|
+
// @noble/curves will throw if the key is not exactly 32 bytes, so we
|
|
23
|
+
// normalize it before passing to the sign method.
|
|
24
|
+
assert_1.default.ok((privateKey.length === 66 && privateKey.startsWith(SECP256K1_PREFIX)) ||
|
|
25
|
+
privateKey.length === 64);
|
|
26
|
+
const normedPrivateKey = privateKey.length === 66 ? privateKey.slice(2) : privateKey;
|
|
27
|
+
return secp256k1_1.secp256k1
|
|
28
|
+
.sign(Sha512_1.default.half(message), normedPrivateKey, {
|
|
29
|
+
// "Canonical" signatures
|
|
30
|
+
lowS: true,
|
|
31
|
+
// Would fail tests if signatures aren't deterministic
|
|
32
|
+
extraEntropy: undefined,
|
|
33
|
+
})
|
|
34
|
+
.toDERHex(true)
|
|
35
|
+
.toUpperCase();
|
|
36
|
+
},
|
|
37
|
+
verify(message, signature, publicKey) {
|
|
38
|
+
const decoded = secp256k1_1.secp256k1.Signature.fromDER(signature);
|
|
39
|
+
return secp256k1_1.secp256k1.verify(decoded, Sha512_1.default.half(message), publicKey);
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
exports.default = secp256k1;
|
|
43
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/signing-schemes/secp256k1/index.ts"],"names":[],"mappings":";;;;;AAAA,wDAA8D;AAC9D,uDAAqE;AACrE,qDAAsD;AAQtD,mCAA0C;AAC1C,gEAAuC;AACvC,gEAAuC;AAEvC,MAAM,gBAAgB,GAAG,IAAI,CAAA;AAE7B,MAAM,SAAS,GAAkB;IAC/B,aAAa,CACX,OAAmB,EACnB,OAA8B;QAK9B,MAAM,OAAO,GAAG,IAAA,wBAAgB,EAAC,OAAO,EAAE,OAAO,CAAC,CAAA;QAClD,MAAM,UAAU,GACd,gBAAgB,GAAG,IAAA,kBAAU,EAAC,IAAA,uBAAe,EAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAA;QAE7D,MAAM,SAAS,GAAG,IAAA,kBAAU,EAAC,qBAAc,CAAC,YAAY,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAA;QACxE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,CAAA;IAClC,CAAC;IAED,IAAI,CAAC,OAAmB,EAAE,UAAqB;QAC7C,oEAAoE;QACpE,qEAAqE;QACrE,kDAAkD;QAClD,gBAAM,CAAC,EAAE,CACP,CAAC,UAAU,CAAC,MAAM,KAAK,EAAE,IAAI,UAAU,CAAC,UAAU,CAAC,gBAAgB,CAAC,CAAC;YACnE,UAAU,CAAC,MAAM,KAAK,EAAE,CAC3B,CAAA;QACD,MAAM,gBAAgB,GACpB,UAAU,CAAC,MAAM,KAAK,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,UAAU,CAAA;QAC7D,OAAO,qBAAc;aAClB,IAAI,CAAC,gBAAM,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,gBAAgB,EAAE;YAC5C,yBAAyB;YACzB,IAAI,EAAE,IAAI;YACV,sDAAsD;YACtD,YAAY,EAAE,SAAS;SACxB,CAAC;aACD,QAAQ,CAAC,IAAI,CAAC;aACd,WAAW,EAAE,CAAA;IAClB,CAAC;IAED,MAAM,CACJ,OAAmB,EACnB,SAAoB,EACpB,SAAoB;QAEpB,MAAM,OAAO,GAAG,qBAAc,CAAC,SAAS,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA;QAC3D,OAAO,qBAAc,CAAC,MAAM,CAAC,OAAO,EAAE,gBAAM,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,SAAS,CAAC,CAAA;IACxE,CAAC;CACF,CAAA;AAED,kBAAe,SAAS,CAAA"}
|
|
@@ -4,12 +4,11 @@
|
|
|
4
4
|
* @param [opts.accountIndex=0] - The account number to generate.
|
|
5
5
|
* @param [opts.validator=false] - Generate root key-pair,
|
|
6
6
|
* as used by validators.
|
|
7
|
-
* @returns {
|
|
8
|
-
*
|
|
7
|
+
* @returns {bigint} 256 bit scalar value.
|
|
9
8
|
*/
|
|
10
|
-
export declare function derivePrivateKey(seed:
|
|
9
|
+
export declare function derivePrivateKey(seed: Uint8Array, opts?: {
|
|
11
10
|
validator?: boolean;
|
|
12
11
|
accountIndex?: number;
|
|
13
|
-
}):
|
|
14
|
-
export declare function accountPublicFromPublicGenerator(publicGenBytes:
|
|
15
|
-
//# sourceMappingURL=
|
|
12
|
+
}): bigint;
|
|
13
|
+
export declare function accountPublicFromPublicGenerator(publicGenBytes: Uint8Array): Uint8Array;
|
|
14
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/signing-schemes/secp256k1/utils.ts"],"names":[],"mappings":"AAmCA;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,UAAU,EAChB,IAAI,GAAE;IACJ,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,YAAY,CAAC,EAAE,MAAM,CAAA;CACjB,GACL,MAAM,CAiBR;AAED,wBAAgB,gCAAgC,CAAC,cAAc,EAAE,UAAU,cAM1E"}
|
|
@@ -1,39 +1,16 @@
|
|
|
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
|
-
};
|
|
25
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
4
|
};
|
|
28
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
6
|
exports.accountPublicFromPublicGenerator = exports.derivePrivateKey = void 0;
|
|
30
|
-
const
|
|
31
|
-
const Sha512_1 = __importDefault(require("
|
|
32
|
-
const
|
|
7
|
+
const secp256k1_1 = require("@noble/curves/secp256k1");
|
|
8
|
+
const Sha512_1 = __importDefault(require("../../utils/Sha512"));
|
|
9
|
+
const ZERO = BigInt(0);
|
|
33
10
|
function deriveScalar(bytes, discrim) {
|
|
34
|
-
const order = secp256k1.
|
|
35
|
-
for (let i = 0; i <=
|
|
36
|
-
// We hash the bytes to find a 256
|
|
11
|
+
const order = secp256k1_1.secp256k1.CURVE.n;
|
|
12
|
+
for (let i = 0; i <= 4294967295; i++) {
|
|
13
|
+
// We hash the bytes to find a 256-bit number, looping until we are sure it
|
|
37
14
|
// is less than the order of the curve.
|
|
38
15
|
const hasher = new Sha512_1.default().add(bytes);
|
|
39
16
|
// If the optional discriminator index was passed in, update the hash.
|
|
@@ -41,9 +18,9 @@ function deriveScalar(bytes, discrim) {
|
|
|
41
18
|
hasher.addU32(discrim);
|
|
42
19
|
}
|
|
43
20
|
hasher.addU32(i);
|
|
44
|
-
const key = hasher.
|
|
21
|
+
const key = hasher.first256BigInt();
|
|
45
22
|
/* istanbul ignore else */
|
|
46
|
-
if (key
|
|
23
|
+
if (key > ZERO && key < order) {
|
|
47
24
|
return key;
|
|
48
25
|
}
|
|
49
26
|
}
|
|
@@ -53,7 +30,7 @@ function deriveScalar(bytes, discrim) {
|
|
|
53
30
|
// How often will an (essentially) random number generated by Sha512 be larger than that?
|
|
54
31
|
// There's 2^32 chances (the for loop) to get a number smaller than the order,
|
|
55
32
|
// and it's rare that you'll even get past the first loop iteration.
|
|
56
|
-
// Note that in TypeScript we actually need the throw, otherwise the function signature would be
|
|
33
|
+
// Note that in TypeScript we actually need the throw, otherwise the function signature would be bigint | undefined
|
|
57
34
|
//
|
|
58
35
|
/* istanbul ignore next */
|
|
59
36
|
throw new Error('impossible unicorn ;)');
|
|
@@ -64,12 +41,11 @@ function deriveScalar(bytes, discrim) {
|
|
|
64
41
|
* @param [opts.accountIndex=0] - The account number to generate.
|
|
65
42
|
* @param [opts.validator=false] - Generate root key-pair,
|
|
66
43
|
* as used by validators.
|
|
67
|
-
* @returns {
|
|
68
|
-
*
|
|
44
|
+
* @returns {bigint} 256 bit scalar value.
|
|
69
45
|
*/
|
|
70
46
|
function derivePrivateKey(seed, opts = {}) {
|
|
71
47
|
const root = opts.validator;
|
|
72
|
-
const order = secp256k1.
|
|
48
|
+
const order = secp256k1_1.secp256k1.CURVE.n;
|
|
73
49
|
// This private generator represents the `root` private key, and is what's
|
|
74
50
|
// used by validators for signing when a keypair is generated from a seed.
|
|
75
51
|
const privateGen = deriveScalar(seed);
|
|
@@ -77,21 +53,19 @@ function derivePrivateKey(seed, opts = {}) {
|
|
|
77
53
|
// As returned by validation_create for a given seed
|
|
78
54
|
return privateGen;
|
|
79
55
|
}
|
|
80
|
-
const publicGen = secp256k1.
|
|
56
|
+
const publicGen = secp256k1_1.secp256k1.ProjectivePoint.BASE.multiply(privateGen).toRawBytes(true);
|
|
81
57
|
// A seed can generate many keypairs as a function of the seed and a uint32.
|
|
82
58
|
// Almost everyone just uses the first account, `0`.
|
|
83
59
|
const accountIndex = opts.accountIndex || 0;
|
|
84
|
-
return deriveScalar(publicGen
|
|
85
|
-
.add(privateGen)
|
|
86
|
-
.mod(order);
|
|
60
|
+
return (deriveScalar(publicGen, accountIndex) + privateGen) % order;
|
|
87
61
|
}
|
|
88
62
|
exports.derivePrivateKey = derivePrivateKey;
|
|
89
63
|
function accountPublicFromPublicGenerator(publicGenBytes) {
|
|
90
|
-
const rootPubPoint = secp256k1.
|
|
64
|
+
const rootPubPoint = secp256k1_1.secp256k1.ProjectivePoint.fromHex(publicGenBytes);
|
|
91
65
|
const scalar = deriveScalar(publicGenBytes, 0);
|
|
92
|
-
const point = secp256k1.
|
|
66
|
+
const point = secp256k1_1.secp256k1.ProjectivePoint.BASE.multiply(scalar);
|
|
93
67
|
const offset = rootPubPoint.add(point);
|
|
94
|
-
return offset.
|
|
68
|
+
return offset.toRawBytes(true);
|
|
95
69
|
}
|
|
96
70
|
exports.accountPublicFromPublicGenerator = accountPublicFromPublicGenerator;
|
|
97
|
-
//# sourceMappingURL=
|
|
71
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/signing-schemes/secp256k1/utils.ts"],"names":[],"mappings":";;;;;;AAAA,uDAAmD;AAEnD,gEAAuC;AAEvC,MAAM,IAAI,GAAG,MAAM,CAAC,CAAC,CAAC,CAAA;AAEtB,SAAS,YAAY,CAAC,KAAiB,EAAE,OAAgB;IACvD,MAAM,KAAK,GAAG,qBAAS,CAAC,KAAK,CAAC,CAAC,CAAA;IAC/B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,UAAW,EAAE,CAAC,EAAE,EAAE;QACrC,2EAA2E;QAC3E,uCAAuC;QACvC,MAAM,MAAM,GAAG,IAAI,gBAAM,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;QACtC,sEAAsE;QACtE,IAAI,OAAO,KAAK,SAAS,EAAE;YACzB,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;SACvB;QACD,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;QAChB,MAAM,GAAG,GAAG,MAAM,CAAC,cAAc,EAAE,CAAA;QACnC,0BAA0B;QAC1B,IAAI,GAAG,GAAG,IAAI,IAAI,GAAG,GAAG,KAAK,EAAE;YAC7B,OAAO,GAAG,CAAA;SACX;KACF;IACD,iDAAiD;IACjD,8EAA8E;IAC9E,8EAA8E;IAC9E,yFAAyF;IACzF,8EAA8E;IAC9E,oEAAoE;IACpE,mHAAmH;IACnH,EAAE;IACF,0BAA0B;IAC1B,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAA;AAC1C,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,gBAAgB,CAC9B,IAAgB,EAChB,OAGI,EAAE;IAEN,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAA;IAC3B,MAAM,KAAK,GAAG,qBAAS,CAAC,KAAK,CAAC,CAAC,CAAA;IAE/B,0EAA0E;IAC1E,0EAA0E;IAC1E,MAAM,UAAU,GAAG,YAAY,CAAC,IAAI,CAAC,CAAA;IACrC,IAAI,IAAI,EAAE;QACR,oDAAoD;QACpD,OAAO,UAAU,CAAA;KAClB;IACD,MAAM,SAAS,GACb,qBAAS,CAAC,eAAe,CAAC,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;IACtE,4EAA4E;IAC5E,oDAAoD;IACpD,MAAM,YAAY,GAAG,IAAI,CAAC,YAAY,IAAI,CAAC,CAAA;IAC3C,OAAO,CAAC,YAAY,CAAC,SAAS,EAAE,YAAY,CAAC,GAAG,UAAU,CAAC,GAAG,KAAK,CAAA;AACrE,CAAC;AAvBD,4CAuBC;AAED,SAAgB,gCAAgC,CAAC,cAA0B;IACzE,MAAM,YAAY,GAAG,qBAAS,CAAC,eAAe,CAAC,OAAO,CAAC,cAAc,CAAC,CAAA;IACtE,MAAM,MAAM,GAAG,YAAY,CAAC,cAAc,EAAE,CAAC,CAAC,CAAA;IAC9C,MAAM,KAAK,GAAG,qBAAS,CAAC,eAAe,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAA;IAC7D,MAAM,MAAM,GAAG,YAAY,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;IACtC,OAAO,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;AAChC,CAAC;AAND,4EAMC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { decodeSeed } from '@transia/ripple-address-codec';
|
|
2
|
+
import type { Algorithm, HexString, KeyPair } from './types';
|
|
3
|
+
declare function generateSeed(options?: {
|
|
4
|
+
entropy?: Uint8Array;
|
|
5
|
+
algorithm?: Algorithm;
|
|
6
|
+
}): string;
|
|
7
|
+
declare function deriveKeypair(seed: string, options?: {
|
|
8
|
+
algorithm?: Algorithm;
|
|
9
|
+
validator?: boolean;
|
|
10
|
+
accountIndex?: number;
|
|
11
|
+
}): KeyPair;
|
|
12
|
+
declare function sign(messageHex: HexString, privateKey: HexString): HexString;
|
|
13
|
+
declare function verify(messageHex: HexString, signature: HexString, publicKey: HexString): boolean;
|
|
14
|
+
declare function deriveAddress(publicKey: string): string;
|
|
15
|
+
declare function deriveNodeAddress(publicKey: string): string;
|
|
16
|
+
export { generateSeed, deriveKeypair, sign, verify, deriveAddress, deriveNodeAddress, decodeSeed, };
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,UAAU,EAGX,MAAM,+BAA+B,CAAA;AAQtC,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAiB,MAAM,SAAS,CAAA;AAc3E,iBAAS,YAAY,CACnB,OAAO,GAAE;IACP,OAAO,CAAC,EAAE,UAAU,CAAA;IACpB,SAAS,CAAC,EAAE,SAAS,CAAA;CACjB,GACL,MAAM,CAUR;AAED,iBAAS,aAAa,CACpB,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE;IACR,SAAS,CAAC,EAAE,SAAS,CAAA;IACrB,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB,GACA,OAAO,CAcT;AAED,iBAAS,IAAI,CAAC,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,GAAG,SAAS,CAGrE;AAED,iBAAS,MAAM,CACb,UAAU,EAAE,SAAS,EACrB,SAAS,EAAE,SAAS,EACpB,SAAS,EAAE,SAAS,GACnB,OAAO,CAOT;AAUD,iBAAS,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED,iBAAS,iBAAiB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAIpD;AAED,OAAO,EACL,YAAY,EACZ,aAAa,EACb,IAAI,EACJ,MAAM,EACN,aAAa,EACb,iBAAiB,EACjB,UAAU,GACX,CAAA"}
|
|
@@ -0,0 +1,73 @@
|
|
|
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.decodeSeed = exports.deriveNodeAddress = exports.deriveAddress = exports.verify = exports.sign = exports.deriveKeypair = exports.generateSeed = void 0;
|
|
7
|
+
const ripple_address_codec_1 = require("@transia/ripple-address-codec");
|
|
8
|
+
Object.defineProperty(exports, "decodeSeed", { enumerable: true, get: function () { return ripple_address_codec_1.decodeSeed; } });
|
|
9
|
+
const ripemd160_1 = require("@transia/isomorphic/ripemd160");
|
|
10
|
+
const sha256_1 = require("@transia/isomorphic/sha256");
|
|
11
|
+
const utils_1 = require("@transia/isomorphic/utils");
|
|
12
|
+
const utils_2 = require("./signing-schemes/secp256k1/utils");
|
|
13
|
+
const Sha512_1 = __importDefault(require("./utils/Sha512"));
|
|
14
|
+
const assert_1 = __importDefault(require("./utils/assert"));
|
|
15
|
+
const getAlgorithmFromKey_1 = require("./utils/getAlgorithmFromKey");
|
|
16
|
+
const secp256k1_1 = __importDefault(require("./signing-schemes/secp256k1"));
|
|
17
|
+
const ed25519_1 = __importDefault(require("./signing-schemes/ed25519"));
|
|
18
|
+
function getSigningScheme(algorithm) {
|
|
19
|
+
const schemes = { 'ecdsa-secp256k1': secp256k1_1.default, ed25519: ed25519_1.default };
|
|
20
|
+
return schemes[algorithm];
|
|
21
|
+
}
|
|
22
|
+
function generateSeed(options = {}) {
|
|
23
|
+
assert_1.default.ok(!options.entropy || options.entropy.length >= 16, 'entropy too short');
|
|
24
|
+
const entropy = options.entropy
|
|
25
|
+
? options.entropy.slice(0, 16)
|
|
26
|
+
: (0, utils_1.randomBytes)(16);
|
|
27
|
+
const type = options.algorithm === 'ed25519' ? 'ed25519' : 'secp256k1';
|
|
28
|
+
return (0, ripple_address_codec_1.encodeSeed)(entropy, type);
|
|
29
|
+
}
|
|
30
|
+
exports.generateSeed = generateSeed;
|
|
31
|
+
function deriveKeypair(seed, options) {
|
|
32
|
+
var _a;
|
|
33
|
+
const decoded = (0, ripple_address_codec_1.decodeSeed)(seed);
|
|
34
|
+
const proposedAlgorithm = (_a = options === null || options === void 0 ? void 0 : options.algorithm) !== null && _a !== void 0 ? _a : decoded.type;
|
|
35
|
+
const algorithm = proposedAlgorithm === 'ed25519' ? 'ed25519' : 'ecdsa-secp256k1';
|
|
36
|
+
const scheme = getSigningScheme(algorithm);
|
|
37
|
+
const keypair = scheme.deriveKeypair(decoded.bytes, options);
|
|
38
|
+
const messageToVerify = Sha512_1.default.half('This test message should verify.');
|
|
39
|
+
const signature = scheme.sign(messageToVerify, keypair.privateKey);
|
|
40
|
+
/* istanbul ignore if */
|
|
41
|
+
if (!scheme.verify(messageToVerify, signature, keypair.publicKey)) {
|
|
42
|
+
throw new Error('derived keypair did not generate verifiable signature');
|
|
43
|
+
}
|
|
44
|
+
return keypair;
|
|
45
|
+
}
|
|
46
|
+
exports.deriveKeypair = deriveKeypair;
|
|
47
|
+
function sign(messageHex, privateKey) {
|
|
48
|
+
const algorithm = (0, getAlgorithmFromKey_1.getAlgorithmFromPrivateKey)(privateKey);
|
|
49
|
+
return getSigningScheme(algorithm).sign((0, utils_1.hexToBytes)(messageHex), privateKey);
|
|
50
|
+
}
|
|
51
|
+
exports.sign = sign;
|
|
52
|
+
function verify(messageHex, signature, publicKey) {
|
|
53
|
+
const algorithm = (0, getAlgorithmFromKey_1.getAlgorithmFromPublicKey)(publicKey);
|
|
54
|
+
return getSigningScheme(algorithm).verify((0, utils_1.hexToBytes)(messageHex), signature, publicKey);
|
|
55
|
+
}
|
|
56
|
+
exports.verify = verify;
|
|
57
|
+
function computePublicKeyHash(publicKeyBytes) {
|
|
58
|
+
return (0, ripemd160_1.ripemd160)((0, sha256_1.sha256)(publicKeyBytes));
|
|
59
|
+
}
|
|
60
|
+
function deriveAddressFromBytes(publicKeyBytes) {
|
|
61
|
+
return (0, ripple_address_codec_1.encodeAccountID)(computePublicKeyHash(publicKeyBytes));
|
|
62
|
+
}
|
|
63
|
+
function deriveAddress(publicKey) {
|
|
64
|
+
return deriveAddressFromBytes((0, utils_1.hexToBytes)(publicKey));
|
|
65
|
+
}
|
|
66
|
+
exports.deriveAddress = deriveAddress;
|
|
67
|
+
function deriveNodeAddress(publicKey) {
|
|
68
|
+
const generatorBytes = (0, ripple_address_codec_1.decodeNodePublic)(publicKey);
|
|
69
|
+
const accountPublicBytes = (0, utils_2.accountPublicFromPublicGenerator)(generatorBytes);
|
|
70
|
+
return deriveAddressFromBytes(accountPublicBytes);
|
|
71
|
+
}
|
|
72
|
+
exports.deriveNodeAddress = deriveNodeAddress;
|
|
73
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,wEAKsC;AAyGpC,2FA5GA,iCAAU,OA4GA;AAxGZ,6DAAyD;AACzD,uDAAmD;AACnD,qDAAmE;AAEnE,6DAAoF;AACpF,4DAAmC;AACnC,4DAAmC;AAEnC,qEAGoC;AAEpC,4EAAmD;AACnD,wEAA+C;AAE/C,SAAS,gBAAgB,CAAC,SAAoB;IAC5C,MAAM,OAAO,GAAG,EAAE,iBAAiB,EAAE,mBAAS,EAAE,OAAO,EAAP,iBAAO,EAAE,CAAA;IACzD,OAAO,OAAO,CAAC,SAAS,CAAC,CAAA;AAC3B,CAAC;AAED,SAAS,YAAY,CACnB,UAGI,EAAE;IAEN,gBAAM,CAAC,EAAE,CACP,CAAC,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,MAAM,IAAI,EAAE,EAChD,mBAAmB,CACpB,CAAA;IACD,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO;QAC7B,CAAC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