diff --git a/src/account.ts b/src/account.ts index 1cc691d7..e7cbb349 100644 --- a/src/account.ts +++ b/src/account.ts @@ -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( @@ -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') + } } /** diff --git a/test/account.spec.ts b/test/account.spec.ts index 2691502f..c999b357 100644 --- a/test/account.spec.ts +++ b/test/account.spec.ts @@ -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)) + }) + }) }) })