github.com/quantosnetwork/Quantos@v0.0.0-20220306172517-e20b28c5a29a/address/qbitAddress_deprecated.go (about)

     1  package address
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/base32"
     6  	"encoding/binary"
     7  	"encoding/json"
     8  	"github.com/davecgh/go-spew/spew"
     9  	"github.com/iancoleman/strcase"
    10  	"github.com/zeebo/blake3"
    11  	"lukechampine.com/frand"
    12  	"strings"
    13  	"unsafe"
    14  )
    15  
    16  /*
    17  
    18  	QBIT Addresses
    19  
    20  	Purpose: Wallet addresses
    21  	Size: 36 bytes
    22  	Prefix: 0x (0x = byte representation)
    23  
    24  	Structure of Address
    25  
    26  	[All Qbit address starts with]
    27      [4]byte  uint32 [ 0x38A ]
    28  	[4]byte crc32 IEEE of kyber public key generated at the same time as the p2p peer ID
    29  	[20]byte of cryptographically safe random bytes data
    30  	[8]byte as a random nonce to avoid addresses collisions
    31  
    32  	0x is then appended to the address to mark it as bytes
    33  
    34  	@Suggestion we could but qbit: [address here] to differentiate from other 0x address types
    35  
    36  	906 / 38A = cointype
    37  
    38  */
    39  
    40  func SliceToArray32(bytes []byte) *[32]uint8 { return (*[32]uint8)(unsafe.Pointer(&bytes[0])) }
    41  func SliceToArray64(bytes []byte) *[64]uint8 { return (*[64]uint8)(unsafe.Pointer(&bytes[0])) }
    42  
    43  type QBITAddress struct {
    44  	seed            []byte
    45  	words           *[16]uint32
    46  	network         [2]byte
    47  	protocolVersion [2]byte
    48  	prefix          uint32 // uint32(906)
    49  	checksumIEEE    uint32
    50  	context         uint32 // blake3 context of the address
    51  	Signature       []byte
    52  }
    53  
    54  var ZEROADDRESS string
    55  var ZBYTES [32]byte
    56  
    57  func GenerateNewQbitAddress(networkID [2]byte, version [2]byte, prefix uint32, context uint32) *QBITAddress {
    58  
    59  	var addr QBITAddress
    60  	add := NewQBITAddress(networkID, version, prefix, context, &addr)
    61  	return add
    62  
    63  }
    64  
    65  func (q *QBITAddress) Hash() []byte {
    66  	qs, _ := json.Marshal(q.seed)
    67  	hash := blake3.Sum256(qs)
    68  	return hash[:]
    69  
    70  }
    71  
    72  func (q *QBITAddress) String() string {
    73  	h := q.Hash()
    74  	str := base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(h)
    75  	str = Reverse(str)
    76  	str = strings.ToLower(str)
    77  	str = strcase.ToCamel(str)
    78  	str = Add0xPrefix(str)
    79  	return str
    80  }
    81  
    82  func QBITAddressFromAddressString(str string) string {
    83  	strsplit := strings.Split(str, "0x")
    84  	revAddrStr := strsplit[0]
    85  	addrStr := Reverse(revAddrStr)
    86  	return addrStr
    87  
    88  }
    89  
    90  func Add0xPrefix(addr string) string {
    91  	return "0x" + addr
    92  }
    93  
    94  func NewQBITAddress(networkID [2]byte, version [2]byte, prefix uint32, context uint32, addr *QBITAddress) *QBITAddress {
    95  
    96  	q := addr
    97  	q.network = networkID
    98  	q.protocolVersion = version
    99  	q.prefix = prefix
   100  	q.context = context
   101  	seed1 := generateRandomBytes()
   102  	seed2 := generateRandomBytes()
   103  	seed := make([][]byte, 2)
   104  	seed[0] = seed1[:]
   105  	seed[1] = seed2[:]
   106  	theSeed := bytes.Join(seed, nil)
   107  	q.seed = theSeed
   108  	q.words = new([16]uint32)
   109  
   110  	return q
   111  }
   112  
   113  func getZeroAddress(netID [2]byte, version [2]byte, prefix uint32, context uint32) *QBITAddress {
   114  	q := GenerateNewQbitAddress(netID, version, prefix, context)
   115  
   116  	buf := make([]byte, 64)
   117  	b := fillBufferWithZeros(buf)
   118  	spew.Dump(b[:])
   119  	q.seed = b[:]
   120  	q.context = context
   121  
   122  	return q
   123  
   124  }
   125  
   126  func ZeroAddress(netID [2]byte, version [2]byte, prefix uint32, context uint32) string {
   127  	addr := getZeroAddress(netID, version, prefix, context)
   128  	return addr.String()
   129  }
   130  
   131  func fillBufferWithZeros(buf []byte) []byte {
   132  	for i, _ := range buf {
   133  		buf[i] = 0
   134  	}
   135  	return buf
   136  }
   137  
   138  func generateRandomBytes() [32]byte {
   139  	b := frand.Entropy256()
   140  	frand.Read(b[:])
   141  	return b
   142  
   143  }
   144  
   145  func BytesToWords(bytes *[64]uint8, words *[16]uint32) {
   146  	words[0] = binary.LittleEndian.Uint32(bytes[0*4:])
   147  	words[1] = binary.LittleEndian.Uint32(bytes[1*4:])
   148  	words[2] = binary.LittleEndian.Uint32(bytes[2*4:])
   149  	words[3] = binary.LittleEndian.Uint32(bytes[3*4:])
   150  	words[4] = binary.LittleEndian.Uint32(bytes[4*4:])
   151  	words[5] = binary.LittleEndian.Uint32(bytes[5*4:])
   152  	words[6] = binary.LittleEndian.Uint32(bytes[6*4:])
   153  	words[7] = binary.LittleEndian.Uint32(bytes[7*4:])
   154  	words[8] = binary.LittleEndian.Uint32(bytes[8*4:])
   155  	words[9] = binary.LittleEndian.Uint32(bytes[9*4:])
   156  	words[10] = binary.LittleEndian.Uint32(bytes[10*4:])
   157  	words[11] = binary.LittleEndian.Uint32(bytes[11*4:])
   158  	words[12] = binary.LittleEndian.Uint32(bytes[12*4:])
   159  	words[13] = binary.LittleEndian.Uint32(bytes[13*4:])
   160  	words[14] = binary.LittleEndian.Uint32(bytes[14*4:])
   161  	words[15] = binary.LittleEndian.Uint32(bytes[15*4:])
   162  
   163  }
   164  
   165  func WordsToBytes(words *[16]uint32, bytes []byte) {
   166  	bytes = bytes[:64]
   167  	binary.LittleEndian.PutUint32(bytes[0*4:1*4], words[0])
   168  	binary.LittleEndian.PutUint32(bytes[1*4:2*4], words[1])
   169  	binary.LittleEndian.PutUint32(bytes[2*4:3*4], words[2])
   170  	binary.LittleEndian.PutUint32(bytes[3*4:4*4], words[3])
   171  	binary.LittleEndian.PutUint32(bytes[4*4:5*4], words[4])
   172  	binary.LittleEndian.PutUint32(bytes[5*4:6*4], words[5])
   173  	binary.LittleEndian.PutUint32(bytes[6*4:7*4], words[6])
   174  	binary.LittleEndian.PutUint32(bytes[7*4:8*4], words[7])
   175  	binary.LittleEndian.PutUint32(bytes[8*4:9*4], words[8])
   176  	binary.LittleEndian.PutUint32(bytes[9*4:10*4], words[9])
   177  	binary.LittleEndian.PutUint32(bytes[10*4:11*4], words[10])
   178  	binary.LittleEndian.PutUint32(bytes[11*4:12*4], words[11])
   179  	binary.LittleEndian.PutUint32(bytes[12*4:13*4], words[12])
   180  	binary.LittleEndian.PutUint32(bytes[13*4:14*4], words[13])
   181  	binary.LittleEndian.PutUint32(bytes[14*4:15*4], words[14])
   182  	binary.LittleEndian.PutUint32(bytes[15*4:16*4], words[15])
   183  }
   184  
   185  func KeyFromBytes(key []byte, out *[8]uint32) {
   186  	key = key[:32]
   187  	out[0] = binary.LittleEndian.Uint32(key[0:])
   188  	out[1] = binary.LittleEndian.Uint32(key[4:])
   189  	out[2] = binary.LittleEndian.Uint32(key[8:])
   190  	out[3] = binary.LittleEndian.Uint32(key[12:])
   191  	out[4] = binary.LittleEndian.Uint32(key[16:])
   192  	out[5] = binary.LittleEndian.Uint32(key[20:])
   193  	out[6] = binary.LittleEndian.Uint32(key[24:])
   194  	out[7] = binary.LittleEndian.Uint32(key[28:])
   195  }
   196  
   197  func Reverse(s string) string {
   198  	r := []rune(s)
   199  	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
   200  		r[i], r[j] = r[j], r[i]
   201  	}
   202  	return string(r)
   203  }