axelar-cgp-sui

Module 0xa1::weighted_signers

use 0x1::vector;
use 0x2::bcs;
use 0x2::hash;
use 0xa1::bytes32;
use 0xa1::weighted_signer;

Struct WeightedSigners

struct WeightedSigners has copy, drop, store
Fields
signers: vector<weighted_signer::WeightedSigner>
threshold: u128
nonce: bytes32::Bytes32

Constants

const EInvalidSignerOrder: vector<u8> = [105, 110, 118, 97, 108, 105, 100, 32, 115, 105, 103, 110, 101, 114, 32, 111, 114, 100, 101, 114, 58, 32, 115, 105, 103, 110, 101, 114, 115, 32, 109, 117, 115, 116, 32, 98, 101, 32, 105, 110, 32, 97, 115, 99, 101, 110, 100, 105, 110, 103, 32, 111, 114, 100, 101, 114, 32, 98, 121, 32, 116, 104, 101, 105, 114, 32, 112, 117, 98, 108, 105, 99, 32, 107, 101, 121];


Errors ——

const EInvalidSignersLength: vector<u8> = [105, 110, 118, 97, 108, 105, 100, 32, 115, 105, 103, 110, 101, 114, 115, 32, 108, 101, 110, 103, 116, 104, 58, 32, 101, 120, 112, 101, 99, 116, 101, 100, 32, 97, 116, 32, 108, 101, 97, 115, 116, 32, 49, 32, 115, 105, 103, 110, 101, 114];

const EInvalidThreshold: vector<u8> = [105, 110, 118, 97, 108, 105, 100, 32, 116, 104, 114, 101, 115, 104, 111, 108, 100, 58, 32, 101, 120, 112, 101, 99, 116, 101, 100, 32, 110, 111, 110, 45, 122, 101, 114, 111, 32, 118, 97, 108, 117, 101, 32, 97, 110, 100, 32, 108, 101, 115, 115, 32, 116, 104, 97, 110, 32, 111, 114, 32, 101, 113, 117, 97, 108, 32, 116, 111, 32, 116, 104, 101, 32, 116, 111, 116, 97, 108, 32, 119, 101, 105, 103, 104, 116, 32, 111, 102, 32, 116, 104, 101, 32, 115, 105, 103, 110, 101, 114, 115];

Function peel


Package Functions —————– Decode a WeightedSigners from the BCS encoded bytes.

public(friend) fun peel(bcs: &mut bcs::BCS): weighted_signers::WeightedSigners
Implementation
public(package) fun peel(bcs: &mut BCS): WeightedSigners {
    let len = bcs.peel_vec_length();
    assert!(len > 0, EInvalidSignersLength);

    WeightedSigners {
        signers: vector::tabulate!(len, |_| weighted_signer::peel(bcs)),
        threshold: bcs.peel_u128(),
        nonce: bytes32::peel(bcs),
    }
}

Function validate

Validates the weighted signers. The following must be true:

  1. The signers are in ascending order by their public key.
  2. The threshold is greater than zero.
  3. The threshold is less than or equal to the total weight of the signers.
public(friend) fun validate(self: &weighted_signers::WeightedSigners)
Implementation
public(package) fun validate(self: &WeightedSigners) {
    self.validate_signers();
    self.validate_threshold();
}

Function hash

public(friend) fun hash(self: &weighted_signers::WeightedSigners): bytes32::Bytes32
Implementation
public(package) fun hash(self: &WeightedSigners): Bytes32 {
    bytes32::from_bytes(hash::keccak256(&bcs::to_bytes(self)))
}

Function signers

public(friend) fun signers(self: &weighted_signers::WeightedSigners): &vector<weighted_signer::WeightedSigner>
Implementation
public(package) fun signers(self: &WeightedSigners): &vector<WeightedSigner> {
    &self.signers
}

Function threshold

public(friend) fun threshold(self: &weighted_signers::WeightedSigners): u128
Implementation
public(package) fun threshold(self: &WeightedSigners): u128 {
    self.threshold
}

Function nonce

public(friend) fun nonce(self: &weighted_signers::WeightedSigners): bytes32::Bytes32
Implementation
public(package) fun nonce(self: &WeightedSigners): Bytes32 {
    self.nonce
}

Function validate_signers


Internal Functions —– Validates the order of the signers and the length of the signers. The signers must be in ascending order by their public key. Otherwise, the error EInvalidSignersLength is raised.

fun validate_signers(self: &weighted_signers::WeightedSigners)
Implementation
fun validate_signers(self: &WeightedSigners) {
    assert!(!self.signers.is_empty(), EInvalidSignersLength);
    let mut previous = &weighted_signer::default();
    self.signers.do_ref!(
        |signer| {
            signer.validate();
            assert!(previous.lt(signer), EInvalidSignerOrder);
            previous = signer;
        },
    );
}

Function total_weight

Calculates the total weight of the signers.

fun total_weight(self: &weighted_signers::WeightedSigners): u128
Implementation
fun total_weight(self: &WeightedSigners): u128 {
    self.signers.fold!<WeightedSigner, u128>(
        0,
        |acc, signer| acc + signer.weight(),
    )
}

Function validate_threshold

Validates the threshold. The threshold must be greater than zero and less than or equal to the total weight of the signers. Otherwise, the error EInvalidThreshold is raised.

fun validate_threshold(self: &weighted_signers::WeightedSigners)
Implementation
fun validate_threshold(self: &WeightedSigners) {
    assert!(self.threshold != 0 && self.total_weight() >= self.threshold, EInvalidThreshold);
}