Skip to content

Commit a5eb218

Browse files
committed
feat: extract deserialize and serialize functions for dkg commands
1 parent 93d9552 commit a5eb218

File tree

3 files changed

+182
-132
lines changed

3 files changed

+182
-132
lines changed

src/deserialize.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export const deserializeDkgRound1 = (data: Buffer) => {
2+
let pos = 0
3+
const secretPackageLen = data.readUint16BE(pos)
4+
pos += 2
5+
const secretPackage = data.subarray(pos, pos + secretPackageLen)
6+
pos += secretPackageLen
7+
const publicPackageLen = data.readUint16BE(pos)
8+
pos += 2
9+
const publicPackage = data.subarray(pos, pos + publicPackageLen)
10+
pos += publicPackageLen
11+
12+
return {
13+
secretPackage,
14+
publicPackage
15+
}
16+
}
17+
18+
export const deserializeDkgRound2 = (data: Buffer) => {
19+
let pos = 0
20+
const secretPackageLen = data.readUint16BE(pos)
21+
pos += 2
22+
const secretPackage = data.subarray(pos, pos + secretPackageLen)
23+
pos += secretPackageLen
24+
const publicPackageLen = data.readUint16BE(pos)
25+
pos += 2
26+
const publicPackage = data.subarray(pos, pos + publicPackageLen)
27+
pos += publicPackageLen
28+
29+
return {
30+
secretPackage,
31+
publicPackage
32+
}
33+
}

src/index.ts

Lines changed: 32 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,19 @@ import {
3737
ResponseIdentity,
3838
ResponseSign
3939
} from './types'
40-
import {encodeRound3Inputs, minimizeRound3Inputs} from "./ironfish";
40+
import {
41+
serializeDkgGetCommitments,
42+
serializeDkgGetNonces,
43+
serializeDkgRound1,
44+
serializeDkgRound2,
45+
serializeDkgRound3, serializeDkgSign,
46+
} from './serialize'
47+
import { deserializeDkgRound1, deserializeDkgRound2 } from './deserialize'
4148

4249
export * from './types'
4350

51+
const DUMMY_PATH = "m/44'/1338'/0";
52+
4453
export default class IronfishApp extends GenericApp {
4554
readonly CLA_DKG: number = 0x63;
4655
readonly INS!: IronfishIns
@@ -181,20 +190,9 @@ export default class IronfishApp extends GenericApp {
181190
])
182191
}
183192

184-
async dkgRound1(path: string, index:number, identities: string[], minSigners: number): Promise<ResponseDkgRound1> {
185-
let blob = Buffer
186-
.alloc(1 + 1 + identities.length * 129 + 1);
187-
console.log(`dkgRound1 msg size: ${blob.byteLength}`)
188-
189-
190-
blob.writeUint8(index);
191-
blob.writeUint8(identities.length, 1);
192-
for (let i = 0; i < identities.length; i++) {
193-
blob.fill(Buffer.from(identities[i], "hex"), 1 + 1 + (i * 129))
194-
}
195-
blob.writeUint8(minSigners, 1 + 1 + identities.length * 129);
196-
197-
const chunks = this.prepareChunks(path, blob)
193+
async dkgRound1(index:number, identities: string[], minSigners: number): Promise<ResponseDkgRound1> {
194+
const blob = serializeDkgRound1(index, identities, minSigners);
195+
const chunks = this.prepareChunks(DUMMY_PATH, blob)
198196

199197
try{
200198
let response = Buffer.alloc(0)
@@ -251,22 +249,10 @@ export default class IronfishApp extends GenericApp {
251249
}
252250

253251
} else {
254-
// console.log("raw round1 " + data.toString("hex"))
255-
let pos = 0
256-
const secretPackageLen = data.readUint16BE(pos)
257-
pos += 2
258-
const secretPackage = data.subarray(pos, pos + secretPackageLen)
259-
pos += secretPackageLen
260-
const publicPackageLen = data.readUint16BE(pos)
261-
pos += 2
262-
const publicPackage = data.subarray(pos, pos + publicPackageLen)
263-
pos += publicPackageLen
264-
265252
return {
266253
returnCode,
267254
errorMessage,
268-
secretPackage,
269-
publicPackage
255+
...deserializeDkgRound1(data)
270256
}
271257
}
272258
}
@@ -277,35 +263,9 @@ export default class IronfishApp extends GenericApp {
277263
}
278264

279265

280-
async dkgRound2(path: string, index: number, round1PublicPackages: string[], round1SecretPackage: string): Promise<ResponseDkgRound2> {
281-
let round1PublicPackagesLen = round1PublicPackages[0].length / 2
282-
let round1SecretPackageLen = round1SecretPackage.length / 2
283-
284-
let blob = Buffer
285-
.alloc(1 + 1 + 2 + round1PublicPackages.length * round1PublicPackagesLen + 2 + round1SecretPackageLen);
286-
console.log(`dkgRound2 msg size: ${blob.byteLength}`)
287-
288-
let pos = 0;
289-
290-
blob.writeUint8(index, pos);
291-
pos += 1;
292-
blob.writeUint8(round1PublicPackages.length, pos);
293-
pos += 1;
294-
blob.writeUint16BE(round1PublicPackagesLen, pos);
295-
pos += 2;
296-
297-
for (let i = 0; i < round1PublicPackages.length; i++) {
298-
blob.fill(Buffer.from(round1PublicPackages[i], "hex"), pos)
299-
pos += round1PublicPackagesLen;
300-
}
301-
302-
blob.writeUint16BE(round1SecretPackageLen, pos);
303-
pos += 2;
304-
305-
blob.fill(Buffer.from(round1SecretPackage, "hex"), pos)
306-
pos += round1SecretPackageLen;
307-
308-
const chunks = this.prepareChunks(path, blob)
266+
async dkgRound2(index: number, round1PublicPackages: string[], round1SecretPackage: string): Promise<ResponseDkgRound2> {
267+
const blob = serializeDkgRound2(index, round1PublicPackages, round1SecretPackage)
268+
const chunks = this.prepareChunks(DUMMY_PATH, blob)
309269

310270
try{
311271
let response = Buffer.alloc(0)
@@ -362,21 +322,10 @@ export default class IronfishApp extends GenericApp {
362322
}
363323

364324
} else {
365-
let pos = 0
366-
const secretPackageLen = data.readUint16BE(pos)
367-
pos += 2
368-
const secretPackage = data.subarray(pos, pos + secretPackageLen)
369-
pos += secretPackageLen
370-
const publicPackageLen = data.readUint16BE(pos)
371-
pos += 2
372-
const publicPackage = data.subarray(pos, pos + publicPackageLen)
373-
pos += publicPackageLen
374-
375325
return {
376326
returnCode,
377327
errorMessage,
378-
secretPackage,
379-
publicPackage
328+
...deserializeDkgRound2(data)
380329
}
381330
}
382331
}
@@ -387,11 +336,9 @@ export default class IronfishApp extends GenericApp {
387336
}
388337

389338

390-
async dkgRound3(path: string, index: number, round1PublicPackages: string[], round2PublicPackages: string[], round2SecretPackage: string): Promise<ResponseDkgRound3> {
391-
const {participants, round2PublicPkgs, round1PublicPkgs, gskBytes } = minimizeRound3Inputs(index, round1PublicPackages, round2PublicPackages)
392-
const blob = encodeRound3Inputs(index, participants, round1PublicPkgs, round2PublicPkgs, round2SecretPackage, gskBytes)
393-
394-
const chunks = this.prepareChunks(path, blob)
339+
async dkgRound3(index: number, round1PublicPackages: string[], round2PublicPackages: string[], round2SecretPackage: string): Promise<ResponseDkgRound3> {
340+
const blob = serializeDkgRound3(index, round1PublicPackages, round2PublicPackages, round2SecretPackage)
341+
const chunks = this.prepareChunks(DUMMY_PATH, blob)
395342

396343
try{
397344
let response = Buffer.alloc(0)
@@ -461,21 +408,9 @@ export default class IronfishApp extends GenericApp {
461408
}
462409

463410

464-
async dkgGetCommitments(path: string, identities: string[], tx_hash: string): Promise<ResponseDkgGetCommitments> {
465-
let blob = Buffer
466-
.alloc(1 + identities.length * 129 + 32);
467-
console.log(`dkgGetCommitment msg size: ${blob.byteLength}`)
468-
469-
470-
blob.writeUint8(identities.length, 0);
471-
472-
for (let i = 0; i < identities.length; i++) {
473-
blob.fill(Buffer.from(identities[i], "hex"), 1 + (i * 129));
474-
}
475-
476-
blob.fill(Buffer.from(tx_hash, "hex"), 1 + identities.length * 129);
477-
478-
const chunks = this.prepareChunks(path, blob)
411+
async dkgGetCommitments(identities: string[], tx_hash: string): Promise<ResponseDkgGetCommitments> {
412+
const blob = serializeDkgGetCommitments(identities, tx_hash)
413+
const chunks = this.prepareChunks(DUMMY_PATH, blob)
479414

480415
try{
481416
let response = Buffer.alloc(0)
@@ -547,21 +482,9 @@ export default class IronfishApp extends GenericApp {
547482
}
548483

549484

550-
async dkgGetNonces(path: string, identities: string[], tx_hash: string): Promise<ResponseDkgGetNonces> {
551-
let blob = Buffer
552-
.alloc(1 + identities.length * 129 + 32);
553-
console.log(`dkgGetNonces msg size: ${blob.byteLength}`)
554-
555-
556-
blob.writeUint8(identities.length, 0);
557-
558-
for (let i = 0; i < identities.length; i++) {
559-
blob.fill(Buffer.from(identities[i], "hex"), 1 + (i * 129));
560-
}
561-
562-
blob.fill(Buffer.from(tx_hash, "hex"), 1 + identities.length * 129);
563-
564-
const chunks = this.prepareChunks(path, blob)
485+
async dkgGetNonces(identities: string[], tx_hash: string): Promise<ResponseDkgGetNonces> {
486+
const blob = serializeDkgGetNonces(identities, tx_hash)
487+
const chunks = this.prepareChunks(DUMMY_PATH, blob)
565488

566489
try{
567490
let response = Buffer.alloc(0)
@@ -632,32 +555,9 @@ export default class IronfishApp extends GenericApp {
632555
}
633556
}
634557

635-
async dkgSign(path: string, pkRandomness: string, frostSigningPackage: string, nonces: string): Promise<ResponseDkgSign> {
636-
let pkRandomnessLen = pkRandomness.length/2
637-
let frostSigningPackageLen = frostSigningPackage.length/2
638-
let noncesLen = nonces.length/2
639-
640-
let blob = Buffer
641-
.alloc(2 +pkRandomnessLen + 2 + frostSigningPackageLen + 2 + noncesLen);
642-
console.log(`dkgSign msg size: ${blob.byteLength}`)
643-
644-
let pos = 0;
645-
646-
blob.writeUint16BE(pkRandomnessLen, pos);
647-
pos += 2;
648-
blob.fill(Buffer.from(pkRandomness, "hex"), pos);
649-
pos += pkRandomnessLen;
650-
651-
blob.writeUint16BE(frostSigningPackageLen, pos);
652-
pos += 2;
653-
blob.fill(Buffer.from(frostSigningPackage, "hex"), pos);
654-
pos += frostSigningPackageLen;
655-
656-
blob.writeUint16BE(noncesLen, pos);
657-
pos += 2;
658-
blob.fill(Buffer.from(nonces, "hex"), pos);
659-
660-
const chunks = this.prepareChunks(path, blob)
558+
async dkgSign(pkRandomness: string, frostSigningPackage: string, nonces: string): Promise<ResponseDkgSign> {
559+
const blob = serializeDkgSign(pkRandomness, frostSigningPackage, nonces)
560+
const chunks = this.prepareChunks(DUMMY_PATH, blob)
661561

662562
try{
663563
let response = Buffer.alloc(0)
@@ -838,8 +738,8 @@ export default class IronfishApp extends GenericApp {
838738
}
839739

840740

841-
async dkgRestoreKeys(path: string, encryptedKeys: string): Promise<ResponseBase> {
842-
const chunks = this.prepareChunks(path, Buffer.from(encryptedKeys, "hex"))
741+
async dkgRestoreKeys(encryptedKeys: string): Promise<ResponseBase> {
742+
const chunks = this.prepareChunks(DUMMY_PATH, Buffer.from(encryptedKeys, "hex"))
843743

844744
try{
845745
let response = Buffer.alloc(0)

src/serialize.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { encodeRound3Inputs, minimizeRound3Inputs } from './ironfish'
2+
3+
export const serializeDkgRound1 = (index:number, identities:string[], minSigners:number) : Buffer => {
4+
let blob = Buffer
5+
.alloc(1 + 1 + identities.length * 129 + 1);
6+
console.log(`dkgRound1 msg size: ${blob.byteLength}`)
7+
8+
9+
blob.writeUint8(index);
10+
blob.writeUint8(identities.length, 1);
11+
for (let i = 0; i < identities.length; i++) {
12+
blob.fill(Buffer.from(identities[i], "hex"), 1 + 1 + (i * 129))
13+
}
14+
blob.writeUint8(minSigners, 1 + 1 + identities.length * 129);
15+
16+
return blob
17+
}
18+
19+
export const serializeDkgRound2 = (index:number, round1PublicPackages: string[], round1SecretPackage: string): Buffer => {
20+
let round1PublicPackagesLen = round1PublicPackages[0].length / 2
21+
let round1SecretPackageLen = round1SecretPackage.length / 2
22+
23+
let blob = Buffer
24+
.alloc(1 + 1 + 2 + round1PublicPackages.length * round1PublicPackagesLen + 2 + round1SecretPackageLen);
25+
console.log(`dkgRound2 msg size: ${blob.byteLength}`)
26+
27+
let pos = 0;
28+
29+
blob.writeUint8(index, pos);
30+
pos += 1;
31+
blob.writeUint8(round1PublicPackages.length, pos);
32+
pos += 1;
33+
blob.writeUint16BE(round1PublicPackagesLen, pos);
34+
pos += 2;
35+
36+
for (let i = 0; i < round1PublicPackages.length; i++) {
37+
blob.fill(Buffer.from(round1PublicPackages[i], "hex"), pos)
38+
pos += round1PublicPackagesLen;
39+
}
40+
41+
blob.writeUint16BE(round1SecretPackageLen, pos);
42+
pos += 2;
43+
44+
blob.fill(Buffer.from(round1SecretPackage, "hex"), pos)
45+
pos += round1SecretPackageLen;
46+
47+
return blob
48+
}
49+
50+
51+
export const serializeDkgRound3 = (index: number, round1PublicPackages: string[], round2PublicPackages: string[], round2SecretPackage: string): Buffer => {
52+
const {participants, round2PublicPkgs, round1PublicPkgs, gskBytes } = minimizeRound3Inputs(index, round1PublicPackages, round2PublicPackages)
53+
const blob = encodeRound3Inputs(index, participants, round1PublicPkgs, round2PublicPkgs, round2SecretPackage, gskBytes)
54+
55+
return blob
56+
}
57+
58+
export const serializeDkgGetCommitments = (identities: string[], tx_hash: string):Buffer => {
59+
let blob = Buffer
60+
.alloc(1 + identities.length * 129 + 32);
61+
console.log(`dkgGetCommitment msg size: ${blob.byteLength}`)
62+
63+
64+
blob.writeUint8(identities.length, 0);
65+
66+
for (let i = 0; i < identities.length; i++) {
67+
blob.fill(Buffer.from(identities[i], "hex"), 1 + (i * 129));
68+
}
69+
70+
blob.fill(Buffer.from(tx_hash, "hex"), 1 + identities.length * 129);
71+
72+
return blob
73+
}
74+
75+
export const serializeDkgGetNonces = (identities: string[], tx_hash: string) :Buffer=> {
76+
let blob = Buffer
77+
.alloc(1 + identities.length * 129 + 32);
78+
console.log(`dkgGetNonces msg size: ${blob.byteLength}`)
79+
80+
81+
blob.writeUint8(identities.length, 0);
82+
83+
for (let i = 0; i < identities.length; i++) {
84+
blob.fill(Buffer.from(identities[i], "hex"), 1 + (i * 129));
85+
}
86+
87+
blob.fill(Buffer.from(tx_hash, "hex"), 1 + identities.length * 129);
88+
return blob;
89+
}
90+
91+
export const serializeDkgSign = (pkRandomness: string, frostSigningPackage: string, nonces: string):Buffer => {
92+
let pkRandomnessLen = pkRandomness.length/2
93+
let frostSigningPackageLen = frostSigningPackage.length/2
94+
let noncesLen = nonces.length/2
95+
96+
let blob = Buffer
97+
.alloc(2 +pkRandomnessLen + 2 + frostSigningPackageLen + 2 + noncesLen);
98+
console.log(`dkgSign msg size: ${blob.byteLength}`)
99+
100+
let pos = 0;
101+
102+
blob.writeUint16BE(pkRandomnessLen, pos);
103+
pos += 2;
104+
blob.fill(Buffer.from(pkRandomness, "hex"), pos);
105+
pos += pkRandomnessLen;
106+
107+
blob.writeUint16BE(frostSigningPackageLen, pos);
108+
pos += 2;
109+
blob.fill(Buffer.from(frostSigningPackage, "hex"), pos);
110+
pos += frostSigningPackageLen;
111+
112+
blob.writeUint16BE(noncesLen, pos);
113+
pos += 2;
114+
blob.fill(Buffer.from(nonces, "hex"), pos);
115+
116+
return blob
117+
}

0 commit comments

Comments
 (0)