Skip to content

Commit

Permalink
Merge pull request #316 from massalabs/feature/chainid_2
Browse files Browse the repository at this point in the history
Feature/chainid 2
  • Loading branch information
Ben-Rey authored Dec 22, 2023
2 parents 4ffc6d8 + e51ae7a commit cda6c80
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 20 deletions.
15 changes: 15 additions & 0 deletions assembly/__tests__/vm-mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { changeCallStack, resetStorage } from '../vm-mock/storage';
import {
mockAdminContext,
mockSetChainId,
setDeployContext,
setLocalContext,
} from '../vm-mock/env';
Expand Down Expand Up @@ -290,3 +291,17 @@ describe('Testing mocked Context', () => {
);
});
});

describe('Testing mocked Chain id', () => {
beforeEach(() => {
mockSetChainId(9_000_000);
});

afterAll(() => {
mockSetChainId(77);
});

it('chain id mock value', () => {
expect(env.chainId()).toBe(9_000_000);
});
});
3 changes: 3 additions & 0 deletions assembly/env/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,7 @@ export namespace env {

@external("massa", "assembly_script_validate_address")
export declare function validateAddress(address: string): bool;

@external("massa", "assembly_script_chain_id")
export declare function chainId(): u64;
}
9 changes: 9 additions & 0 deletions assembly/std/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,12 @@ export function currentPeriod(): u64 {
export function currentThread(): u8 {
return env.currentThread();
}

/**
* Retrieve the current chain id
*
* @returns the current chain id.
*/
export function chainId(): u64 {
return env.chainId();
}
3 changes: 3 additions & 0 deletions assembly/vm-mock/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,6 @@ export declare function setDeployContext(callerAddress?: string): void;
*/
@external("massa", "assembly_script_set_local_context")
export declare function setLocalContext(address?: string): void;

@external("massa", "assembly_script_set_chain_id")
export declare function mockSetChainId(value: number): void;
64 changes: 44 additions & 20 deletions vm-mock/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function resetLedger() {
let webModule;

const scCallMockStack = [];
let chainIdMock = BigInt(77658366); // Default value, chain id for Buildnet

/**
* Create a mock vm to simulate calls and responses of Massa WebAssembly sdk.
Expand Down Expand Up @@ -216,6 +217,11 @@ export default function createMockedABI(
);
}

function concatenateArrays(oldValue, newValue) {

Check warning on line 220 in vm-mock/vm.js

View workflow job for this annotation

GitHub Actions / build

Missing JSDoc comment

Check warning on line 220 in vm-mock/vm.js

View workflow job for this annotation

GitHub Actions / test / build

Missing JSDoc comment
const totalLength = oldValue.byteLength + newValue.byteLength;
return new Uint8Array(totalLength);
}

resetLedger();

const myImports = {
Expand Down Expand Up @@ -250,9 +256,10 @@ export default function createMockedABI(
throw new Error('Runtime error: data entry not found');
}
} else {
throw new Error('Runtime error: address parsing error: ' + contractAddress);
throw new Error(
'Runtime error: address parsing error: ' + contractAddress,
);
}

},

assembly_script_reset_storage() {
Expand All @@ -262,7 +269,8 @@ export default function createMockedABI(
assembly_script_change_call_stack(callstackPtr) {
callStack = ptrToString(callstackPtr);
callerAddress = callStack.split(' , ')[0];
contractAddress = (callStack.split(' , ').length > 1) ? callStack.split(' , ')[1] : '';
contractAddress =
callStack.split(' , ').length > 1 ? callStack.split(' , ')[1] : '';
},

assembly_script_generate_event(msgPtr) {
Expand Down Expand Up @@ -327,7 +335,9 @@ export default function createMockedABI(
}
addressStorage.delete(k);
} else {
throw new Error('Runtime error: address parsing error: ' + contractAddress);
throw new Error(
'Runtime error: address parsing error: ' + contractAddress,
);
}
},

Expand Down Expand Up @@ -364,7 +374,7 @@ export default function createMockedABI(
}

const oldValue = addressStorage.get(key);
const concat = new Uint8Array(oldValue.byteLength + newValue.byteLength);
const concat = concatenateArrays(oldValue, newValue);
concat.set(new Uint8Array(oldValue), 0);
concat.set(new Uint8Array(newValue), oldValue.byteLength);

Expand All @@ -387,7 +397,7 @@ export default function createMockedABI(
}

const oldValue = addressStorage.get(key);
const concat = new Uint8Array(oldValue.byteLength + newValue.byteLength);
const concat = concatenateArrays(oldValue, newValue);
concat.set(new Uint8Array(oldValue), 0);
concat.set(new Uint8Array(newValue), oldValue.byteLength);

Expand Down Expand Up @@ -475,8 +485,7 @@ export default function createMockedABI(
if (!addrPtr) {
// if the address is not set, uses the current contract address as caller address
callerAddress = contractAddress;
}
else {
} else {
callerAddress = ptrToString(addrPtr);
contractAddress = callerAddress;
}
Expand Down Expand Up @@ -682,7 +691,9 @@ export default function createMockedABI(
const addressTo = ptrToString(_addressToPtr);

if (!ledger.has(addressFrom)) {
throw new Error('Runtime error: address parsing error: ' + addressFrom);
throw new Error(
'Runtime error: address parsing error: ' + addressFrom,
);
}

if (!ledger.has(addressTo)) {
Expand Down Expand Up @@ -720,8 +731,11 @@ export default function createMockedABI(
return true;
},

assembly_script_evm_signature_verify(dataPtr, signaturePtr, publicKeyPtr) {

assembly_script_evm_signature_verify(
dataPtr,
signaturePtr,
publicKeyPtr,
) {
const signatureBuf = getArrayBuffer(signaturePtr);
if (signatureBuf.byteLength !== 65) {
console.log('Invalid signature length. Expected 65 bytes');
Expand All @@ -730,32 +744,37 @@ export default function createMockedABI(

const pubKeyBuf = getArrayBuffer(publicKeyPtr);
if (pubKeyBuf.byteLength !== 64) {
console.log('Invalid publickey length. Expected 64 bytes uncompressed secp256k1 public key');
console.log(
'Invalid public key length. Expected 64 bytes uncompressed secp256k1 public key',
);
throw new Error();
}

const digest = hashMessage(new Uint8Array(getArrayBuffer(dataPtr)));
const signature = "0x" + Buffer.from(signatureBuf).toString('hex');
const signature = '0x' + Buffer.from(signatureBuf).toString('hex');
const recovered = SigningKey.recoverPublicKey(digest, signature);

const publicKey = "0x" + "04"/* compression header*/ + Buffer.from(pubKeyBuf).toString('hex');
const publicKey =
'0x' +
'04' /* compression header*/ +
Buffer.from(pubKeyBuf).toString('hex');
return recovered === publicKey;
},

assembly_script_evm_get_pubkey_from_signature(dataPtr, signaturePtr) {

const signatureBuf = getArrayBuffer(signaturePtr);
if (signatureBuf.byteLength !== 65) {
console.log('Invalid signature length. Expected 65 bytes');
throw new Error();
}
const digest = "0x" + Buffer.from(getArrayBuffer(dataPtr)).toString('hex');
const digest =
'0x' + Buffer.from(getArrayBuffer(dataPtr)).toString('hex');

const signature = "0x" + Buffer.from(signatureBuf).toString('hex');
const signature = '0x' + Buffer.from(signatureBuf).toString('hex');

const recovered = SigningKey.recoverPublicKey(digest, signature);

return newArrayBuffer(Buffer.from(recovered.substring(2), "hex"));
return newArrayBuffer(Buffer.from(recovered.substring(2), 'hex'));
},

assembly_script_address_from_public_key(publicKeyPtr) {
Expand All @@ -765,15 +784,20 @@ export default function createMockedABI(
},

assembly_script_get_origin_operation_id() {
return newString(generateRandOpId()
);
return newString(generateRandOpId());
},

assembly_script_keccak256_hash(dataPtr) {
const data = getArrayBuffer(dataPtr);
const hash = sha3.keccak256.arrayBuffer(data);
return newArrayBuffer(hash);
},
assembly_script_set_chain_id(value) {
chainIdMock = BigInt(value);
},
assembly_script_chain_id() {
return chainIdMock;
},
},
};

Expand Down

0 comments on commit cda6c80

Please sign in to comment.