0xa1::weighted_signers
WeightedSigners
peel
validate
hash
signers
threshold
nonce
validate_signers
total_weight
validate_threshold
use 0x1::vector;
use 0x2::bcs;
use 0x2::hash;
use 0xa1::bytes32;
use 0xa1::weighted_signer;
WeightedSigners
struct WeightedSigners has copy, drop, store
signers: vector<weighted_signer::WeightedSigner>
threshold: u128
nonce: bytes32::Bytes32
#[error]
const EInvalidSignerOrder: vector<u8> = b"invalid signer order: signers must be in ascending order by their public key";
Errors ——
#[error]
const EInvalidSignersLength: vector<u8> = b"invalid signers length: expected at least 1 signer";
#[error]
const EInvalidThreshold: vector<u8> = b"invalid threshold: expected non-zero value and less than or equal to the total weight of the signers";
peel
Package Functions
—————–
Decode a WeightedSigners
from the BCS encoded bytes.
public(friend) fun peel(bcs: &mut bcs::BCS): weighted_signers::WeightedSigners
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),
}
}
validate
Validates the weighted signers. The following must be true:
public(friend) fun validate(self: &weighted_signers::WeightedSigners)
public(package) fun validate(self: &WeightedSigners) {
self.validate_signers();
self.validate_threshold();
}
hash
public(friend) fun hash(self: &weighted_signers::WeightedSigners): bytes32::Bytes32
public(package) fun hash(self: &WeightedSigners): Bytes32 {
bytes32::from_bytes(hash::keccak256(&bcs::to_bytes(self)))
}
signers
public(friend) fun signers(self: &weighted_signers::WeightedSigners): &vector<weighted_signer::WeightedSigner>
public(package) fun signers(self: &WeightedSigners): &vector<WeightedSigner> {
&self.signers
}
threshold
public(friend) fun threshold(self: &weighted_signers::WeightedSigners): u128
public(package) fun threshold(self: &WeightedSigners): u128 {
self.threshold
}
nonce
public(friend) fun nonce(self: &weighted_signers::WeightedSigners): bytes32::Bytes32
public(package) fun nonce(self: &WeightedSigners): Bytes32 {
self.nonce
}
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)
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;
});
}
total_weight
Calculates the total weight of the signers.
fun total_weight(self: &weighted_signers::WeightedSigners): u128
fun total_weight(self: &WeightedSigners): u128 {
self
.signers
.fold!<WeightedSigner, u128>(0, |acc, signer| acc + signer.weight())
}
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)
fun validate_threshold(self: &WeightedSigners) {
assert!(
self.threshold != 0 && self.total_weight() >= self.threshold,
EInvalidThreshold,
);
}