Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wallet:transaction decrypting at most 255 notes #3950

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions ironfish/src/workerPool/tasks/decryptNotes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ describe('DecryptNotesRequest', () => {
const deserializedRequest = DecryptNotesRequest.deserializePayload(request.jobId, buffer)
expect(deserializedRequest).toEqual(request)
})

it('serializes over 255 notes', () => {
const length = 600

const request = new DecryptNotesRequest(
Array.from({ length }, () => ({
serializedNote: Buffer.alloc(ENCRYPTED_NOTE_LENGTH, 1),
incomingViewKey: Buffer.alloc(ACCOUNT_KEY_LENGTH, 1).toString('hex'),
outgoingViewKey: Buffer.alloc(ACCOUNT_KEY_LENGTH, 1).toString('hex'),
viewKey: Buffer.alloc(VIEW_KEY_LENGTH, 1).toString('hex'),
currentNoteIndex: 2,
decryptForSpender: true,
})),
0,
)
const buffer = serializePayloadToBuffer(request)
const deserializedRequest = DecryptNotesRequest.deserializePayload(request.jobId, buffer)
expect(deserializedRequest.payloads).toHaveLength(length)
})
})

describe('DecryptNotesResponse', () => {
Expand All @@ -54,6 +73,24 @@ describe('DecryptNotesResponse', () => {
const deserializedResponse = DecryptNotesResponse.deserializePayload(response.jobId, buffer)
expect(deserializedResponse).toEqual(response)
})

it('serializes over 255 notes', () => {
const length = 600

const request = new DecryptNotesResponse(
Array.from({ length }, () => ({
forSpender: false,
index: 1,
hash: Buffer.alloc(32, 1),
nullifier: Buffer.alloc(32, 1),
serializedNote: Buffer.alloc(DECRYPTED_NOTE_LENGTH, 1),
})),
0,
)
const buffer = serializePayloadToBuffer(request)
const deserializedResponse = DecryptNotesResponse.deserializePayload(request.jobId, buffer)
expect(deserializedResponse.notes).toHaveLength(length)
})
})

describe('DecryptNotesTask', () => {
Expand Down
12 changes: 4 additions & 8 deletions ironfish/src/workerPool/tasks/decryptNotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export class DecryptNotesRequest extends WorkerMessage {
}

serializePayload(bw: bufio.StaticWriter | bufio.BufferWriter): void {
bw.writeU8(this.payloads.length)
for (const payload of this.payloads) {
let flags = 0
flags |= Number(!!payload.currentNoteIndex) << 0
Expand All @@ -57,8 +56,7 @@ export class DecryptNotesRequest extends WorkerMessage {
const reader = bufio.read(buffer, true)
const payloads = []

const length = reader.readU8()
for (let i = 0; i < length; i++) {
while (reader.left() > 0) {
const flags = reader.readU8()
const hasCurrentNoteIndex = flags & (1 << 0)
const decryptForSpender = Boolean(flags & (1 << 1))
Expand All @@ -82,7 +80,7 @@ export class DecryptNotesRequest extends WorkerMessage {
}

getSize(): number {
let size = 1
let size = 0
for (const payload of this.payloads) {
size += 1
size += ENCRYPTED_NOTE_LENGTH
Expand All @@ -106,7 +104,6 @@ export class DecryptNotesResponse extends WorkerMessage {
}

serializePayload(bw: bufio.StaticWriter | bufio.BufferWriter): void {
bw.writeU8(this.notes.length)
for (const note of this.notes) {
const hasDecryptedNote = Number(!!note)
bw.writeU8(hasDecryptedNote)
Expand Down Expand Up @@ -135,8 +132,7 @@ export class DecryptNotesResponse extends WorkerMessage {
const reader = bufio.read(buffer)
const notes = []

const length = reader.readU8()
for (let i = 0; i < length; i++) {
while (reader.left() > 0) {
const hasDecryptedNote = reader.readU8()
if (!hasDecryptedNote) {
notes.push(null)
Expand Down Expand Up @@ -173,7 +169,7 @@ export class DecryptNotesResponse extends WorkerMessage {
}

getSize(): number {
let size = 1
let size = 0

for (const note of this.notes) {
size += 1
Expand Down