Skip to content

Commit 8a213b7

Browse files
authored
Disallow creation of accounts with no names (#5312)
1 parent f41ef7c commit 8a213b7

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

ironfish/src/wallet/account/__fixtures__/account.test.ts.fixture

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5841,5 +5841,67 @@
58415841
"sequence": 1
58425842
}
58435843
}
5844+
],
5845+
"Accounts setName should rename an account": [
5846+
{
5847+
"value": {
5848+
"encrypted": false,
5849+
"version": 4,
5850+
"id": "4d934926-259d-4926-958d-a4d6b3e4d50c",
5851+
"name": "accountA",
5852+
"spendingKey": "d680a4a56c52fee0832800067948d66c0296f06970252a01cfb8368be8a8d141",
5853+
"viewKey": "b21cec78809681a5ca376220c7b75aeaff2e7b6159bb07db194a072e568f56beb2a8804428dc4fb36fa8dd99347312e8570386a5f55a9844555778149a352d4e",
5854+
"incomingViewKey": "9aad17ed39340920700c12392762953da2c7a14df0d2afea783d84cb19401b03",
5855+
"outgoingViewKey": "0ba463093a0f32b0b163bbfc88a398eb22386363457a9f786cc40d226f80ce18",
5856+
"publicAddress": "abd628bdcf088f7465f98628de06723514415b9511c859b943db519738f70552",
5857+
"createdAt": {
5858+
"hash": {
5859+
"type": "Buffer",
5860+
"data": "base64:R5HXrp+X3xAO8VWOhHctagm0N2I4goP3XG8goyqIqoY="
5861+
},
5862+
"sequence": 1
5863+
},
5864+
"scanningEnabled": true,
5865+
"proofAuthorizingKey": "e839d3ed4a71d618b48bac49b51b94766437410813775eaf976d3e4c29cdb909"
5866+
},
5867+
"head": {
5868+
"hash": {
5869+
"type": "Buffer",
5870+
"data": "base64:R5HXrp+X3xAO8VWOhHctagm0N2I4goP3XG8goyqIqoY="
5871+
},
5872+
"sequence": 1
5873+
}
5874+
}
5875+
],
5876+
"Accounts setName should not allow blank names": [
5877+
{
5878+
"value": {
5879+
"encrypted": false,
5880+
"version": 4,
5881+
"id": "a4d623c5-1816-4dcd-b152-8581e3184482",
5882+
"name": "accountA",
5883+
"spendingKey": "184d9512d2e35b4ef90ad6bce36874da8e2570bd16705ecd7b2e3bc3b8b2790c",
5884+
"viewKey": "9b88c9881b603e5599fbd8cc3d6e2538e291b325a2d85c6d863f4dc8e5e65d0422f8d8b14eb0ac9714d9e90dfc84d94e3aab030100a71c06c7840f1ddca5454e",
5885+
"incomingViewKey": "4c72a84c56a21765950d6e835b250277590bfec11f0311ee162f48ac563c4007",
5886+
"outgoingViewKey": "d8fce77c8c7660c7028246828316029e6eede4767688533b3d4ef83e97d63d99",
5887+
"publicAddress": "d4e04322213119f855d6ba0e56ef4da36f5d72d3abbf81ed2ff951a6d022b6a0",
5888+
"createdAt": {
5889+
"hash": {
5890+
"type": "Buffer",
5891+
"data": "base64:R5HXrp+X3xAO8VWOhHctagm0N2I4goP3XG8goyqIqoY="
5892+
},
5893+
"sequence": 1
5894+
},
5895+
"scanningEnabled": true,
5896+
"proofAuthorizingKey": "7f0eabcd1ab1efe3818ad7d20cae55a6def4bb2850baacdca3aca03a18bbf30a"
5897+
},
5898+
"head": {
5899+
"hash": {
5900+
"type": "Buffer",
5901+
"data": "base64:R5HXrp+X3xAO8VWOhHctagm0N2I4goP3XG8goyqIqoY="
5902+
},
5903+
"sequence": 1
5904+
}
5905+
}
58445906
]
58455907
}

ironfish/src/wallet/account/account.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,27 @@ describe('Accounts', () => {
167167
await expect(account.getTransaction(tx.hash())).resolves.toBeDefined()
168168
})
169169

170+
describe('setName', () => {
171+
it('should rename an account', async () => {
172+
const { node } = nodeTest
173+
174+
const account = await useAccountFixture(node.wallet, 'accountA')
175+
176+
await account.setName('newName')
177+
178+
expect(node.wallet.getAccountByName('newName')).toBeDefined()
179+
})
180+
181+
it('should not allow blank names', async () => {
182+
const { node } = nodeTest
183+
184+
const account = await useAccountFixture(node.wallet, 'accountA')
185+
186+
await expect(account.setName('')).rejects.toThrow('Account name cannot be blank')
187+
await expect(account.setName(' ')).rejects.toThrow('Account name cannot be blank')
188+
})
189+
})
190+
170191
describe('loadPendingTransactions', () => {
171192
it('should load pending transactions', async () => {
172193
const { node } = nodeTest

ironfish/src/wallet/account/account.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ export class Account {
128128
}
129129

130130
async setName(name: string, tx?: IDatabaseTransaction): Promise<void> {
131+
if (!name.trim()) {
132+
throw new Error('Account name cannot be blank')
133+
}
134+
131135
this.name = name
132136

133137
await this.walletDb.setAccount(this, tx)

ironfish/src/wallet/wallet.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,18 @@ describe('Wallet', () => {
809809
expect(head?.hash).toEqualHash(block2.header.hash)
810810
expect(head?.sequence).toEqual(block2.header.sequence)
811811
})
812+
813+
it('should not allow blank names', async () => {
814+
const node = nodeTest.node
815+
816+
await expect(node.wallet.createAccount('')).rejects.toThrow(
817+
'Account name cannot be blank',
818+
)
819+
820+
await expect(node.wallet.createAccount(' ')).rejects.toThrow(
821+
'Account name cannot be blank',
822+
)
823+
})
812824
})
813825

814826
describe('removeAccount', () => {

ironfish/src/wallet/wallet.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,10 @@ export class Wallet {
12951295
setDefault: false,
12961296
},
12971297
): Promise<Account> {
1298+
if (!name.trim()) {
1299+
throw new Error('Account name cannot be blank')
1300+
}
1301+
12981302
if (this.getAccountByName(name)) {
12991303
throw new DuplicateAccountNameError(name)
13001304
}

0 commit comments

Comments
 (0)