Skip to content

Commit

Permalink
Add is contract address
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed May 23, 2024
1 parent 3594583 commit ec9fd24
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
21 changes: 19 additions & 2 deletions assembly/std/__tests__/address.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Args } from '@massalabs/as-types';
import { Address } from '../address';
import { validateAddress } from '../utils';
import { addAddressToLedger } from '../../vm-mock';
import { setBytecodeOf } from '../contract';

describe('Address tests', () => {
it('basic tests', () => {
Expand Down Expand Up @@ -46,8 +48,8 @@ describe('Address tests', () => {

it('multiple args', () => {
[
'A12LmTm4zRYkUQZusw7eevvV5ySzSwndJpENQ7EZHcmDbWafx96T',
'A1aMywGBgBywiL6WcbKR4ugxoBtdP9P3waBVi5e713uvj7F1DJL',
'AU12LmTm4zRYkUQZusw7eevvV5ySzSwndJpENQ7EZHcmDbWafx96T',
'AU12LmTm4zRYkUQZusw7eevvV5ySzSwndJpENQ7EZHcmDbWafx96T',
].forEach((input) => {
const theNumber = 4;
const args = new Args()
Expand All @@ -59,4 +61,19 @@ describe('Address tests', () => {
expect(deserialized.toString()).toBe(input);
});
});

test('is user address', () => {
const userAddress = new Address(
'AU1aMywGBgBywiL6WcbKR4ugxoBtdP9P3waBVi5e713uvj7F1DJL',
);
expect(userAddress.isUser()).toBeTruthy();
});

test('is contract address', () => {
const sc = 'AS1aMywGBgBywiL6WcbKR4ugxoBtdP9P3waBVi5e713uvj7F1DJL';
addAddressToLedger(sc);
const contractAddress = new Address(sc);
setBytecodeOf(contractAddress, [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
expect(contractAddress.isSmartContract()).toBeTruthy();
});
});
29 changes: 29 additions & 0 deletions assembly/std/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/

import { Serializable, Args, Result } from '@massalabs/as-types';
import { validateAddress } from './utils';
import { getBytecodeOf } from './contract/bytecode';

Check warning on line 14 in assembly/std/address.ts

View workflow job for this annotation

GitHub Actions / build

'getBytecodeOf' is defined but never used. Allowed unused vars must match /^_|^[A-Z](?:From|To)?$/u

/**
* Represents a Massa blockchain address.
Expand Down Expand Up @@ -114,4 +116,31 @@ export class Address implements Serializable {
notEqual(address: Address): boolean {
return !(this == address);
}

isValid(): bool {
return validateAddress(this._value);
}

/**
* Checks if the address is a smart contract.
*
* @param address - The address to check.
*
* @returns `true` if the address is a smart contract, `false` otherwise.
*
* @throws
* If no bytecode is found at the address.
*/
isSmartContract(): bool {
return !this._value.startsWith('AS') && !this.isValid();
}

/**
* Checks if the address is an External owned account address.
*
* @returns `true` if the address is a Eoa address, `false` otherwise.
*/
isEoa(): bool {
return this._value.startsWith('AU') && this.isValid();
}
}
3 changes: 2 additions & 1 deletion assembly/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"excludeProtected": true,
"excludeExternals": true,
"includeVersion": true,
"skipErrorChecking": true
"skipErrorChecking": true,
"experimentalDecorators": true
}
}

0 comments on commit ec9fd24

Please sign in to comment.