@koralabs/kora-labs-common 6.4.25 → 6.6.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/package.json +3 -1
- package/utils/cip8/index.d.ts +29 -0
- package/utils/cip8/index.js +154 -0
- package/utils/crypto/index.d.ts +1 -0
- package/utils/crypto/index.js +16 -1
- package/utils/index.d.ts +1 -0
- package/utils/index.js +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@koralabs/kora-labs-common",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.6.1",
|
|
4
4
|
"description": "Kora Labs Common Utilities",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -40,12 +40,14 @@
|
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@aws-sdk/client-kms": "^3.1003.0",
|
|
43
|
+
"@emurgo/cardano-message-signing-nodejs": "^1.0.1",
|
|
43
44
|
"bech32": "^2.0.0",
|
|
44
45
|
"blakejs": "^1.2.1",
|
|
45
46
|
"boolean": "^3.2.0",
|
|
46
47
|
"bs58": "^6.0.0",
|
|
47
48
|
"cbor": "^9.0.2",
|
|
48
49
|
"crc": "^4.3.2",
|
|
50
|
+
"libsodium-wrappers-sumo": "^0.8.3",
|
|
49
51
|
"pluralize-esm": "^9.0.5"
|
|
50
52
|
},
|
|
51
53
|
"overrides": {
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Produce a CIP-30 signData (CIP-8) signature.
|
|
3
|
+
*
|
|
4
|
+
* Builds COSE_Sign1 and COSE_Key structures that verifyCip30SignData can verify.
|
|
5
|
+
*
|
|
6
|
+
* @param privateKeyHex - raw Ed25519 private key (32 bytes hex)
|
|
7
|
+
* @param publicKeyHex - raw Ed25519 public key (32 bytes hex)
|
|
8
|
+
* @param payloadHex - hex of the payload to sign (e.g. UTF-8 bytes of requestId + handle)
|
|
9
|
+
* @param addressHex - hex of the signing address (included in COSE protected headers)
|
|
10
|
+
* @returns { signature: string, key: string } — COSESign1 and COSEKey as CBOR hex
|
|
11
|
+
*/
|
|
12
|
+
export declare const signCip30Data: (privateKeyHex: string, publicKeyHex: string, payloadHex: string, addressHex: string, signFn?: ((messageHex: string) => string) | undefined) => Promise<{
|
|
13
|
+
signature: string;
|
|
14
|
+
key: string;
|
|
15
|
+
}>;
|
|
16
|
+
/**
|
|
17
|
+
* Verify a CIP-30 signData (CIP-8) signature.
|
|
18
|
+
*
|
|
19
|
+
* Parses the COSE structures produced by the wallet, reconstructs the
|
|
20
|
+
* SigStructure using the library (not manual CBOR), and verifies the
|
|
21
|
+
* Ed25519 signature with Node.js crypto.
|
|
22
|
+
*
|
|
23
|
+
* @param signatureCborHex - COSESign1 CBOR hex from wallet signData response
|
|
24
|
+
* @param publicKeyCborHex - COSEKey CBOR hex from wallet signData response
|
|
25
|
+
* @param expectedPayloadHex - hex of the expected signed payload (e.g. requestId + handle as UTF-8 bytes)
|
|
26
|
+
* @param expectedAddressHex - hex of the expected signing address (must match the address in the COSE protected headers)
|
|
27
|
+
* @returns true if the signature is valid
|
|
28
|
+
*/
|
|
29
|
+
export declare const verifyCip30SignData: (signatureCborHex: string, publicKeyCborHex: string, expectedPayloadHex: string, expectedAddressHex: string) => Promise<boolean>;
|
|
@@ -0,0 +1,154 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.verifyCip30SignData = exports.signCip30Data = void 0;
|
|
27
|
+
const crypto_1 = require("crypto");
|
|
28
|
+
let _cms;
|
|
29
|
+
const getCms = async () => {
|
|
30
|
+
if (!_cms) {
|
|
31
|
+
_cms = await Promise.resolve().then(() => __importStar(require('@emurgo/cardano-message-signing-nodejs')));
|
|
32
|
+
}
|
|
33
|
+
return _cms;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Produce a CIP-30 signData (CIP-8) signature.
|
|
37
|
+
*
|
|
38
|
+
* Builds COSE_Sign1 and COSE_Key structures that verifyCip30SignData can verify.
|
|
39
|
+
*
|
|
40
|
+
* @param privateKeyHex - raw Ed25519 private key (32 bytes hex)
|
|
41
|
+
* @param publicKeyHex - raw Ed25519 public key (32 bytes hex)
|
|
42
|
+
* @param payloadHex - hex of the payload to sign (e.g. UTF-8 bytes of requestId + handle)
|
|
43
|
+
* @param addressHex - hex of the signing address (included in COSE protected headers)
|
|
44
|
+
* @returns { signature: string, key: string } — COSESign1 and COSEKey as CBOR hex
|
|
45
|
+
*/
|
|
46
|
+
const signCip30Data = async (privateKeyHex, publicKeyHex, payloadHex, addressHex,
|
|
47
|
+
/** Optional sign function for extended keys. Takes message hex, returns signature hex. */
|
|
48
|
+
signFn) => {
|
|
49
|
+
const cms = await getCms();
|
|
50
|
+
const payloadBytes = Buffer.from(payloadHex, 'hex');
|
|
51
|
+
const addrBytes = Buffer.from(addressHex, 'hex');
|
|
52
|
+
const pubKeyBytes = Buffer.from(publicKeyHex, 'hex');
|
|
53
|
+
// Build protected headers: algorithm = EdDSA, address = signing address
|
|
54
|
+
const protectedHeaders = cms.HeaderMap.new();
|
|
55
|
+
protectedHeaders.set_algorithm_id(cms.Label.from_algorithm_id(cms.AlgorithmId.EdDSA));
|
|
56
|
+
protectedHeaders.set_header(cms.Label.new_text('address'), cms.CBORValue.new_bytes(addrBytes));
|
|
57
|
+
const headers = cms.Headers.new(cms.ProtectedHeaderMap.new(protectedHeaders), cms.HeaderMap.new());
|
|
58
|
+
const builder = cms.COSESign1Builder.new(headers, payloadBytes, false);
|
|
59
|
+
// Sign the SigStructure with the Ed25519 private key.
|
|
60
|
+
// Accepts 32-byte seed (Node.js crypto) or 64-byte extended key (Cardano BIP32).
|
|
61
|
+
// For extended keys, a signFn callback must be provided since the standard
|
|
62
|
+
// DER format doesn't support pre-clamped scalars.
|
|
63
|
+
const sigStructureBytes = builder.make_data_to_sign().to_bytes();
|
|
64
|
+
const privKeyRaw = Buffer.from(privateKeyHex, 'hex');
|
|
65
|
+
let signatureBytes;
|
|
66
|
+
if (privKeyRaw.length <= 32) {
|
|
67
|
+
// 32-byte seed: use Node.js crypto
|
|
68
|
+
const privKeyDer = Buffer.concat([
|
|
69
|
+
Buffer.from('302e020100300506032b657004220420', 'hex'),
|
|
70
|
+
privKeyRaw
|
|
71
|
+
]);
|
|
72
|
+
const keyObject = (0, crypto_1.createPrivateKey)({ key: privKeyDer, format: 'der', type: 'pkcs8' });
|
|
73
|
+
signatureBytes = (0, crypto_1.sign)(null, Buffer.from(sigStructureBytes), keyObject);
|
|
74
|
+
}
|
|
75
|
+
else if (signFn) {
|
|
76
|
+
// Extended key with custom sign function (e.g. @cardano-sdk/crypto Ed25519PrivateKey.sign)
|
|
77
|
+
signatureBytes = Buffer.from(signFn(Buffer.from(sigStructureBytes).toString('hex')), 'hex');
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
throw new Error('64-byte extended key requires a signFn parameter');
|
|
81
|
+
}
|
|
82
|
+
const coseSign1 = builder.build(signatureBytes);
|
|
83
|
+
// Build COSE_Key with the public key
|
|
84
|
+
const coseKey = cms.COSEKey.new(cms.Label.from_key_type(cms.KeyType.OKP));
|
|
85
|
+
coseKey.set_algorithm_id(cms.Label.from_algorithm_id(cms.AlgorithmId.EdDSA));
|
|
86
|
+
coseKey.set_header(cms.Label.new_int(cms.Int.new_i32(-1)), cms.CBORValue.from_label(cms.Label.from_curve_type(cms.CurveType.Ed25519)));
|
|
87
|
+
coseKey.set_header(cms.Label.new_int(cms.Int.new_i32(-2)), cms.CBORValue.new_bytes(pubKeyBytes));
|
|
88
|
+
return {
|
|
89
|
+
signature: Buffer.from(coseSign1.to_bytes()).toString('hex'),
|
|
90
|
+
key: Buffer.from(coseKey.to_bytes()).toString('hex')
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
exports.signCip30Data = signCip30Data;
|
|
94
|
+
/**
|
|
95
|
+
* Verify a CIP-30 signData (CIP-8) signature.
|
|
96
|
+
*
|
|
97
|
+
* Parses the COSE structures produced by the wallet, reconstructs the
|
|
98
|
+
* SigStructure using the library (not manual CBOR), and verifies the
|
|
99
|
+
* Ed25519 signature with Node.js crypto.
|
|
100
|
+
*
|
|
101
|
+
* @param signatureCborHex - COSESign1 CBOR hex from wallet signData response
|
|
102
|
+
* @param publicKeyCborHex - COSEKey CBOR hex from wallet signData response
|
|
103
|
+
* @param expectedPayloadHex - hex of the expected signed payload (e.g. requestId + handle as UTF-8 bytes)
|
|
104
|
+
* @param expectedAddressHex - hex of the expected signing address (must match the address in the COSE protected headers)
|
|
105
|
+
* @returns true if the signature is valid
|
|
106
|
+
*/
|
|
107
|
+
const verifyCip30SignData = async (signatureCborHex, publicKeyCborHex, expectedPayloadHex, expectedAddressHex) => {
|
|
108
|
+
var _a;
|
|
109
|
+
const cms = await getCms();
|
|
110
|
+
// Parse COSESign1 and COSEKey from wallet output
|
|
111
|
+
const coseSign1 = cms.COSESign1.from_bytes(Buffer.from(signatureCborHex, 'hex'));
|
|
112
|
+
const coseKey = cms.COSEKey.from_bytes(Buffer.from(publicKeyCborHex, 'hex'));
|
|
113
|
+
// Extract the address from the protected headers and verify it matches
|
|
114
|
+
const protectedHeaders = coseSign1.headers().protected().deserialized_headers();
|
|
115
|
+
const addressLabel = cms.Label.new_text('address');
|
|
116
|
+
const addressValue = protectedHeaders.header(addressLabel);
|
|
117
|
+
if (addressValue) {
|
|
118
|
+
const addressBytes = Buffer.from((_a = addressValue.as_bytes()) !== null && _a !== void 0 ? _a : new Uint8Array()).toString('hex');
|
|
119
|
+
if (addressBytes !== expectedAddressHex) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// Verify the payload matches what we expect
|
|
124
|
+
const signedPayload = coseSign1.payload();
|
|
125
|
+
if (signedPayload) {
|
|
126
|
+
const payloadHex = Buffer.from(signedPayload).toString('hex');
|
|
127
|
+
if (payloadHex !== expectedPayloadHex) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// Reconstruct the SigStructure (what was actually signed) using the library
|
|
132
|
+
const sigStructure = coseSign1.signed_data();
|
|
133
|
+
const sigStructureBytes = sigStructure.to_bytes();
|
|
134
|
+
// Extract the raw Ed25519 signature
|
|
135
|
+
const signatureBytes = coseSign1.signature();
|
|
136
|
+
// Extract the Ed25519 public key from COSEKey (label -2 is the "x" coordinate / public key)
|
|
137
|
+
const xLabel = cms.Label.new_int(cms.Int.new_i32(-2));
|
|
138
|
+
const pubKeyValue = coseKey.header(xLabel);
|
|
139
|
+
if (!pubKeyValue) {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
const pubKeyBytes = pubKeyValue.as_bytes();
|
|
143
|
+
if (!pubKeyBytes || pubKeyBytes.length !== 32) {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
// Wrap the raw Ed25519 public key in DER SPKI format for Node.js crypto
|
|
147
|
+
const pubKeyDer = Buffer.concat([
|
|
148
|
+
Buffer.from('302a300506032b6570032100', 'hex'),
|
|
149
|
+
Buffer.from(pubKeyBytes)
|
|
150
|
+
]);
|
|
151
|
+
const keyObject = (0, crypto_1.createPublicKey)({ key: pubKeyDer, format: 'der', type: 'spki' });
|
|
152
|
+
return (0, crypto_1.verify)(null, Buffer.from(sigStructureBytes), keyObject, Buffer.from(signatureBytes));
|
|
153
|
+
};
|
|
154
|
+
exports.verifyCip30SignData = verifyCip30SignData;
|
package/utils/crypto/index.d.ts
CHANGED
|
@@ -23,4 +23,5 @@ export declare const bech32AddressFromHashes: (paymentHash: string, paymentHashT
|
|
|
23
23
|
export declare const buildHolderInfo: (addr: string) => AddressDetails;
|
|
24
24
|
export declare const getDateStringFromSlot: (currentSlot: number) => Date;
|
|
25
25
|
export declare const getSlotNumberFromDate: (date: Date) => number;
|
|
26
|
+
export declare const addressHexToBech32: (hex: string) => string;
|
|
26
27
|
export declare const blake2b: (input: string | Buffer | Uint8Array, outlen?: number) => string;
|
package/utils/crypto/index.js
CHANGED
|
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.blake2b = exports.getSlotNumberFromDate = exports.getDateStringFromSlot = exports.buildHolderInfo = exports.bech32AddressFromHashes = exports.bech32FromHex = exports.getPaymentKeyHash = exports.buildStakeKey = exports.buildPaymentAddressType = exports.getPaymentAddressType = exports.checkNameLabel = exports.parseAssetNameLabel = exports.decodeAddress = exports.getChallengeFromVerifier = exports.base64urlencode = exports.sha256 = exports.getRandomCodeVerifier = exports.dec2hex = exports.getNativeCrypto = void 0;
|
|
29
|
+
exports.blake2b = exports.addressHexToBech32 = exports.getSlotNumberFromDate = exports.getDateStringFromSlot = exports.buildHolderInfo = exports.bech32AddressFromHashes = exports.bech32FromHex = exports.getPaymentKeyHash = exports.buildStakeKey = exports.buildPaymentAddressType = exports.getPaymentAddressType = exports.checkNameLabel = exports.parseAssetNameLabel = exports.decodeAddress = exports.getChallengeFromVerifier = exports.base64urlencode = exports.sha256 = exports.getRandomCodeVerifier = exports.dec2hex = exports.getNativeCrypto = void 0;
|
|
30
30
|
const bech32_1 = require("bech32");
|
|
31
31
|
const blakejs_1 = require("blakejs");
|
|
32
32
|
const bs58_1 = __importDefault(require("bs58"));
|
|
@@ -271,6 +271,21 @@ const getSlotNumberFromDate = (date) => {
|
|
|
271
271
|
return (Math.floor(date.getTime() / 1000) - 1596491091) + 4924800;
|
|
272
272
|
};
|
|
273
273
|
exports.getSlotNumberFromDate = getSlotNumberFromDate;
|
|
274
|
+
const addressHexToBech32 = (hex) => {
|
|
275
|
+
const bytes = Uint8Array.from(Buffer.from(hex, 'hex'));
|
|
276
|
+
if (bytes.length === 0)
|
|
277
|
+
return '';
|
|
278
|
+
const header = bytes[0];
|
|
279
|
+
const addressType = header >> 4;
|
|
280
|
+
const networkId = header & 0x0f;
|
|
281
|
+
const isTestnet = networkId === 0;
|
|
282
|
+
const isReward = addressType === 14 || addressType === 15;
|
|
283
|
+
const type = isReward ? 'stake' : 'addr';
|
|
284
|
+
const prefix = isTestnet && (type === 'addr' || type === 'stake') ? `${type}_test` : type;
|
|
285
|
+
const words = bech32_1.bech32.toWords(bytes);
|
|
286
|
+
return bech32_1.bech32.encode(prefix, words, 2048);
|
|
287
|
+
};
|
|
288
|
+
exports.addressHexToBech32 = addressHexToBech32;
|
|
274
289
|
const blake2b = (input, outlen = 32) => {
|
|
275
290
|
if (typeof input == 'string') {
|
|
276
291
|
input = Buffer.from(input, 'hex');
|
package/utils/index.d.ts
CHANGED
|
@@ -30,4 +30,5 @@ export declare const isUserIssueTrackingId: (value: unknown) => value is string;
|
|
|
30
30
|
export declare const normalizeUserIssueEventSegment: (value: string) => string;
|
|
31
31
|
export declare const buildUserIssueEventKey: (repo: string, flow: string, pathOrFunction: string, step: string) => string;
|
|
32
32
|
export { decodeCborToJson, encodeJsonToDatum, DefaultTextFormat as KeyType } from './cbor';
|
|
33
|
+
export * from './cip8';
|
|
33
34
|
export * from './crypto';
|
package/utils/index.js
CHANGED
|
@@ -199,4 +199,5 @@ var cbor_1 = require("./cbor");
|
|
|
199
199
|
Object.defineProperty(exports, "decodeCborToJson", { enumerable: true, get: function () { return cbor_1.decodeCborToJson; } });
|
|
200
200
|
Object.defineProperty(exports, "encodeJsonToDatum", { enumerable: true, get: function () { return cbor_1.encodeJsonToDatum; } });
|
|
201
201
|
Object.defineProperty(exports, "KeyType", { enumerable: true, get: function () { return cbor_1.DefaultTextFormat; } });
|
|
202
|
+
__exportStar(require("./cip8"), exports);
|
|
202
203
|
__exportStar(require("./crypto"), exports);
|