-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* first commit * first commit * feat: changes * feat: small change * feat: more changes, still tests to add * feat: more changes, still tests to add * feat: more changes, still tests to add * feat: more changes, still tests to add * feat: more changes, still tests to add * feat: more changes, still tests to add * feat: more changes, still tests to add * feat: more changes, still tests to add * feat: more changes, still tests to add * feat: added key tests * feat: increased coverage * feat: vcdm diagram modified * feat: account and externallyownedaccount * feat: account and externallyownedaccount * feat: account and externallyownedaccount * feat: added test * feat: added test * feat: more tests * feat: more tests * feat: more tests * feat: more coverage * feat: more coverage * feat: updated class diagram * feat: updated comments in parent interface * feat: removed TODOs as per code review
- Loading branch information
1 parent
353aad8
commit 755ce19
Showing
13 changed files
with
388 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { InvalidOperation } from '@vechain/sdk-errors'; | ||
import { type Address } from '../Address'; | ||
import { type Currency } from '../Currency'; | ||
import { type VeChainDataModel } from '../VeChainDataModel'; | ||
|
||
type AccountType = 'EOA' | 'Contract'; | ||
|
||
/** | ||
* Represents a VeChain account. | ||
* | ||
* @implements {VeChainDataModel<Account>} | ||
*/ | ||
abstract class Account implements VeChainDataModel<Account> { | ||
public readonly address: Address; | ||
public readonly balance: Currency; | ||
// Replace the string array with a Transaction class #1162 | ||
public readonly transactions: string[]; | ||
protected abstract get type(): AccountType; | ||
|
||
constructor(address: Address, balance: Currency, transactions?: string[]) { | ||
this.address = address; | ||
this.balance = balance; | ||
this.transactions = transactions ?? []; | ||
} | ||
|
||
/** | ||
* Throws an exception because the account cannot be represented as a big integer. | ||
* @returns {bigint} The BigInt representation of the account. | ||
* @throws {InvalidOperation} The account cannot be represented as a bigint. | ||
* @override {@link VeChainDataModel#bi} | ||
* @remark The conversion to BigInt is not supported for an account. | ||
*/ | ||
public get bi(): bigint { | ||
throw new InvalidOperation( | ||
'Account.bi', | ||
'There is no big integer representation for an account.', | ||
{ data: '' } | ||
); | ||
} | ||
|
||
/** | ||
* Throws an exception because the account cannot be represented as a byte array. | ||
* @returns {Uint8Array} The byte array representation of the account. | ||
* @throws {InvalidOperation} The account cannot be represented as a byte array. | ||
* @override {@link VeChainDataModel#bytes} | ||
* @remark The conversion to byte array is not supported for an account. | ||
*/ | ||
public get bytes(): Uint8Array { | ||
throw new InvalidOperation( | ||
'Account.bytes', | ||
'There is no bytes representation for an account.', | ||
{ data: '' } | ||
); | ||
} | ||
|
||
/** | ||
* Throws an exception because the account cannot be represented as a number. | ||
* @returns {bigint} The number representation of the account. | ||
* @throws {InvalidOperation} The account cannot be represented as a number. | ||
* @override {@link VeChainDataModel#n} | ||
* @remark The conversion to number is not supported for an account. | ||
*/ | ||
public get n(): number { | ||
throw new InvalidOperation( | ||
'Account.n', | ||
'There is no number representation for an account.', | ||
{ data: '' } | ||
); | ||
} | ||
|
||
/** | ||
* Adds a transaction to the account. | ||
* @param {string} transaction The transaction to add. | ||
*/ | ||
public addTransaction(transaction: string): void { | ||
// Replace body once Transaction class is implemented #1162 | ||
this.transactions.push(transaction); | ||
} | ||
|
||
/** | ||
* Compare this instance with `that` in a meaningful way. | ||
* | ||
* @param {Account} that object to compare. | ||
* @return a negative number if `this` < `that`, zero if `this` = `that`, a positive number if `this` > that`. | ||
* @override {@link VeChainDataModel#compareTo} | ||
*/ | ||
public compareTo(that: Account): number { | ||
const typeCompareTo = this.type.localeCompare(that.type); | ||
if (typeCompareTo !== 0) { | ||
return typeCompareTo; | ||
} | ||
const addressCompareTo = this.address.compareTo(that.address); | ||
if (addressCompareTo !== 0) { | ||
return addressCompareTo; | ||
} | ||
return this.balance.compareTo(that.balance); | ||
} | ||
|
||
/** | ||
* Checks if the given value is equal to the current instance. | ||
* | ||
* @param {Account} that - The value to compare. | ||
* @returns {boolean} - True if the values are equal, false otherwise. | ||
* @override {@link VeChainDataModel#isEqual} | ||
*/ | ||
public isEqual(that: Account): boolean { | ||
return this.compareTo(that) === 0; | ||
} | ||
|
||
/** | ||
* Returns a string representation of the account. | ||
* | ||
* @returns {string} A string representation of the account. | ||
*/ | ||
public toString(): string { | ||
return `${this.type} Address: ${this.address.toString()} Balance: ${this.balance.bi.toString()}`; | ||
} | ||
} | ||
|
||
export { Account }; | ||
export type { AccountType }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { InvalidDataType } from '@vechain/sdk-errors'; | ||
import { Address } from '../Address'; | ||
import { type Currency } from '../Currency'; | ||
import { type Mnemonic } from '../Mnemonic'; | ||
import { Account, type AccountType } from './Account'; | ||
|
||
/** | ||
* Represents an externally owned account (EOA) on the VeChainThor blockchain. | ||
* | ||
* @extends {Account} | ||
*/ | ||
class ExternallyOwnedAccount extends Account { | ||
readonly type: AccountType = 'EOA'; | ||
|
||
private readonly mnemonic: Mnemonic; | ||
// Review whether we need to add the SECP256k1 key pair here #1122 | ||
|
||
constructor( | ||
address: Address, | ||
balance: Currency, | ||
mnemonic: Mnemonic, | ||
transactions?: string[] | ||
) { | ||
if (!ExternallyOwnedAccount.isValid(address, mnemonic)) { | ||
throw new InvalidDataType( | ||
'ExternallyOwnedAccount.constructor', | ||
'The address and mnemonic do not match.', | ||
{ address: address.toString() } | ||
); | ||
} | ||
super(address, balance, transactions); | ||
this.mnemonic = mnemonic; | ||
} | ||
|
||
/** | ||
* Validates that the given address and mnemonic's address match. | ||
* @param {Address} address Address to validate. | ||
* @param {Mnemonic} mnemonic Mnemonic to validate. | ||
* @returns {boolean} True if the address and mnemonic's address match, false otherwise. | ||
*/ | ||
public static isValid(address: Address, mnemonic: Mnemonic): boolean { | ||
const addressFromMnemonic = Address.ofMnemonic(mnemonic); | ||
return address.isEqual(addressFromMnemonic); | ||
} | ||
|
||
/** | ||
* Compares the current ExternallyOwnedAccount object with the given ExternallyOwnedAccount object. | ||
* @param {ExternallyOwnedAccount} that - The ExternallyOwnedAccount object to compare with. | ||
* @return {number} - A negative number if the current object is less than the given object, | ||
* zero if they are equal, or a positive number if the current object is greater. | ||
* @override {@link Account#compareTo} | ||
* @remark The comparison is based on the address and mnemonic of the ExternallyOwnedAccount. | ||
*/ | ||
public compareTo(that: ExternallyOwnedAccount): number { | ||
const accountCompareTo = super.compareTo(that); | ||
if (accountCompareTo !== 0) { | ||
return accountCompareTo; | ||
} | ||
return this.mnemonic.compareTo(that.mnemonic); | ||
} | ||
|
||
/** | ||
* Checks if the current ExternallyOwnedAccount object is equal to the given ExternallyOwnedAccount object. | ||
* @param {ExternallyOwnedAccount} that - The ExternallyOwnedAccount object to compare with. | ||
* @return {boolean} - True if the objects are equal, false otherwise. | ||
* @override {@link Account#isEqual} | ||
* @remark The comparison is based on the address and mnemonic of the ExternallyOwnedAccount. | ||
*/ | ||
public isEqual(that: ExternallyOwnedAccount): boolean { | ||
return super.isEqual(that) && this.mnemonic.isEqual(that.mnemonic); | ||
} | ||
|
||
/** | ||
* Returns a string representation of the ExternallyOwnedAccount. | ||
* | ||
* @returns {string} A string representation of the ExternallyOwnedAccount. | ||
*/ | ||
public toString(): string { | ||
return `${super.toString()} Mnemonic: ${this.mnemonic.toString()}`; | ||
} | ||
} | ||
|
||
export { ExternallyOwnedAccount }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './Account'; | ||
export * from './ExternallyOwnedAccount'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './account'; | ||
export * from './Address'; | ||
export * from './Currency'; | ||
export * from './Hash'; | ||
|
Oops, something went wrong.
755ce19
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test Coverage
Summary