github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_grant_kyc_transaction_unit_test.go (about) 1 //go:build all || unit 2 // +build all unit 3 4 package hedera 5 6 /*- 7 * 8 * Hedera Go SDK 9 * 10 * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC 11 * 12 * Licensed under the Apache License, Version 2.0 (the "License"); 13 * you may not use this file except in compliance with the License. 14 * You may obtain a copy of the License at 15 * 16 * http://www.apache.org/licenses/LICENSE-2.0 17 * 18 * Unless required by applicable law or agreed to in writing, software 19 * distributed under the License is distributed on an "AS IS" BASIS, 20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 * See the License for the specific language governing permissions and 22 * limitations under the License. 23 * 24 */ 25 26 import ( 27 "testing" 28 "time" 29 30 "github.com/hashgraph/hedera-protobufs-go/services" 31 protobuf "google.golang.org/protobuf/proto" 32 33 "github.com/stretchr/testify/assert" 34 35 "github.com/stretchr/testify/require" 36 ) 37 38 func TestUnitTokenGrantKycTransactionValidate(t *testing.T) { 39 t.Parallel() 40 41 client, err := _NewMockClient() 42 client.SetLedgerID(*NewLedgerIDTestnet()) 43 require.NoError(t, err) 44 client.SetAutoValidateChecksums(true) 45 accountID, err := AccountIDFromString("0.0.123-esxsf") 46 require.NoError(t, err) 47 tokenID, err := TokenIDFromString("0.0.123-esxsf") 48 require.NoError(t, err) 49 50 tokenGrantKyc := NewTokenGrantKycTransaction(). 51 SetAccountID(accountID). 52 SetTokenID(tokenID) 53 54 err = tokenGrantKyc.validateNetworkOnIDs(client) 55 require.NoError(t, err) 56 } 57 58 func TestUnitTokenGrantKycTransactionValidateWrong(t *testing.T) { 59 t.Parallel() 60 61 client, err := _NewMockClient() 62 client.SetLedgerID(*NewLedgerIDTestnet()) 63 require.NoError(t, err) 64 client.SetAutoValidateChecksums(true) 65 accountID, err := AccountIDFromString("0.0.123-rmkykd") 66 require.NoError(t, err) 67 tokenID, err := TokenIDFromString("0.0.123-rmkykd") 68 require.NoError(t, err) 69 70 tokenGrantKyc := NewTokenGrantKycTransaction(). 71 SetAccountID(accountID). 72 SetTokenID(tokenID) 73 74 err = tokenGrantKyc.validateNetworkOnIDs(client) 75 assert.Error(t, err) 76 if err != nil { 77 assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error()) 78 } 79 } 80 81 func TestUnitTokenGrantKycTransactionGet(t *testing.T) { 82 t.Parallel() 83 84 tokenID := TokenID{Token: 7} 85 86 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 87 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 88 89 transaction, err := NewTokenGrantKycTransaction(). 90 SetTransactionID(transactionID). 91 SetNodeAccountIDs(nodeAccountID). 92 SetTokenID(tokenID). 93 SetAccountID(AccountID{Account: 3}). 94 SetMaxTransactionFee(NewHbar(10)). 95 SetTransactionMemo(""). 96 SetTransactionValidDuration(60 * time.Second). 97 SetRegenerateTransactionID(false). 98 Freeze() 99 require.NoError(t, err) 100 101 transaction.GetTransactionID() 102 transaction.GetNodeAccountIDs() 103 104 _, err = transaction.GetTransactionHash() 105 require.NoError(t, err) 106 107 transaction.GetTokenID() 108 transaction.GetAccountID() 109 transaction.GetMaxTransactionFee() 110 transaction.GetTransactionMemo() 111 transaction.GetRegenerateTransactionID() 112 _, err = transaction.GetSignatures() 113 require.NoError(t, err) 114 transaction.GetRegenerateTransactionID() 115 transaction.GetMaxTransactionFee() 116 transaction.GetRegenerateTransactionID() 117 } 118 119 func TestUnitTokenGrantKycTransactionNothingSet(t *testing.T) { 120 t.Parallel() 121 122 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 123 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 124 125 transaction, err := NewTokenGrantKycTransaction(). 126 SetTransactionID(transactionID). 127 SetNodeAccountIDs(nodeAccountID). 128 Freeze() 129 require.NoError(t, err) 130 131 transaction.GetTransactionID() 132 transaction.GetNodeAccountIDs() 133 134 _, err = transaction.GetTransactionHash() 135 require.NoError(t, err) 136 137 transaction.GetTokenID() 138 transaction.GetAccountID() 139 transaction.GetMaxTransactionFee() 140 transaction.GetTransactionMemo() 141 transaction.GetRegenerateTransactionID() 142 _, err = transaction.GetSignatures() 143 require.NoError(t, err) 144 transaction.GetRegenerateTransactionID() 145 transaction.GetMaxTransactionFee() 146 transaction.GetRegenerateTransactionID() 147 } 148 149 func TestUnitTokenGrantKycTransactionProtoCheck(t *testing.T) { 150 t.Parallel() 151 152 tokenID := TokenID{Token: 7} 153 accountID := AccountID{Account: 7} 154 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 155 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 156 157 transaction, err := NewTokenGrantKycTransaction(). 158 SetTransactionID(transactionID). 159 SetNodeAccountIDs(nodeAccountID). 160 SetTokenID(tokenID). 161 SetAccountID(accountID). 162 SetMaxTransactionFee(NewHbar(10)). 163 SetTransactionMemo(""). 164 SetTransactionValidDuration(60 * time.Second). 165 SetRegenerateTransactionID(false). 166 Freeze() 167 require.NoError(t, err) 168 169 transaction.GetTransactionID() 170 transaction.GetNodeAccountIDs() 171 172 proto := transaction.build().GetTokenGrantKyc() 173 require.Equal(t, proto.Token.String(), tokenID._ToProtobuf().String()) 174 require.Equal(t, proto.Account.String(), accountID._ToProtobuf().String()) 175 } 176 177 func TestUnitTokenGrantKycTransactionCoverage(t *testing.T) { 178 t.Parallel() 179 180 checksum := "dmqui" 181 grpc := time.Second * 30 182 token := TokenID{Token: 3, checksum: &checksum} 183 account := AccountID{Account: 3, checksum: &checksum} 184 nodeAccountID := []AccountID{{Account: 10}} 185 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 186 187 newKey, err := PrivateKeyGenerateEd25519() 188 require.NoError(t, err) 189 190 client, err := _NewMockClient() 191 client.SetLedgerID(*NewLedgerIDTestnet()) 192 require.NoError(t, err) 193 client.SetAutoValidateChecksums(true) 194 195 transaction, err := NewTokenGrantKycTransaction(). 196 SetTransactionID(transactionID). 197 SetNodeAccountIDs(nodeAccountID). 198 SetTokenID(token). 199 SetAccountID(account). 200 SetGrpcDeadline(&grpc). 201 SetMaxTransactionFee(NewHbar(3)). 202 SetMaxRetry(3). 203 SetMaxBackoff(time.Second * 30). 204 SetMinBackoff(time.Second * 10). 205 SetTransactionMemo("no"). 206 SetTransactionValidDuration(time.Second * 30). 207 SetRegenerateTransactionID(false). 208 Freeze() 209 require.NoError(t, err) 210 211 err = transaction.validateNetworkOnIDs(client) 212 require.NoError(t, err) 213 _, err = transaction.Schedule() 214 require.NoError(t, err) 215 transaction.GetTransactionID() 216 transaction.GetNodeAccountIDs() 217 transaction.GetMaxRetry() 218 transaction.GetMaxTransactionFee() 219 transaction.GetMaxBackoff() 220 transaction.GetMinBackoff() 221 transaction.GetRegenerateTransactionID() 222 byt, err := transaction.ToBytes() 223 require.NoError(t, err) 224 txFromBytes, err := TransactionFromBytes(byt) 225 require.NoError(t, err) 226 sig, err := newKey.SignTransaction(&transaction.Transaction) 227 require.NoError(t, err) 228 229 _, err = transaction.GetTransactionHash() 230 require.NoError(t, err) 231 transaction.GetMaxTransactionFee() 232 transaction.GetTransactionMemo() 233 transaction.GetRegenerateTransactionID() 234 transaction.GetTokenID() 235 transaction.GetAccountID() 236 _, err = transaction.GetSignatures() 237 require.NoError(t, err) 238 transaction.getName() 239 switch b := txFromBytes.(type) { 240 case TokenGrantKycTransaction: 241 b.AddSignature(newKey.PublicKey(), sig) 242 } 243 } 244 245 func TestUnitTokenGrantKycTransactionMock(t *testing.T) { 246 t.Parallel() 247 248 newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98") 249 require.NoError(t, err) 250 251 call := func(request *services.Transaction) *services.TransactionResponse { 252 require.NotEmpty(t, request.SignedTransactionBytes) 253 signedTransaction := services.SignedTransaction{} 254 _ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction) 255 256 require.NotEmpty(t, signedTransaction.BodyBytes) 257 transactionBody := services.TransactionBody{} 258 _ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody) 259 260 require.NotNil(t, transactionBody.TransactionID) 261 transactionId := transactionBody.TransactionID.String() 262 require.NotEqual(t, "", transactionId) 263 264 sigMap := signedTransaction.GetSigMap() 265 require.NotNil(t, sigMap) 266 267 return &services.TransactionResponse{ 268 NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, 269 } 270 } 271 responses := [][]interface{}{{ 272 call, 273 }} 274 275 client, server := NewMockClientAndServer(responses) 276 defer server.Close() 277 278 checksum := "dmqui" 279 token := TokenID{Token: 3, checksum: &checksum} 280 account := AccountID{Account: 3, checksum: &checksum} 281 282 freez, err := NewTokenGrantKycTransaction(). 283 SetNodeAccountIDs([]AccountID{{Account: 3}}). 284 SetTokenID(token). 285 SetAccountID(account). 286 FreezeWith(client) 287 require.NoError(t, err) 288 289 _, err = freez.Sign(newKey).Execute(client) 290 require.NoError(t, err) 291 }