Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSP61 #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions PSPs/drafts/psp-61.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ This proposal aims to define the interface detection standard for WebAssembly sm

## Motivation

Motivation of this proposal is to provide a standard way for smart contracts to detect the interface of the smart contract they are interacting with.
This is useful for smart contracts which interact with other smart contracts, as it allows them to detect the interface of the smart contract they are interacting with and adjust their behavior accordingly,
Motivation of this proposal is to provide a standard way for smart contracts to detect the interface of the smart contract they are interacting with.
This is useful for smart contracts which interact with other smart contracts, as it allows them to detect the interface of the smart contract they are interacting with and adjust their behavior accordingly,
without having any ABI of that contracts.

## Specification

### 1. Interface ID (TRAIT_ID)

Interface ID is a 4-byte identifier of the interface, stored in `u32` big-endian format. It is calculated as the first 4 bytes of the blake2b hash of the string,
that is concatenation of all messages, sorted in lexicographical order. Such as in the example below:
Interface ID is a 4-byte identifier of the interface, stored in `u32` big-endian format. It is calculated as the first 4 bytes of the blake2b hash of the string,
that is concatenation of all message selectors, sorted in lexicographical order. Such as in the example below:

```rust
message_selectors.sort_unstable();

let trait_id = ::ink_ir::Selector::compute(&message_selectors.join("").into_bytes()).into_be_u32();
```

Where `message_selectors` is a vector of all message selectors of the interface in the format `<trait-name>::<message-name>`,
Where `message_selectors` is a vector of all message selectors of the interface in the format `<trait-name>::<message-name>`,
and `::ink_ir::Selector::compute` that computes the BLAKE-2 256-bit based selector from the given input bytes.

```rust
Expand Down Expand Up @@ -136,18 +136,21 @@ use ink::prelude::{
vec::Vec,
};

/// User-defined interfaces (not part of PSP, but provided to show the full implementation
pub trait PSP61Internal {
fn _interfaces(&self) -> Vec<u32> {
vec![]
}
}

/// OB interfaces (not part of PSP, but provided to show the full implementation
pub trait PSP61InternalOB {
varex83 marked this conversation as resolved.
Show resolved Hide resolved
fn _interfaces_ob(&self) -> Vec<u32> {
vec![]
}
}

/// Implementation of PSP61
pub trait PSP61Impl: PSP61Internal + PSP61InternalOB {
fn supports_interface(&self, interface_id: u32) -> bool {
self._interfaces().contains(&interface_id) || self._interfaces_ob().contains(&interface_id)
Expand All @@ -160,7 +163,7 @@ pub trait PSP61Impl: PSP61Internal + PSP61InternalOB {
}
}

/// Macro for implementing PSP61Internal trait
/// Macro for implementing PSP61Internal trait, not part of PSP, provided for full example
#[macro_export]
macro_rules! supported_interfaces {
varex83 marked this conversation as resolved.
Show resolved Hide resolved
($contract:ident => $($interface_id:expr),*) => {
Expand All @@ -176,7 +179,7 @@ macro_rules! supported_interfaces {
}
```

And implementation of this trait is done via `#[openbrush::implementation]` macro:
And implementation of this trait in OB is done via `#[openbrush::implementation]` macro:
```rust
// examples/psp61/lib.rs
#![cfg_attr(not(feature = "std"), no_std, no_main)]
Expand Down