|
| 1 | +import { Buffer } from 'buffer'; |
| 2 | +import * as CSL from '@emurgo/cardano-serialization-lib-nodejs'; |
| 3 | +import { Decoder, Encoder } from 'cbor'; |
| 4 | + |
| 5 | +// Helper function to convert a Uint8Array or Buffer to a hex string |
| 6 | +export function bytesToHex(bytes: Uint8Array | Buffer): string { |
| 7 | + return Buffer.from(bytes).toString('hex'); |
| 8 | +} |
| 9 | + |
| 10 | +export interface CSLSigStructureOutput { |
| 11 | + sigStructureCborBytes: Uint8Array; |
| 12 | + protectedHeaderCborBytes: Uint8Array; |
| 13 | + payloadBytes: Buffer; |
| 14 | +} |
| 15 | + |
| 16 | +export interface CSLCoseObjectsOutput { |
| 17 | + manualCoseSign1Hex: string; |
| 18 | + manualCoseKeyHex: string; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Creates the CSL signature structure for off-chain message signing. |
| 23 | + * |
| 24 | + * @param addressCborBytes - The CBOR bytes of the CSL address. |
| 25 | + * @param message - The message to be signed. |
| 26 | + * @returns An object containing the signature structure CBOR bytes, protected header CBOR bytes, and payload bytes. |
| 27 | + */ |
| 28 | +export function createCSLSigStructure(addressCborBytes: Uint8Array, message: string): CSLSigStructureOutput { |
| 29 | + // Payload |
| 30 | + const payloadBytes = Buffer.from(message, 'utf-8'); |
| 31 | + |
| 32 | + // Protected Header |
| 33 | + const protectedHeaderMap = new Map<number | string, any>(); |
| 34 | + protectedHeaderMap.set(1, -8); // Algorithm ID: EdDSA |
| 35 | + protectedHeaderMap.set('address', Buffer.from(addressCborBytes)); |
| 36 | + const protectedHeaderCborBytes = Encoder.encode(protectedHeaderMap); |
| 37 | + |
| 38 | + // Sig_structure |
| 39 | + const sigStructureArray: any[] = [ |
| 40 | + 'Signature1', |
| 41 | + Buffer.from(protectedHeaderCborBytes), |
| 42 | + Buffer.from([]), // Empty external_aad |
| 43 | + Buffer.from(payloadBytes), |
| 44 | + ]; |
| 45 | + const sigStructureCborBytes = Encoder.encode(sigStructureArray); |
| 46 | + |
| 47 | + return { sigStructureCborBytes, protectedHeaderCborBytes, payloadBytes }; |
| 48 | +} |
| 49 | + |
| 50 | +// COSE objects construction function |
| 51 | +export function constructCSLCoseObjects( |
| 52 | + protectedHeaderCborBytes: Uint8Array, |
| 53 | + payloadBytes: Buffer, |
| 54 | + cslSignatureBytes: Uint8Array, |
| 55 | + paymentPubKey: CSL.PublicKey |
| 56 | +): CSLCoseObjectsOutput { |
| 57 | + // COSE_Sign1 Construction |
| 58 | + const unprotectedHeadersMap = new Map<string, any>(); |
| 59 | + unprotectedHeadersMap.set('hashed', false); |
| 60 | + const coseSign1Array: any[] = [ |
| 61 | + Buffer.from(protectedHeaderCborBytes), |
| 62 | + unprotectedHeadersMap, |
| 63 | + Buffer.from(payloadBytes), |
| 64 | + Buffer.from(cslSignatureBytes), |
| 65 | + ]; |
| 66 | + const finalCoseSign1CborBytes = Encoder.encode(coseSign1Array); |
| 67 | + /* // directly encoding the coseSign1Array without prepending the 0xD2 tag. |
| 68 | + * const coseSign1PayloadBytes = Encoder.encode(coseSign1Array); |
| 69 | + * const coseSign1Tag = Buffer.from([0xD2]); // Tag 18 for COSE_Sign1 |
| 70 | + * const finalCoseSign1CborBytes = Buffer.concat([coseSign1Tag, coseSign1PayloadBytes]); |
| 71 | + */ |
| 72 | + const manualCoseSign1Hex = bytesToHex(finalCoseSign1CborBytes); |
| 73 | + |
| 74 | + // COSE_Key Construction |
| 75 | + const coseKeyMap = new Map<number, any>(); |
| 76 | + coseKeyMap.set(1, 1); // kty: OKP (Octet Key Pair) |
| 77 | + coseKeyMap.set(3, -8); // alg: EdDSA |
| 78 | + coseKeyMap.set(-1, 6); // crv: Ed25519 |
| 79 | + coseKeyMap.set(-2, Buffer.from(paymentPubKey.as_bytes())); // x: public_key_bytes (Ed25519 public key) |
| 80 | + const finalCoseKeyCborBytes = Encoder.encode(coseKeyMap); |
| 81 | + const manualCoseKeyHex = bytesToHex(finalCoseKeyCborBytes); |
| 82 | + |
| 83 | + return { manualCoseSign1Hex, manualCoseKeyHex }; |
| 84 | +} |
| 85 | + |
| 86 | +export function coseObjectsOutputToBuffer(output: CSLCoseObjectsOutput): Buffer { |
| 87 | + return Buffer.from(Encoder.encode(output)); |
| 88 | +} |
| 89 | + |
| 90 | +export async function bufferToCoseObjectsOutput(buffer: Buffer): Promise<CSLCoseObjectsOutput> { |
| 91 | + return await Decoder.decodeFirst(buffer); |
| 92 | +} |
0 commit comments