Skip to content

Commit

Permalink
Merge pull request #284 from massalabs/evm-update
Browse files Browse the repository at this point in the history
Evm update
  • Loading branch information
Ben-Rey authored Jul 25, 2023
2 parents 5df51fc + fee3db5 commit dd0e551
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 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-78%25-orange)
![check-code-coverage](https://img.shields.io/badge/coverage-77%25-orange)

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
14 changes: 14 additions & 0 deletions assembly/env/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ export namespace env {
publicKey: StaticArray<u8>,
): bool;

@external("massa", "assembly_script_evm_get_address_from_pubkey")
export declare function evmGetAddressFromPubkey(
publicKey: StaticArray<u8>,
): StaticArray<u8>;

@external("massa", "assembly_script_evm_get_pubkey_from_signature")
export declare function evmGetPubkeyFromSignature(
hash: StaticArray<u8>,
signature: StaticArray<u8>,
): StaticArray<u8>;

@external("massa", "assembly_script_is_address_eoa")
export declare function isAddressEoa(address: string): bool;

@external("massa", "assembly_script_address_from_public_key")
export declare function publicKeyToAddress(publicKey: string): string;

Expand Down
11 changes: 11 additions & 0 deletions assembly/std/utils/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,14 @@ export function publicKeyToAddress(pubKey: string): Address {
export function validateAddress(address: string): bool {
return env.validateAddress(address);
}

/**
* Checks if the given address is a user or a contract address.
*
* @param address - the string address to identify.
*
* @returns 'true' if the address is a user address, 'false' otherwise.
*/
export function isAddressEoa(address: string): bool {
return env.isAddressEoa(address);
}
36 changes: 34 additions & 2 deletions assembly/std/utils/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ export function isSignatureValid(
* Checks if an EVM signature is valid.
*
* @param digest - Digest message.
* @param publicKey - Expects a public key in ETH full format.
* Length: 65 bytes
* @param signature - Expects a SECP256K1 signature in full ETH format.
* Format: (r, s, v) v will be ignored.
* Length: 65 bytes
* @param publicKey - Expects a SECP256K1 public key in raw format.
* Length: 64 bytes
*
* @returns 'true' if the signature is valid, 'false' otherwise.
*
Expand All @@ -66,6 +66,38 @@ export function isEvmSignatureValid(
return env.isEvmSignatureValid(digest, signature, publicKey);
}

/**
* Get an EVM address from a public key.
*
* @param publicKey - Expects a SECP256K1 public key in raw format.
* Length: 64 bytes
*
* @returns The EVM address corresponding to the public key, serialized as a `StaticArray<u8>`.
*
*/
export function evmGetAddressFromPubkey(
publicKey: StaticArray<u8>,
): StaticArray<u8> {
return env.evmGetAddressFromPubkey(publicKey);
}

/**
* Get an EVM public key from a signature.
*
* @param hash - Hashed data that was signed.
* @param signature - Expects a SECP256K1 signature in full ETH format.
* Format: (r, s, v) v will be ignored.
* Length: 65 bytes
*
* @returns The EVM public key corresponding to the signature, serialized as a `StaticArray<u8>`.
*/
export function evmGetPubkeyFromSignature(
hash: StaticArray<u8>,
signature: StaticArray<u8>,
): StaticArray<u8> {
return env.evmGetPubkeyFromSignature(hash, signature);
}

/**
* Computes the Keccak256 hash of the given `data`.
*
Expand Down
3 changes: 3 additions & 0 deletions assembly/std/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
* In the file **address.ts** you can find utilities functions to interact further with {@link Address} such as:
* - {@link publicKeyToAddress}.
* - {@link validateAddress}.
* - {@link isAddressEoa}.
* To be moved in the {@link Address} namespace.
*
* In the file **crypto.ts** you can find basic **cryptographic** functions such as:
* - {@link sha256}.
* - {@link isSignatureValid}.
* - {@link keccak256}.
* - {@link isEvmSignatureValid}.
* - {@link evmGetAddressFromPubkey}.
* - {@link evmGetPubkeyFromSignature}.
* - {@link toBase58}.
*
* In the file **events.ts** you can find functions to generate **events** in the blockchain such as:
Expand Down

0 comments on commit dd0e551

Please sign in to comment.