Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

Commit

Permalink
add validation for nonce and balance
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanio committed Sep 30, 2020
1 parent 69a5ef4 commit b0aacc0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class Account {
}

/**
* This constructor takes the values, validates and assigns them.
* This constructor assigns and validates the values.
* Use the static factory methods to assist in creating an Account from varying data types.
*/
constructor(
Expand All @@ -70,17 +70,27 @@ export class Account {
stateRoot = KECCAK256_RLP,
codeHash = KECCAK256_NULL,
) {
if (stateRoot.length !== 32) {
throw new Error('stateRoot must have a length of 32')
}
if (codeHash.length !== 32) {
throw new Error('codeHash must have a length of 32')
}

this.nonce = nonce
this.balance = balance
this.stateRoot = stateRoot
this.codeHash = codeHash

this._validate()
}

private _validate() {
if (this.nonce.lt(new BN(0))) {
throw new Error('nonce must be greater than zero')
}
if (this.balance.lt(new BN(0))) {
throw new Error('balance must be greater than zero')
}
if (this.stateRoot.length !== 32) {
throw new Error('stateRoot must have a length of 32')
}
if (this.codeHash.length !== 32) {
throw new Error('codeHash must have a length of 32')
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions test/account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,18 @@ describe('Account', function() {
Account.fromRlpSerializedAccount(data as any)
})
})

it('should not accept nonce less than 0', function() {
assert.throws(() => {
new Account(new BN(-5))
})
})

it('should not accept balance less than 0', function() {
assert.throws(() => {
new Account(undefined, new BN(-5))
})
})
})
})

Expand Down

0 comments on commit b0aacc0

Please sign in to comment.