Skip to content

Commit 83e49dc

Browse files
authored
feat(ironfish): Store salt and nonce on encrypted account (#5371)
* feat(rust-nodejs): Create napi struct for xchacha20poly1305 key * feat(ironfish): Store salt and nonce on encrypted account * cargo fmt * Fix imports * Fix import
1 parent 6062764 commit 83e49dc

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

ironfish/src/wallet/account/encryptedAccount.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
/* This Source Code Form is subject to the terms of the Mozilla Public
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4-
import { decrypt } from '@ironfish/rust-nodejs'
4+
import { decrypt, xchacha20poly1305 } from '@ironfish/rust-nodejs'
55
import { AccountDecryptionFailedError } from '../errors'
66
import { AccountValueEncoding, EncryptedAccountValue } from '../walletdb/accountValue'
77
import { WalletDB } from '../walletdb/walletdb'
88
import { Account } from './account'
99

1010
export class EncryptedAccount {
1111
private readonly walletDb: WalletDB
12+
readonly salt: Buffer
13+
readonly nonce: Buffer
1214
readonly data: Buffer
1315

1416
constructor({ data, walletDb }: { data: Buffer; walletDb: WalletDB }) {
17+
this.salt = Buffer.alloc(xchacha20poly1305.XSALT_LENGTH)
18+
this.nonce = Buffer.alloc(xchacha20poly1305.XNONCE_LENGTH)
1519
this.data = data
1620
this.walletDb = walletDb
1721
}
@@ -31,6 +35,8 @@ export class EncryptedAccount {
3135
serialize(): EncryptedAccountValue {
3236
return {
3337
encrypted: true,
38+
salt: this.salt,
39+
nonce: this.nonce,
3440
data: this.data,
3541
}
3642
}

ironfish/src/wallet/walletdb/accountValue.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* This Source Code Form is subject to the terms of the Mozilla Public
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4-
import { encrypt, generateKey } from '@ironfish/rust-nodejs'
4+
import { encrypt, generateKey, xchacha20poly1305 } from '@ironfish/rust-nodejs'
55
import {
66
AccountValueEncoding,
77
DecryptedAccountValue,
@@ -95,6 +95,8 @@ describe('AccountValueEncoding', () => {
9595
const encryptedValue: EncryptedAccountValue = {
9696
encrypted: true,
9797
data: encryptedData,
98+
salt: Buffer.alloc(xchacha20poly1305.XSALT_LENGTH),
99+
nonce: Buffer.alloc(xchacha20poly1305.XNONCE_LENGTH),
98100
}
99101

100102
const buffer = encoder.serialize(encryptedValue)

ironfish/src/wallet/walletdb/accountValue.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* This Source Code Form is subject to the terms of the Mozilla Public
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4-
import { KEY_LENGTH, PUBLIC_ADDRESS_LENGTH } from '@ironfish/rust-nodejs'
4+
import { KEY_LENGTH, PUBLIC_ADDRESS_LENGTH, xchacha20poly1305 } from '@ironfish/rust-nodejs'
55
import bufio from 'bufio'
66
import { IDatabaseEncoding } from '../../storage'
77
import { MultisigKeys } from '../interfaces/multisigKeys'
@@ -13,6 +13,8 @@ const VERSION_LENGTH = 2
1313

1414
export type EncryptedAccountValue = {
1515
encrypted: true
16+
salt: Buffer
17+
nonce: Buffer
1618
data: Buffer
1719
}
1820

@@ -48,6 +50,8 @@ export class AccountValueEncoding implements IDatabaseEncoding<AccountValue> {
4850
let flags = 0
4951
flags |= Number(!!value.encrypted) << 5
5052
bw.writeU8(flags)
53+
bw.writeBytes(value.salt)
54+
bw.writeBytes(value.nonce)
5155
bw.writeVarBytes(value.data)
5256

5357
return bw.render()
@@ -113,9 +117,13 @@ export class AccountValueEncoding implements IDatabaseEncoding<AccountValue> {
113117
// Skip flags
114118
reader.readU8()
115119

120+
const salt = reader.readBytes(xchacha20poly1305.XSALT_LENGTH)
121+
const nonce = reader.readBytes(xchacha20poly1305.XNONCE_LENGTH)
116122
const data = reader.readVarBytes()
117123
return {
118124
encrypted: true,
125+
nonce,
126+
salt,
119127
data,
120128
}
121129
}
@@ -182,6 +190,8 @@ export class AccountValueEncoding implements IDatabaseEncoding<AccountValue> {
182190
getSizeEncrypted(value: EncryptedAccountValue): number {
183191
let size = 0
184192
size += 1 // flags
193+
size += xchacha20poly1305.XSALT_LENGTH
194+
size += xchacha20poly1305.XNONCE_LENGTH
185195
size += bufio.sizeVarBytes(value.data)
186196
return size
187197
}

0 commit comments

Comments
 (0)