Skip to content

chi-okeke/Conflict-Free-Diamond-Verification

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

Conflict-Free Diamond Verification Smart Contract

A comprehensive Clarity smart contract for the Stacks blockchain that enables transparent tracking and verification of conflict-free diamonds throughout their entire supply chain.

Overview

This smart contract provides a decentralized system for registering, verifying, and tracking the ownership chain of diamonds, ensuring transparency and authenticity in the diamond trade. It helps combat conflict diamonds by maintaining an immutable record of diamond provenance.

Features

🔐 Authorization System

  • Contract Owner: Full administrative control
  • Authorized Verifiers: Certified entities that can verify diamond authenticity
  • Multi-level Permission Controls: Role-based access for secure operations

💎 Diamond Registration

  • Register diamonds with unique identifiers
  • Record essential details: origin, carat weight, certification number, mining date
  • Automatic timestamp tracking
  • Status management (pending/verified/rejected)

✅ Verification Process

  • Only authorized verifiers can approve/reject diamonds
  • Prevents duplicate verification
  • Immutable verification records
  • Verifier attribution for accountability

📜 Custody Chain Tracking

  • Complete transfer history for each diamond
  • Transfer notes and timestamps
  • Only verified diamonds can be transferred
  • Prevents fraudulent ownership claims

🛡️ Security Features

  • Comprehensive input validation on all user inputs
  • 9 distinct error codes for precise error handling
  • Prevention of self-transfers
  • Immutable verification status once approved
  • Owner-only update permissions before verification

Data Structure

Diamond Record

{
  owner: principal,
  origin: string (max 100 chars),
  carat-weight: uint,
  certification-number: string (max 64 chars),
  mining-date: uint (block height),
  is-verified: bool,
  verifier: optional principal,
  status: string ("pending", "verified", "rejected"),
  created-at: uint (block height)
}

Custody Chain Record

{
  from-owner: principal,
  to-owner: principal,
  transfer-date: uint (block height),
  notes: string (max 256 chars)
}

Functions

Admin Functions

add-verifier

(add-verifier (verifier principal))

Adds an authorized verifier. Only callable by contract owner.

remove-verifier

(remove-verifier (verifier principal))

Removes an authorized verifier. Only callable by contract owner.

Public Functions

register-diamond

(register-diamond 
  (diamond-id (string-ascii 64))
  (origin (string-ascii 100))
  (carat-weight uint)
  (cert-number (string-ascii 64))
  (mining-date uint))

Registers a new diamond in the system. The caller becomes the initial owner.

Validations:

  • Diamond ID must be unique and non-empty
  • Origin must be non-empty
  • Carat weight must be between 1-100,000
  • Mining date must be valid and not in the future
  • Certification number must be non-empty

verify-diamond

(verify-diamond (diamond-id (string-ascii 64)) (approved bool))

Verifies or rejects a diamond. Only callable by authorized verifiers.

Requirements:

  • Caller must be an authorized verifier
  • Diamond must not already be verified

transfer-diamond

(transfer-diamond 
  (diamond-id (string-ascii 64))
  (new-owner principal)
  (notes (string-ascii 256)))

Transfers diamond ownership to a new owner.

Requirements:

  • Caller must be the current owner
  • Diamond must be verified
  • Cannot transfer to self
  • Creates an immutable custody chain record

update-diamond-origin

(update-diamond-origin 
  (diamond-id (string-ascii 64))
  (new-origin (string-ascii 100)))

Updates the origin information of a diamond.

Requirements:

  • Caller must be the owner
  • Diamond must not be verified yet

Read-Only Functions

get-diamond-info

Returns complete diamond information including verification status and ownership.

get-diamond-owner

Returns the current owner of a specific diamond.

get-custody-chain

Returns a specific transfer record from the custody chain.

get-transfer-count

Returns the total number of transfers for a diamond.

is-diamond-verified

Checks if a diamond has been verified.

get-diamond-status

Returns the current status ("pending", "verified", or "rejected").

get-diamond-full-details

Returns comprehensive information including diamond data and transfer count.

check-verifier-status

Checks if a principal is an authorized verifier.

get-verifier-count

Returns the total number of authorized verifiers.

Error Codes

Code Constant Description
u100 err-owner-only Action requires contract owner privileges
u101 err-not-found Diamond or record not found
u102 err-already-exists Diamond ID or verifier already exists
u103 err-unauthorized Caller not authorized for this action
u104 err-invalid-status Invalid diamond status
u105 err-not-authorized-verifier Caller is not an authorized verifier
u106 err-already-verified Diamond already verified
u107 err-invalid-transfer Transfer conditions not met
u108 err-invalid-input Input validation failed

Usage Example

1. Deploy Contract

Deploy the contract to Stacks blockchain. The deployer becomes the contract owner.

2. Add Verifiers

(contract-call? .diamond-verification add-verifier 'SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7)

3. Register a Diamond

(contract-call? .diamond-verification register-diamond 
  "DIA-001-2024-KIM"
  "Kimberley, South Africa"
  u150
  "GIA-2157893456"
  u800000)

4. Verify Diamond

(contract-call? .diamond-verification verify-diamond "DIA-001-2024-KIM" true)

5. Transfer Diamond

(contract-call? .diamond-verification transfer-diamond 
  "DIA-001-2024-KIM"
  'SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7
  "Sale to authorized dealer - Invoice #12345")

6. Query Diamond Info

(contract-call? .diamond-verification get-diamond-full-details "DIA-001-2024-KIM")

Security Considerations

  1. Input Validation: All user inputs are validated before processing
  2. Access Control: Role-based permissions prevent unauthorized actions
  3. Immutability: Verified diamonds cannot have their verification status changed
  4. Transparency: All transfers are recorded in an immutable custody chain
  5. Prevention of Fraud: Self-transfers and unverified diamond transfers are blocked

Development

Language: Clarity
Blockchain: Stacks
Lines of Code: 287
Dependencies: None (uses only Clarity built-in functions)

Testing Checklist

  • Deploy contract successfully
  • Add and remove verifiers (owner only)
  • Register diamonds with valid data
  • Reject invalid diamond registrations
  • Verify diamonds (verifier only)
  • Transfer verified diamonds
  • Prevent transfers of unverified diamonds
  • Query custody chain history
  • Update origin before verification
  • Prevent updates after verification

About

The Conflict-Free Diamond Verification Smart Contract is a production-ready Clarity-based solution deployed on the Stacks blockchain that provides an immutable, transparent system for tracking and verifying the authenticity and ethical sourcing of diamonds throughout their entire supply chain.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors