In this guide, we will explore how to generate key pairs, create multi-signature addresses, and use digital signatures to verify the authenticity of messages.
As with any MultiChain project, we begin by initializing a local node.
MultiChain allows you to generate public/private key pairs that are not stored in the node’s wallet. These are useful for creating “cold storage” or for participants who manage their own keys externally.
We will now create a 2-of-3 multi-signature address. This address will be a Pay-to-Script-Hash (P2SH) address that requires two signatures to authorize spending.
# Create the multisig address and add it to the node's wallet
multisig_addr <- mc_add_multisig_address(conn,
n_required = 2,
keys = public_keys)
cat("The new 2-of-3 multisig address is:", multisig_addr, "\n")
# Validate the address to see its properties
info <- mc_validate_address(conn, multisig_addr)
print(info)Even though a multisig address requires multiple signatures, the network treats it as a standard entity for permissions. We can grant it the right to receive and send assets.
Digital signatures are used to prove that a specific message was written by the owner of a private key. This is done without revealing the private key itself.
# 1. Pick one of the generated private keys to sign a message
my_privkey <- key_set$privkey[1]
my_address <- key_set$address[1]
message <- "This is a secure contract signed via R."
# 2. Sign the message
signature <- mc_sign_message(conn, my_privkey, message)
cat("Generated Signature:", signature, "\n")
# 3. Verify the message
# Anyone with the public address and the signature can verify the message
is_valid <- mc_verify_message(conn, my_address, signature, message)
if (is_valid) {
print("The signature is authentic and verified!")
} else {
print("Signature verification failed.")
}Shut down the node and remove the temporary data.
# Stop the node
mc_node_stop(conn)
Sys.sleep(2)
# Determine data directory
if (.Platform$OS.type == "windows") {
base_dir <- file.path(Sys.getenv("APPDATA"), "MultiChain")
} else if (Sys.info()["sysname"] == "Darwin") {
base_dir <- file.path(Sys.getenv("HOME"), "Library/Application Support/MultiChain")
} else {
base_dir <- file.path(Sys.getenv("HOME"), ".multichain")
}
chain_dir <- file.path(base_dir, chain_name)
if (dir.exists(chain_dir)) {
unlink(chain_dir, recursive = TRUE)
}In this vignette, we demonstrated how to:
mc_create_keypairs for external key management.mc_add_multisig_address.mc_validate_address.mc_sign_message and
mc_verify_message.