bitcoinimage

package module
v0.0.0-...-7e4d58a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 31, 2025 License: MIT Imports: 11 Imported by: 0

README

go-bitcoin-image

A Go implementation of bitcoin-backup, providing password-based encryption for Bitcoin wallet backups compatible with the bitcoin-backup TypeScript library.

Features

  • Full compatibility with TypeScript bitcoin-backup format
  • AES-256-GCM encryption with PBKDF2 key derivation
  • Support for multiple backup types (BAP Master, BAP Member, WIF, 1Sat)
  • Automatic iteration fallback (600k/100k) for legacy support
  • Type-safe Go implementation

Installation

go get github.com/b-open-io/go-bitcoin-image

Usage

Encrypting a Backup
package main

import (
    "fmt"
    "log"
    bitcoinimage "github.com/b-open-io/go-bitcoin-image"
)

func main() {
    // Create a BAP member backup
    backup := bitcoinimage.BapMemberBackup{
        WIF:   "L1aW4aubDFB7yfras2S1mN3bqg9nwySY8nkoLmJebSLD5BWv3ENZ",
        ID:    "bap-id-123",
        Label: "My Identity",
    }
    
    // Encrypt with password
    result, err := bitcoinimage.EncryptBackup(backup, "my-secure-password")
    if err != nil {
        log.Fatal(err)
    }
    
    fmt.Println("Encrypted backup:", result.Encrypted)
}
Decrypting a Backup
// Decrypt to map
decrypted, err := bitcoinimage.DecryptBackup(encryptedString, "my-secure-password")
if err != nil {
    log.Fatal(err)
}

// Or decrypt to specific type
member, err := bitcoinimage.DecryptToStruct[bitcoinimage.BapMemberBackup](encryptedString, "my-secure-password")
if err != nil {
    log.Fatal(err)
}

Backup Types

  • BapMasterBackup: HD wallet master key with mnemonic
  • MasterBackupType42: Type 42 derivation master backup
  • BapMemberBackup: Individual BAP identity
  • WifBackup: Simple WIF private key
  • OneSatBackup: 1Sat ordinals wallet (3 keys)

Compatibility

This library maintains full compatibility with the TypeScript bitcoin-backup library, ensuring seamless interoperability between JavaScript/TypeScript and Go applications.

License

MIT License

Documentation

Index

Constants

View Source
const (
	RECOMMENDED_ITERATIONS = 600000 // Current recommended PBKDF2 iterations
	LEGACY_ITERATIONS      = 100000 // Legacy iteration count for backwards compatibility
	SALT_LENGTH            = 16     // Random salt length in bytes
	IV_LENGTH              = 12     // GCM IV/nonce length in bytes
	KEY_LENGTH             = 32     // AES-256 key length in bytes
	TAG_LENGTH             = 16     // GCM authentication tag length
)

Encryption constants matching TypeScript bitcoin-backup library

Variables

This section is empty.

Functions

func DecryptBackup

func DecryptBackup(encrypted string, passphrase string, attemptIterations ...int) (map[string]interface{}, error)

DecryptBackup decrypts an encrypted backup with a passphrase

func DecryptToStruct

func DecryptToStruct[T any](encrypted string, passphrase string, attemptIterations ...int) (*T, error)

DecryptToStruct decrypts directly into a typed struct

Types

type BackupType

type BackupType string

BackupType represents the type of backup

const (
	BackupTypeBapMaster BackupType = "bap-master"
	BackupTypeBapMember BackupType = "bap-member"
	BackupTypeWif       BackupType = "wif"
	BackupTypeOneSat    BackupType = "onesat"
)

func DetectBackupFormat

func DetectBackupFormat(content string) (BackupType, bool, error)

DetectBackupFormat attempts to determine the format of a backup string

type BapMasterBackup

type BapMasterBackup struct {
	IDs       string `json:"ids"`                 // Encrypted BAP data
	Xprv      string `json:"xprv"`                // Extended private key
	Mnemonic  string `json:"mnemonic"`            // BIP39 mnemonic phrase
	Label     string `json:"label,omitempty"`     // Optional label
	CreatedAt string `json:"createdAt,omitempty"` // ISO 8601 timestamp
}

BapMasterBackup represents a BAP master key backup (mnemonic-based)

type BapMemberBackup

type BapMemberBackup struct {
	WIF       string `json:"wif"`                 // Private key in WIF format
	ID        string `json:"id"`                  // BAP ID
	Label     string `json:"label,omitempty"`     // Optional label
	CreatedAt string `json:"createdAt,omitempty"` // ISO 8601 timestamp
}

BapMemberBackup represents a BAP member identity backup

func CreateBapMemberBackup

func CreateBapMemberBackup(wif, id, label string) *BapMemberBackup

CreateBapMemberBackup creates a new BAP member backup

type EncryptionResult

type EncryptionResult struct {
	Encrypted  string // Base64 encoded encrypted data
	Iterations int    // PBKDF2 iterations used
}

EncryptionResult represents the result of encryption

func EncryptBackup

func EncryptBackup(data interface{}, passphrase string, iterations ...int) (*EncryptionResult, error)

EncryptBackup encrypts any backup type with a passphrase

type MasterBackupType42

type MasterBackupType42 struct {
	IDs       string `json:"ids"`                 // Encrypted BAP data
	MasterKey string `json:"masterKey"`           // Master key in WIF format
	KeyName   string `json:"keyName"`             // Key identifier for Type 42
	Label     string `json:"label,omitempty"`     // Optional label
	CreatedAt string `json:"createdAt,omitempty"` // ISO 8601 timestamp
}

MasterBackupType42 represents a Type 42 derivation master backup

type OneSatBackup

type OneSatBackup struct {
	OrdPk      string `json:"ordPk"`               // Ordinals private key
	PayPk      string `json:"payPk"`               // Payment private key
	IdentityPk string `json:"identityPk"`          // Identity private key
	Label      string `json:"label,omitempty"`     // Optional label
	CreatedAt  string `json:"createdAt,omitempty"` // ISO 8601 timestamp
}

OneSatBackup represents a 1Sat ordinals wallet backup

func CreateOneSatBackup

func CreateOneSatBackup(ordPk, payPk, identityPk, label string) *OneSatBackup

CreateOneSatBackup creates a new 1Sat ordinals backup

type WifBackup

type WifBackup struct {
	WIF       string `json:"wif"`                 // Private key in WIF format
	Label     string `json:"label,omitempty"`     // Optional label
	CreatedAt string `json:"createdAt,omitempty"` // ISO 8601 timestamp
}

WifBackup represents a simple WIF key backup

func CreateWifBackup

func CreateWifBackup(wif, label string) *WifBackup

CreateWifBackup creates a new WIF backup

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL