Factory.go
package rpc
import (
"time"
"saseul/model"
"util/signer"
)
// Factory๋ ํธ๋์ญ์
๊ณผ ์์ฒญ์ ์์ฑํ๋ ๊ตฌ์กฐ์ฒด์
๋๋ค.
type Factory struct{}
// Transaction ํจ์๋ ์ฃผ์ด์ง ํ์
๊ณผ ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ์๋ช
๋ ํธ๋์ญ์
์ ์์ฑํฉ๋๋ค.
func (f *Factory) Transaction(txType string, vars map[string]interface{}, privateKey *string) *model.SignedTransaction {
if privateKey == nil {
key := signer.GeneratePrivateKey()
privateKey = &key
}
transaction := f.Data(txType, *privateKey, nil)
for key, value := range vars {
transaction[key] = value
}
signedTx := &model.SignedTransaction{
Transaction: transaction,
PublicKey: signer.PublicKey(*privateKey),
}
signedTx.Signature = signer.Signature(signedTx.Hash(), *privateKey)
return signedTx
}
// Request ํจ์๋ ์ฃผ์ด์ง ํ์
๊ณผ ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ์๋ช
๋ ์์ฒญ์ ์์ฑํฉ๋๋ค.
func (f *Factory) Request(reqType string, vars map[string]interface{}, privateKey *string) *model.SignedRequest {
if privateKey == nil {
key := signer.GeneratePrivateKey()
privateKey = &key
}
request := f.Data(reqType, *privateKey, nil)
for key, value := range vars {
request[key] = value
}
signedReq := &model.SignedRequest{
Request: request,
PublicKey: signer.PublicKey(*privateKey),
}
signedReq.Signature = signer.Signature(signedReq.Hash(), *privateKey)
return signedReq
}
// Data ํจ์๋ ์ฃผ์ด์ง ํ์
๊ณผ ํค๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์์ฑํฉ๋๋ค.
func (f *Factory) Data(dataType string, privateKey string, cid *string) map[string]interface{} {
data := map[string]interface{}{
"type": dataType,
"timestamp": time.Now().UnixNano(),
"from": signer.Address(signer.PublicKey(privateKey)),
}
if cid != nil {
data["cid"] = *cid
}
return data
}
// CTransaction ํจ์๋ ์ฃผ์ด์ง CID์ ํ์
์ ์ฌ์ฉํ์ฌ ์๋ช
๋ ํธ๋์ญ์
์ ์์ฑํฉ๋๋ค.
func (f *Factory) CTransaction(cid, txType string, vars map[string]interface{}, privateKey *string) *model.SignedTransaction {
if privateKey == nil {
key := signer.GeneratePrivateKey()
privateKey = &key
}
transaction := f.Data(txType, *privateKey, &cid)
for key, value := range vars {
transaction[key] = value
}
signedTx := &model.SignedTransaction{
Transaction: transaction,
PublicKey: signer.PublicKey(*privateKey),
}
signedTx.Signature = signer.Signature(signedTx.Hash(), *privateKey)
return signedTx
}
// CRequest ํจ์๋ ์ฃผ์ด์ง CID์ ํ์
์ ์ฌ์ฉํ์ฌ ์๋ช
๋ ์์ฒญ์ ์์ฑํฉ๋๋ค.
func (f *Factory) CRequest(cid, reqType string, vars map[string]interface{}, privateKey *string) *model.SignedRequest {
if privateKey == nil {
key := signer.GeneratePrivateKey()
privateKey = &key
}
request := f.Data(reqType, *privateKey, &cid)
for key, value := range vars {
request[key] = value
}
signedReq := &model.SignedRequest{
Request: request,
PublicKey: signer.PublicKey(*privateKey),
}
signedReq.Signature = signer.Signature(signedReq.Hash(), *privateKey)
return signedReq
}
Factory ๊ตฌ์กฐ์ฒด
Factory ๊ตฌ์กฐ์ฒดTransaction ํจ์
Transaction ํจ์Request ํจ์
Request ํจ์Data ํจ์
Data ํจ์CTransaction ํจ์
CTransaction ํจ์CRequest ํจ์
CRequest ํจ์Last updated