Skip to content

Commit

Permalink
Merge pull request #311 from massalabs/fix-audit
Browse files Browse the repository at this point in the history
Fix audit
  • Loading branch information
Ben-Rey authored Jan 8, 2024
2 parents 4cc03c2 + 91238e5 commit c0620e7
Show file tree
Hide file tree
Showing 9 changed files with 1,995 additions and 417 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Massa-as-sdk

![check-code-coverage](https://img.shields.io/badge/coverage-83%%25-red)
![check-code-coverage](https://img.shields.io/badge/coverage-%25-red)

Massa-as-sdk is a collection of tools, objects, and functions specifically designed for Massa smart contracts in AssemblyScript. This SDK enables you to import object classes, such as address and storage objects, and use them without having to write them from scratch every time. Additionally, it allows you to use Massa's ABI functions.

Expand Down
2 changes: 1 addition & 1 deletion as-pect-ci.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import config from './as-pect.config.js';
config.coverage = ['assembly/**/*.ts'];
// config.coverage = ['assembly/**/*.ts']; // disabled for now waiting for as-pect fix
export default config;
18 changes: 17 additions & 1 deletion assembly/std/__tests__/context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
isDeployingContract,
timestamp,
} from '../context';
import { validateAddress } from '../utils';
import { validateAddress, json2Address } from '../utils';

describe('Context', () => {
test('ownedAddresses', () => {
Expand Down Expand Up @@ -45,4 +45,20 @@ describe('Context', () => {
const time = timestamp();
expect(time).toBeGreaterThan(0);
});

test('json2Address', () => {
const jsonString = '["address1","address2","address3"]';
const expectedOutput = [
new Address('address1'),
new Address('address2'),
new Address('address3'),
];

const result = json2Address(jsonString);

expect(result.length).toBe(expectedOutput.length);
for (let i = 0; i < result.length; i++) {
expect(result[i]).toBe(expectedOutput[i]);
}
});
});
6 changes: 6 additions & 0 deletions assembly/std/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const keySerOk3: StaticArray<u8> = [
];

const keysSerKo8: StaticArray<u8> = []; // edge case
const keysSerKo9: StaticArray<u8> = [0, 0]; // length < 4 (Minimal length)

describe('index tests', () => {
it('derOpKeys ok 1', () => {
Expand Down Expand Up @@ -73,6 +74,11 @@ describe('index tests', () => {
expect(res8.length).toBe(0);
});

it('derOpKeys ko 8', () => {
let res9 = derKeys(keysSerKo9);
expect(res9.length).toBe(0);
});

it('derOpKeys ok 3', () => {
let res = derKeys(keySerOk3);
expect(res.length).toBe(1);
Expand Down
23 changes: 1 addition & 22 deletions assembly/std/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import { env } from '../env/index';
import { Address } from './address';
import { callerHasWriteAccess, Context } from '.';
import { callerHasWriteAccess, Context, json2Address } from '.';

/**
* Determines whether the smart contract is currently being deployed.
Expand All @@ -48,27 +48,6 @@ export function isDeployingContract(): bool {
return callerHasWriteAccess() && Context.callee().notEqual(Context.caller());
}

/**
* Returns an array of addresses.
*
* Parses a JSON-encoded string of addresses and returns an array of `Address` objects.
*
* @remarks
* This function takes a string containing a JSON-encoded array of addresses
* (ex: "[address1,address2,...,addressN]") and returns an array of `Address`
* objects.
*
* @param str - A string containing a JSON-encoded array of addresses.
*
* @returns An array of `Address` objects, one for each address in the input string.
*/
function json2Address(str: string): Array<Address> {
str = str.substr(1, str.length - 2);

const a = str.split(',');
return a.map<Address>((x) => new Address(x.substring(1, x.length - 1)));
}

/**
* Returns the addresses that the current execution context has write access to.
*
Expand Down
5 changes: 4 additions & 1 deletion assembly/std/op-datastore/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*
*/
function getNumberOfKeys(keysSer: StaticArray<u8>): u32 {
// check if keysSer is more than 4 bytes
assert(keysSer.length >= 4, 'Invalid keysSer length');
// The first 4 bytes of the input array represent the number of keys
let arr = new Uint8Array(4);
arr[0] = keysSer[0];
Expand Down Expand Up @@ -46,7 +48,8 @@ function getNumberOfKeys(keysSer: StaticArray<u8>): u32 {
*
*/
export function derKeys(keysSer: StaticArray<u8>): Array<StaticArray<u8>> {
if (keysSer.length == 0) return [];
// check if keysSer is more than 4 bytes (for the number of keys)
if (keysSer.length < 4) return [];

const keyCount: u32 = getNumberOfKeys(keysSer);
const keys = new Array<StaticArray<u8>>(keyCount);
Expand Down
21 changes: 21 additions & 0 deletions assembly/std/utils/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,24 @@ export function validateAddress(address: string): bool {
export function isAddressEoa(address: string): bool {
return env.isAddressEoa(address);
}

/**
* Returns an array of addresses.
*
* Parses a JSON-encoded string of addresses and returns an array of `Address` objects.
*
* @remarks
* This function takes a string containing a JSON-encoded array of addresses
* (ex: "[address1,address2,...,addressN]") and returns an array of `Address`
* objects.
*
* @param str - A string containing a JSON-encoded array of addresses.
*
* @returns An array of `Address` objects, one for each address in the input string.
*/
export function json2Address(str: string): Array<Address> {
str = str.substring(1, str.length - 1);

const a = str.split(',');
return a.map<Address>((x) => new Address(x.substring(1, x.length - 1)));
}
Loading

0 comments on commit c0620e7

Please sign in to comment.