From 2d0a2530def920c0c72a665ce93730a4c2ad8197 Mon Sep 17 00:00:00 2001 From: Dusan Sekulic Date: Tue, 23 Nov 2021 14:31:50 +0100 Subject: [PATCH] Pubkey issuance contract (#183) * added factory method FromPublicKeys for creating multiscript payment * issuer_pubkey added * contract hash update * contract hash update * refactor --- pset/updater_test.go | 4 ++-- transaction/data/issuance.json | 9 +++++---- transaction/issuance.go | 20 +++++++++++++++++++- transaction/issuance_test.go | 29 +++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 7 deletions(-) diff --git a/pset/updater_test.go b/pset/updater_test.go index d3c15f4..c8bed7b 100644 --- a/pset/updater_test.go +++ b/pset/updater_test.go @@ -171,8 +171,8 @@ func TestUpdaterAddIssuance(t *testing.T) { TokenAddress: "el1qqw3e3mk4ng3ks43mh54udznuekaadh9lgwef3mwgzrfzakmdwcvqpe4ppdaa3t44v3zv2u6w56pv6tc666fvgzaclqjnkz0sd", }, expectedNumOuts: 2, - expectedAsset: "707953a405b1a79180ec7830e51d53997b3d1ff9aa614513266059dbfbbdbeb7", - expectedToken: "c99343e56d9783a816b866c48d182a9537d4979f1ed66d90ac6a49ec773daee9", + expectedAsset: "a5795a31c20057a117af937c9b28c119ada3c402c7c326a1c5e4fd1e95e1c588", + expectedToken: "2b06c9d643481c54b1440b615928b3811cf4f499787360c42a368ecc7fd92b2e", }, { args: AddIssuanceArgs{ diff --git a/transaction/data/issuance.json b/transaction/data/issuance.json index 4ec4d8c..b08727b 100644 --- a/transaction/data/issuance.json +++ b/transaction/data/issuance.json @@ -25,13 +25,14 @@ "ticker": "TST", "version": 0, "precision": 0, - "entity": { "domain": "test.io" } + "entity": { "domain": "test.io" }, + "issuer_pubkey": "02a9a7399de89ec2e7de876bbe0b512f78f13d5d0a3315047e5b14109c8bac38f2" }, "expectedAssetAmount": 1000, "expectedTokenAmount": 1, - "expectedEntropy": "2b73af1c9ae64a6903b3055361dd7b75082003a85374049982fc1e8f31b9a8cf", - "expectedAsset": "7af6e284f3729228895eddd45b39f72be65f8866d404e5c4374b2b97e7f178e7", - "expectedToken": "5b706edc0f72ea114e7a1f867c8b63a698944435e62699b9821deecc6d81bcef" + "expectedEntropy": "e405b6a4f891b7226cc3d9075c1a9c64ab73bb5f68e458dbe95340518c455bf7", + "expectedAsset": "210521aacc918a1c78106e720a667606f53eb72be593bc450329843bc3b959f5", + "expectedToken": "a0d7995a1d61446957834aaebd053a2c494dd9d14abc83486e4de2c4edd15943" } ] } diff --git a/transaction/issuance.go b/transaction/issuance.go index b35a723..ac1f0e5 100644 --- a/transaction/issuance.go +++ b/transaction/issuance.go @@ -22,6 +22,7 @@ type IssuanceContract struct { Ticker string `json:"ticker"` Version uint `json:"version"` Precision uint `json:"precision"` + PubKey string `json:"issuer_pubkey"` Entity IssuanceEntity `json:"entity"` } @@ -110,8 +111,12 @@ func NewTxIssuance( return nil, err } - contractHash = chainhash.HashB(serializedContract) + tmp, err := orderJsonKeysLexographically(serializedContract) + if err != nil { + return nil, err + } + contractHash = chainhash.HashB(tmp) } confAssetAmount, err := toConfidentialAssetAmount( @@ -218,3 +223,16 @@ func toConfidentialTokenAmount(tokenAmount uint64) ([]byte, error) { } return confAmount[:], nil } + +func orderJsonKeysLexographically(bytes []byte) ([]byte, error) { + var ifce interface{} + err := json.Unmarshal(bytes, &ifce) + if err != nil { + return []byte{}, err + } + output, err := json.Marshal(ifce) + if err != nil { + return []byte{}, err + } + return output, nil +} diff --git a/transaction/issuance_test.go b/transaction/issuance_test.go index e608ed0..aad209f 100644 --- a/transaction/issuance_test.go +++ b/transaction/issuance_test.go @@ -83,3 +83,32 @@ func TestIssuanceGeneration(t *testing.T) { ) } } + +func TestIsContractHashValid(t *testing.T) { + contract := IssuanceContract{ + Name: "Tiero Token", + Ticker: "TIERO", + Version: 0, + Precision: 8, + PubKey: "02a9a7399de89ec2e7de876bbe0b512f78f13d5d0a3315047e5b14109c8bac38f2", + Entity: IssuanceEntity{ + Domain: "tiero.github.io", + }, + } + + issuance, err := NewTxIssuance(10, 2, 8, &contract) + if err != nil { + t.Fatal(err) + } + + assert.Equal( + t, + "d5c4363ee9cf2a4319c2f0ccc04cdb83d6213e4d26d94d70003c58eaf2473866", + hex.EncodeToString(elementsutil.ReverseBytes(issuance.ContractHash)), //validate online with reverse contract hash + ) + assert.Equal( + t, + "663847f2ea583c00704dd9264d3e21d683db4cc0ccf0c219432acfe93e36c4d5", + hex.EncodeToString(issuance.ContractHash), + ) +}