Skip to content

Commit

Permalink
Fix mock
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed Dec 22, 2023
1 parent adeba37 commit eee0630
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
1 change: 0 additions & 1 deletion assembly/__tests__/vm-mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ describe('Testing mocked Chain id', () => {
});

it('chain id mock value', () => {
let chainId = env.chainId();
expect(env.chainId()).toBe(9_000_000);
});
});
58 changes: 37 additions & 21 deletions vm-mock/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,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 @@ -263,7 +264,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 @@ -328,7 +330,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 @@ -365,7 +369,9 @@ export default function createMockedABI(
}

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

Expand All @@ -388,7 +394,9 @@ export default function createMockedABI(
}

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

Expand Down Expand Up @@ -476,8 +484,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 @@ -683,7 +690,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 @@ -721,8 +730,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 @@ -731,32 +743,37 @@ export default function createMockedABI(

const pubKeyBuf = getArrayBuffer(publicKeyPtr);
if (pubKeyBuf.byteLength !== 64) {
console.log('Invalid public key 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 @@ -766,8 +783,7 @@ export default function createMockedABI(
},

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

assembly_script_keccak256_hash(dataPtr) {
Expand All @@ -776,7 +792,7 @@ export default function createMockedABI(
return newArrayBuffer(hash);
},
assembly_script_set_chain_id(value) {
chainIdMock = value;
chainIdMock = BigInt(value);
},
assembly_script_chain_id() {
return chainIdMock;
Expand Down

0 comments on commit eee0630

Please sign in to comment.