# Module `0x1::guid` A module for generating globally unique identifiers - [Resource `Generator`](#0x1_guid_Generator) - [Struct `GUID`](#0x1_guid_GUID) - [Struct `ID`](#0x1_guid_ID) - [Resource `CreateCapability`](#0x1_guid_CreateCapability) - [Constants](#@Constants_0) - [Function `gen_create_capability`](#0x1_guid_gen_create_capability) - [Function `create_id`](#0x1_guid_create_id) - [Function `create_with_capability`](#0x1_guid_create_with_capability) - [Function `create`](#0x1_guid_create) - [Function `create_impl`](#0x1_guid_create_impl) - [Function `publish_generator`](#0x1_guid_publish_generator) - [Function `id`](#0x1_guid_id) - [Function `creator_address`](#0x1_guid_creator_address) - [Function `id_creator_address`](#0x1_guid_id_creator_address) - [Function `creation_num`](#0x1_guid_creation_num) - [Function `id_creation_num`](#0x1_guid_id_creation_num) - [Function `eq_id`](#0x1_guid_eq_id) - [Function `get_next_creation_num`](#0x1_guid_get_next_creation_num)
use 0x1::signer;
## Resource `Generator` A generator for new GUIDs.
struct Generator has key
Fields
counter: u64
A monotonically increasing counter
## Struct `GUID` A globally unique identifier derived from the sender's address and a counter
struct GUID has drop, store
Fields
id: guid::ID
## Struct `ID` A non-privileged identifier that can be freely created by anyone. Useful for looking up GUID's.
struct ID has copy, drop, store
Fields
creation_num: u64
If creation_num is i, this is the i+1th GUID created by addr
addr: address
Address that created the GUID
## Resource `CreateCapability` A capability to create a privileged identifier on behalf of the given address
struct CreateCapability has drop, store, key
Fields
addr: address
## Constants GUID generator must be published ahead of first usage of create_with_capability function.
const EGUID_GENERATOR_NOT_PUBLISHED: u64 = 0;
## Function `gen_create_capability` Generates a capability to create the privileged GUID on behalf of the signer
public fun gen_create_capability(account: &signer): guid::CreateCapability
Implementation
public fun gen_create_capability(account: &signer): CreateCapability {
    let addr = signer::address_of(account);
    if (!exists<Generator>(addr)) {
        move_to(account, Generator { counter: 0 })
    };
    CreateCapability { addr }
}
## Function `create_id` Create a non-privileged id from addr and creation_num
public fun create_id(addr: address, creation_num: u64): guid::ID
Implementation
public fun create_id(addr: address, creation_num: u64): ID {
    ID { creation_num, addr }
}
## Function `create_with_capability`
public fun create_with_capability(addr: address, _cap: &guid::CreateCapability): guid::GUID
Implementation
public fun create_with_capability(addr: address, _cap: &CreateCapability): GUID acquires Generator {
    assert!(exists<Generator>(addr), EGUID_GENERATOR_NOT_PUBLISHED);
    create_impl(addr)
}
## Function `create` Create and return a new GUID. Creates a Generator under account if it does not already have one
public fun create(account: &signer): guid::GUID
Implementation
public fun create(account: &signer): GUID acquires Generator {
    let addr = signer::address_of(account);
    if (!exists<Generator>(addr)) {
        move_to(account, Generator { counter: 0 })
    };
    create_impl(addr)
}
## Function `create_impl`
fun create_impl(addr: address): guid::GUID
Implementation
fun create_impl(addr: address): GUID acquires Generator {
    let generator = borrow_global_mut<Generator>(addr);
    let creation_num = generator.counter;
    generator.counter = creation_num + 1;
    GUID { id: ID { creation_num, addr } }
}
## Function `publish_generator` Publish a Generator resource under account
public fun publish_generator(account: &signer)
Implementation
public fun publish_generator(account: &signer) {
    move_to(account, Generator { counter: 0 })
}
## Function `id` Get the non-privileged ID associated with a GUID
public fun id(guid: &guid::GUID): guid::ID
Implementation
public fun id(guid: &GUID): ID {
    *&guid.id
}
## Function `creator_address` Return the account address that created the GUID
public fun creator_address(guid: &guid::GUID): address
Implementation
public fun creator_address(guid: &GUID): address {
    guid.id.addr
}
## Function `id_creator_address` Return the account address that created the guid::ID
public fun id_creator_address(id: &guid::ID): address
Implementation
public fun id_creator_address(id: &ID): address {
    id.addr
}
## Function `creation_num` Return the creation number associated with the GUID
public fun creation_num(guid: &guid::GUID): u64
Implementation
public fun creation_num(guid: &GUID): u64 {
    guid.id.creation_num
}
## Function `id_creation_num` Return the creation number associated with the guid::ID
public fun id_creation_num(id: &guid::ID): u64
Implementation
public fun id_creation_num(id: &ID): u64 {
    id.creation_num
}
## Function `eq_id` Return true if the GUID's ID is id
public fun eq_id(guid: &guid::GUID, id: &guid::ID): bool
Implementation
public fun eq_id(guid: &GUID, id: &ID): bool {
    &guid.id == id
}
## Function `get_next_creation_num` Return the number of the next GUID to be created by addr
public fun get_next_creation_num(addr: address): u64
Implementation
public fun get_next_creation_num(addr: address): u64 acquires Generator {
    if (!exists<Generator>(addr)) {
        0
    } else {
        borrow_global<Generator>(addr).counter
    }
}