axelar-cgp-sui

Module 0xa1::weighted_signer

use 0x2::address;
use 0x2::bcs;

Struct WeightedSigner

struct WeightedSigner has copy, drop, store
Fields
pub_key: vector<u8>
weight: u128

Constants

#[error]
const EInvalidPubKeyLength: vector<u8> = b"invalid public key length: expected 33 bytes";

#[error]
const EInvalidWeight: vector<u8> = b"invalid weight: expected non-zero value";

Length of a public key

const PUB_KEY_LENGTH: u64 = 33;

Function pub_key

public(friend) fun pub_key(self: &weighted_signer::WeightedSigner): vector<u8>
Implementation
public(package) fun pub_key(self: &WeightedSigner): vector<u8> {
    self.pub_key
}

Function weight

public(friend) fun weight(self: &weighted_signer::WeightedSigner): u128
Implementation
public(package) fun weight(self: &WeightedSigner): u128 {
    self.weight
}

Function new

public(friend) fun new(pub_key: vector<u8>, weight: u128): weighted_signer::WeightedSigner
Implementation
public(package) fun new(pub_key: vector<u8>, weight: u128): WeightedSigner {
    assert!(pub_key.length() == PUB_KEY_LENGTH, EInvalidPubKeyLength);

    WeightedSigner { pub_key, weight }
}

Function default

Empty weighted signer

public(friend) fun default(): weighted_signer::WeightedSigner
Implementation
public(package) fun default(): WeightedSigner {
    let mut pub_key = @0x0.to_bytes();
    pub_key.push_back(0);

    WeightedSigner {
        pub_key,
        weight: 0,
    }
}

Function peel

public(friend) fun peel(bcs: &mut bcs::BCS): weighted_signer::WeightedSigner
Implementation
public(package) fun peel(bcs: &mut BCS): WeightedSigner {
    let pub_key = bcs.peel_vec_u8();
    let weight = bcs.peel_u128();

    new(pub_key, weight)
}

Function validate

public(friend) fun validate(self: &weighted_signer::WeightedSigner)
Implementation
public(package) fun validate(self: &WeightedSigner) {
    assert!(self.weight != 0, EInvalidWeight);
}

Function lt

Check if self.signer is less than other.signer as bytes

public(friend) fun lt(self: &weighted_signer::WeightedSigner, other: &weighted_signer::WeightedSigner): bool
Implementation
public(package) fun lt(self: &WeightedSigner, other: &WeightedSigner): bool {
    let mut i = 0;

    while (i < PUB_KEY_LENGTH) {
        if (self.pub_key[i] < other.pub_key[i]) {
            return true
        } else if (self.pub_key[i] > other.pub_key[i]) {
            return false
        };

        i = i + 1;
    };

    false
}