/
utxo.ts
301 lines (249 loc) · 8.49 KB
/
utxo.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2022 @webb-tools/
// SPDX-License-Identifier: Apache-2.0
import type { Backend, Curve, JsUtxo } from '@webb-tools/wasm-utils';
import { hexToU8a } from '@polkadot/util';
import { toBuffer, toFixedHex } from './big-number-utils.js';
import { Keypair } from './keypair.js';
export type UtxoGenInput = {
curve: Curve,
backend: Backend,
amount: string,
chainId: string,
blinding?: Uint8Array
index?: string,
keypair?: Keypair,
originChainId?: string
};
/**
* Utxos are objects used to represent ownership of value within a VAnchor instance.
* The input Utxos to a VAnchor transaction represent the spending a previously created Utxo.
* - Therefore, input Utxos should have a privkey configured on the given Utxo's keypair.
* The output Utxos to a VAnchor transaction represent the creation of new Utxos.
* - Therefore, output Utxos don't need to have a privkey configured on the given Utxo's keypair.
*/
export class Utxo {
inner: JsUtxo;
keypair: Keypair = new Keypair();
originChainId: string | undefined;
/** Initialize a new UTXO - unspent transaction output or input. Note, a full TX consists of 2/16 inputs and 2 outputs
*
* @param inner - The wasm representation of a utxo
*/
constructor (inner: JsUtxo) {
this.inner = inner;
}
private static get wasm () {
if (typeof process !== 'undefined' && process.versions != null && process.versions.node != null) {
// If node is running in an esm context, return esm compliant package.
return import('@webb-tools/wasm-utils/njs/wasm-utils-njs.js');
} else {
return import('@webb-tools/wasm-utils/wasm-utils.js');
}
}
serialize (): string {
// The wasmUtxo is a string representation of the utxo. It has all of the information
// required of the parts - except for the Keypair information used for encryption.
const wasmUtxoString = this.inner.serialize();
const parts = wasmUtxoString.split('&');
const encryptionKey = this.keypair.getEncryptionKey()?.slice(2) ?? '';
const utxoString = [
parts[0],
parts[1],
parts[2],
parts[3],
parts[4],
parts[5],
encryptionKey,
parts[6],
parts[7]
].join('&');
return utxoString;
}
/**
* @param utxoString - A string representation of the parts that make up a utxo, split by '&'.
* - All values are represented as hex-encoded byte strings.
* - Relevant values will be interpreted as encoded in BigEndian
* - Optional values are represented as the empty string if not present,
* meaning the split call will always be an array of length "parts".
*
* parts[0] - Curve value, e.g. Bn254, Bls381, Ed25519, etc.
* parts[1] - Backend value, e.g. arkworks or circom
* parts[2] - Amount in atomic units, e.g. ETH in wei amounts or DOT in 10^12 decimals
* parts[3] - TypedChainId, the hex value of the calculated typed chain id
* parts[4] - Blinding, secret random value
* parts[5] - PublicKey, the "publicKey = hash(privateKey)" value which indicates ownership for a utxo.
* parts[6] Optional - EncryptionKey, the public key of "publicKey = encryptionScheme(privateKey)" value used for messaging.
* parts[7] Optional - PrivateKey, the secret key component correlated to the above values.
* parts[8] Optional - Index, the leaf index if the utxo has been inserted in a merkle tree
*
* @returns The Utxo object with appropriately configured inner wasm instance.
*/
static async deserialize (utxoString: string): Promise<Utxo> {
const parts = utxoString.split('&');
let reconstructedKeypair: Keypair;
// deserialize the keypair portion before reconstructing the string meant for wasm
if (parts[7].length === 64) {
// If a private key was provided, create the keypair object with it.
reconstructedKeypair = new Keypair('0x' + parts[7]);
} else {
if (parts[6].length === 64) {
// If an encryption key was provided, create the keypair object with it.
reconstructedKeypair = Keypair.fromString('0x' + parts[5] + parts[6]);
} else {
// If only a public key was provided, create the keypair object with it.
reconstructedKeypair = Keypair.fromString('0x' + parts[5]);
}
}
// Recreate the utxo string for wasm
const wasmUtxoString = [
parts[0],
parts[1],
parts[2],
parts[3],
parts[4],
parts[5],
parts[7],
parts[8]
].join('&');
const wasm = await Utxo.wasm;
const utxo = wasm.JsUtxo.deserialize(wasmUtxoString);
const retval = new Utxo(utxo);
retval.keypair = reconstructedKeypair;
return retval;
}
static async generateUtxo (input: UtxoGenInput): Promise<Utxo> {
const wasm = await Utxo.wasm;
let wasmUtxoPublicKey;
let wasmUtxoPrivateKey;
// Format the inputs as little-endian for wasm.
if (input.keypair) {
if (input.keypair.privkey) {
wasmUtxoPrivateKey = hexToU8a(input.keypair.privkey, 256);
}
wasmUtxoPublicKey = hexToU8a(input.keypair.getPubKey());
}
const wasmUtxo = new wasm.JsUtxo(
input.curve,
input.backend,
input.amount,
input.chainId,
input.blinding,
wasmUtxoPublicKey,
wasmUtxoPrivateKey,
input.index
);
const utxo = new Utxo(wasmUtxo);
if (input.keypair) {
utxo.setKeypair(input.keypair);
}
utxo.setOriginChainId(input.originChainId);
return utxo;
}
/**
* Encrypt UTXO data using the current keypair.
* This is used in the externalDataHash calculations so the funds for this deposit
* can only be spent by the owner of `this.keypair`.
*
* @returns `0x`-prefixed hex string with data
*/
encrypt () {
if (!this.keypair) {
throw new Error('Must set a keypair to encrypt the utxo');
}
const bytes = Buffer.concat([
toBuffer(`0x${this.chainId}`, 8),
toBuffer(`0x${this.amount}`, 31),
toBuffer(`0x${this.blinding}`, 31)
]);
return this.keypair.encrypt(bytes);
}
/**
* Decrypt a UTXO
*
* @param keypair - keypair used to decrypt
* @param data - hex string with data
* @returns a UTXO object
*/
static async decrypt (keypair: Keypair, data: string) {
const decryptedUtxoString = keypair.decrypt(data).toString();
if (decryptedUtxoString.length !== 70) {
throw new Error('Attempted to decrypt malformed data');
}
return Utxo.deserialize(decryptedUtxoString);
}
getKeypair () {
return this.keypair;
}
setKeypair (keypair: Keypair) {
this.keypair = keypair;
}
getOriginChainId () {
return this.originChainId;
}
setOriginChainId (originChainId: string | undefined) {
this.originChainId = originChainId;
}
setIndex (index: number) {
this.inner.setIndex(BigInt(index));
}
get amount (): string {
const value = BigInt('0x' + this.inner.amount);
return value.toString();
}
get blinding (): string {
return this.inner.blinding;
}
get chainId (): string {
const value = BigInt('0x' + this.inner.chainId);
return value.toString();
}
/**
* Returns commitment for this UTXO
*
* @returns the poseidon hash of [chainId, amount, pubKey, blinding]
*/
get commitment (): Uint8Array {
return this.inner.commitment;
}
/**
* @returns the index configured on this UTXO. Output UTXOs generated
* before they have been inserted in a tree.
*
* Utxos used as inputs are expected to have the index
*/
get index (): number|undefined {
if (this.inner.index !== undefined) {
return Number(this.inner.index);
}
return undefined;
}
/**
* @returns the nullifier: hash of [commitment, index, signature]
* where signature = hash([secret key, commitment, index])
*/
get nullifier (): string {
return this.inner.calculate_nullifier();
}
/**
* @returns the public key used for generating the commitment.
* If the utxo is configured with a secret_key, this value should be poseidonHash(secret_key)
*/
get public_key (): string {
return this.inner.public_key;
}
/**
* @returns the secret_key AKA private_key used in the nullifier.
*/
get secret_key (): string {
return this.inner.secret_key;
}
/**
* @returns secrets - an array of secret values represented in the utxo
*/
getSecretsForNote (): string[] {
if (!this.keypair.privkey) {
throw new Error('Missing private key for secrets');
}
return [toFixedHex(this.chainId, 8).slice(2), toFixedHex(this.amount, 32).slice(2), this.secret_key, this.blinding];
}
}