@koralabs/kora-labs-common 6.6.0 → 6.6.2
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 +2 -4
- package/utils/cip8/index.d.ts +15 -0
- package/utils/cip8/index.js +60 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@koralabs/kora-labs-common",
|
|
3
|
-
"version": "6.6.
|
|
3
|
+
"version": "6.6.2",
|
|
4
4
|
"description": "Kora Labs Common Utilities",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -47,9 +47,7 @@
|
|
|
47
47
|
"bs58": "^6.0.0",
|
|
48
48
|
"cbor": "^9.0.2",
|
|
49
49
|
"crc": "^4.3.2",
|
|
50
|
+
"libsodium-wrappers-sumo": "^0.8.3",
|
|
50
51
|
"pluralize-esm": "^9.0.5"
|
|
51
|
-
},
|
|
52
|
-
"overrides": {
|
|
53
|
-
"minimatch": "10.2.2"
|
|
54
52
|
}
|
|
55
53
|
}
|
package/utils/cip8/index.d.ts
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
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
|
+
}>;
|
|
1
16
|
/**
|
|
2
17
|
* Verify a CIP-30 signData (CIP-8) signature.
|
|
3
18
|
*
|
package/utils/cip8/index.js
CHANGED
|
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.verifyCip30SignData = void 0;
|
|
26
|
+
exports.verifyCip30SignData = exports.signCip30Data = void 0;
|
|
27
27
|
const crypto_1 = require("crypto");
|
|
28
28
|
let _cms;
|
|
29
29
|
const getCms = async () => {
|
|
@@ -32,6 +32,65 @@ const getCms = async () => {
|
|
|
32
32
|
}
|
|
33
33
|
return _cms;
|
|
34
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;
|
|
35
94
|
/**
|
|
36
95
|
* Verify a CIP-30 signData (CIP-8) signature.
|
|
37
96
|
*
|