Receipt.go
package model
import (
"encoding/json"
"fmt"
"strings"
"github.com/your/import/path/Hasher"
"github.com/your/import/path/Signer"
)
type Receipt struct {
PreviousBlockhash string `json:"previous_blockhash"`
Beneficiary string `json:"beneficiary"`
SignedQuery map[string]interface{} `json:"signed_query"`
PublicKey string `json:"public_key"`
Signature string `json:"signature"`
Hash string `json:"receipt_hash"`
}
func NewReceipt(initializer map[string]interface{}) *Receipt {
receipt := &Receipt{
PreviousBlockhash: initializer["previous_blockhash"].(string),
Beneficiary: initializer["beneficiary"].(string),
SignedQuery: initializer["signed_query"].(map[string]interface{}),
PublicKey: initializer["public_key"].(string),
Signature: initializer["signature"].(string),
}
receipt.Hash = Hasher.Hash(receipt.ReceiptHeader())
return receipt
}
func (r *Receipt) Obj() map[string]interface{} {
return map[string]interface{}{
"previous_blockhash": r.PreviousBlockhash,
"receipt_hash": r.Hash,
"beneficiary": r.Beneficiary,
"signed_query": r.SignedQuery,
"public_key": r.PublicKey,
"signature": r.Signature,
}
}
func (r *Receipt) JSON() string {
obj := r.Obj()
jsonData, _ := json.Marshal(obj)
return string(jsonData)
}
func (r *Receipt) ReceiptHeader() map[string]interface{} {
return map[string]interface{}{
"previous_blockhash": r.PreviousBlockhash,
"beneficiary": r.Beneficiary,
"signed_query": r.SignedQuery,
}
}
func (r *Receipt) Signer() string {
queryPublicKey, ok := r.SignedQuery["public_key"].(string)
if !ok {
return ""
}
return Signer.Address(queryPublicKey)
}
func (r *Receipt) Validity() bool {
if !r.validateSignedQuery() {
return false
}
if !r.validatePublicKey() {
return false
}
if !Signer.SignatureValidity(Hasher.Hash(r.ReceiptHeader()), r.PublicKey, r.Signature) {
return false
}
query, ok := r.SignedQuery["query"].(map[string]interface{})
if !ok {
return false
}
queryPublicKey, ok := r.SignedQuery["public_key"].(string)
if !ok {
return false
}
querySignature, ok := r.SignedQuery["signature"].(string)
if !ok {
return false
}
if !Signer.SignatureValidity(Hasher.Hash(query), queryPublicKey, querySignature) {
return false
}
queryPreviousBlockhash, ok := query["previous_blockhash"].(string)
if !ok {
return false
}
queryAddress, ok := query["address"].(string)
if !ok {
return false
}
if Signer.Address(r.PublicKey) != queryAddress {
return false
}
if r.PreviousBlockhash != queryPreviousBlockhash {
return false
}
return true
}
func (r *Receipt) validateSignedQuery() bool {
if r.SignedQuery == nil {
return false
}
if _, ok := r.SignedQuery["query"].(map[string]interface{}); !ok {
return false
}
if _, ok := r.SignedQuery["public_key"].(string); !ok {
return false
}
if _, ok := r.SignedQuery["signature"].(string); !ok {
return false
}
return true
}
func (r *Receipt) validatePublicKey() bool {
if r.PublicKey == "" {
return false
}
return true
}1. 패키지 및 임포트 선언
2. Receipt 구조체 정의
3. 초기화 함수
4. 구조체 데이터를 맵으로 변환하는 함수
5. 구조체 데이터를 JSON 형식으로 변환하는 함수
6. 영수증 헤더 데이터를 반환하는 함수
7. 서명자의 주소를 반환하는 함수
8. 영수증의 유효성을 검사하는 함수
9. 서명된 쿼리를 검사하는 함수
10. 공개 키를 검사하는 함수
Last updated