-
Notifications
You must be signed in to change notification settings - Fork 55
/
VkeyWitness.ts
133 lines (113 loc) · 4.1 KB
/
VkeyWitness.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import * as Crypto from '@cardano-sdk/crypto';
import { CborReader, CborWriter } from '../CBOR';
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
import { hexToBytes } from '../../util/misc';
const VKEY_ARRAY_SIZE = 2;
/**
* VkeyWitness (Verification Key Witness) is a component of a transaction that
* provides cryptographic proof that proves that the creator of the transaction
* has access to the private keys controlling the UTxOs being spent.
*/
export class VkeyWitness {
#vkey: Crypto.Ed25519PublicKeyHex;
#signature: Crypto.Ed25519SignatureHex;
#originalBytes: HexBlob | undefined = undefined;
/**
* Initializes a new instance of the VkeyWitness class.
*
* @param vkey This is the public counterpart to the private key (SKey). It's used to verify the signature.
* @param signature This is a cryptographic signature produced by signing the hash of the transaction body with
* the corresponding private key (SKey). Anyone can verify this signature using the provided VKey,
* ensuring that the transaction was authorized by the holder of the private key.
*/
constructor(vkey: Crypto.Ed25519PublicKeyHex, signature: Crypto.Ed25519SignatureHex) {
this.#vkey = vkey;
this.#signature = signature;
}
/**
* Serializes a VkeyWitness into CBOR format.
*
* @returns The VkeyWitness in CBOR format.
*/
toCbor(): HexBlob {
const writer = new CborWriter();
if (this.#originalBytes) return this.#originalBytes;
// CDDL
// vkeywitness = [ $vkey, $signature ]
writer.writeStartArray(VKEY_ARRAY_SIZE);
writer.writeByteString(hexToBytes(this.#vkey as unknown as HexBlob));
writer.writeByteString(hexToBytes(this.#signature as unknown as HexBlob));
return writer.encodeAsHex();
}
/**
* Deserializes the VkeyWitness from a CBOR byte array.
*
* @param cbor The CBOR encoded VkeyWitness object.
* @returns The new VkeyWitness instance.
*/
static fromCbor(cbor: HexBlob): VkeyWitness {
const reader = new CborReader(cbor);
const length = reader.readStartArray();
if (length !== VKEY_ARRAY_SIZE)
throw new InvalidArgumentError(
'cbor',
`Expected an array of ${VKEY_ARRAY_SIZE} elements, but got an array of ${length} elements`
);
const vkey = HexBlob.fromBytes(reader.readByteString()) as unknown as Crypto.Ed25519PublicKeyHex;
const signature = HexBlob.fromBytes(reader.readByteString()) as unknown as Crypto.Ed25519SignatureHex;
reader.readEndArray();
const witness = new VkeyWitness(vkey, signature);
witness.#originalBytes = cbor;
return witness;
}
/**
* Creates a tuple with the vkey and the signature from the current VkeyWitness object.
*
* @returns The tuple with the vkey and the signature.
*/
toCore(): [Crypto.Ed25519PublicKeyHex, Crypto.Ed25519SignatureHex] {
return [this.#vkey, this.#signature];
}
/**
* Creates a VkeyWitness object from a tuple with the vkey and the signature.
*
* @param signatureEntry A tuple with the vkey and the signature.
*/
static fromCore(signatureEntry: [Crypto.Ed25519PublicKeyHex, Crypto.Ed25519SignatureHex]) {
return new VkeyWitness(signatureEntry[0], signatureEntry[1]);
}
/**
* Gets the public key associated with the provided signature.
*
* @returns The public key.
*/
vkey(): Crypto.Ed25519PublicKeyHex {
return this.#vkey;
}
/**
* Sets the public key associated with the provided signature.
*
* @param vkey The public key.
*/
setVkey(vkey: Crypto.Ed25519PublicKeyHex) {
this.#vkey = vkey;
this.#originalBytes = undefined;
}
/**
* Gets transaction body signature computed with the private counterpart to the public key (VKey).
*
* @returns The Ed25519 signature.
*/
signature(): Crypto.Ed25519SignatureHex {
return this.#signature;
}
/**
* Sets the transaction body signature computed with the private counterpart to the public key (VKey).
*
* @param signature The Ed25519 signature.
*/
setSignature(signature: Crypto.Ed25519SignatureHex) {
this.#signature = signature;
this.#originalBytes = undefined;
}
}