github.com/aergoio/aergo@v1.3.1/cmd/aergocli/util/base58addr_test.go (about)

     1  package util
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/aergoio/aergo/types"
     7  	"github.com/mr-tron/base58/base58"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestParseConvBase58Tx(t *testing.T) {
    12  	testjson := "[{\"Hash\":\"525mQMtsWaDLVJbzQZgTFkSG33gtZsho7m4io1HUCeJi\",\"Body\":{\"Nonce\":9,\"Account\":\"AsiFCzSukVNUGufJSzSNLA1nKx39NxKcVBEWvW3riyfixcBjN1Qd\",\"Recipient\":\"AsjHhFbCuULoUVZPiNNV6WEemtEi7Eiy6G4TDaUsMDiedCARbhQR\",\"Amount\":\"100000000\",\"Payload\":null,\"Limit\":100,\"Price\":\"1\",\"Type\":0,\"Sign\":\"3tMHYrizQ532D1WJkt5RSs5AcRmq7betw8zvC66Wh3XHUdvNpNzLWh1SkkGYMGJ669nCVuYHrhwfg1HrUUp6KDwzK\"}}]"
    13  	res, err := ParseBase58Tx([]byte(testjson))
    14  	assert.NoError(t, err, "should be success")
    15  	assert.NotEmpty(t, res, "failed to parse json")
    16  	assert.Equal(t, "525mQMtsWaDLVJbzQZgTFkSG33gtZsho7m4io1HUCeJi", base58.Encode(res[0].Hash), "wrong hash")
    17  	assert.Equal(t, "3tMHYrizQ532D1WJkt5RSs5AcRmq7betw8zvC66Wh3XHUdvNpNzLWh1SkkGYMGJ669nCVuYHrhwfg1HrUUp6KDwzK", base58.Encode(res[0].Body.Sign), "wrong sign")
    18  
    19  	account, err := types.DecodeAddress("AsiFCzSukVNUGufJSzSNLA1nKx39NxKcVBEWvW3riyfixcBjN1Qd")
    20  	assert.NoError(t, err, "should be success")
    21  	assert.Equal(t, account, res[0].Body.Account, "wrong account")
    22  
    23  	recipient, err := types.DecodeAddress("AsjHhFbCuULoUVZPiNNV6WEemtEi7Eiy6G4TDaUsMDiedCARbhQR")
    24  	assert.NoError(t, err, "should be success")
    25  	assert.Equal(t, recipient, res[0].Body.Recipient, "wrong recipient")
    26  }
    27  
    28  func TestParseBase58TxBody(t *testing.T) {
    29  	testjson := "{\"Nonce\":1,\"Account\":\"AsiFCzSukVNUGufJSzSNLA1nKx39NxKcVBEWvW3riyfixcBjN1Qd\",\"Recipient\":\"AsjHhFbCuULoUVZPiNNV6WEemtEi7Eiy6G4TDaUsMDiedCARbhQR\",\"Amount\":\"25000\",\"Payload\":\"aergo\",\"Limit\":100,\"Price\":\"1\",\"Type\":0,\"Sign\":\"3roWPzztf5aLLh16vAnd2ugcPux3wJ1oqqvqkWARobjuAC32xftF42nnbTkXUQdkDaFvuUmctrpQSv8FAVUKcywHW\"}"
    30  	res, err := ParseBase58TxBody([]byte(testjson))
    31  	assert.NoError(t, err, "should be success")
    32  	assert.NotEmpty(t, res, "failed to parse json")
    33  
    34  	assert.Equal(t, "3roWPzztf5aLLh16vAnd2ugcPux3wJ1oqqvqkWARobjuAC32xftF42nnbTkXUQdkDaFvuUmctrpQSv8FAVUKcywHW", base58.Encode(res.Sign), "wrong sign")
    35  	assert.Equal(t, "aergo", base58.Encode(res.Payload), "wrong payload")
    36  	account, err := types.DecodeAddress("AsiFCzSukVNUGufJSzSNLA1nKx39NxKcVBEWvW3riyfixcBjN1Qd")
    37  	assert.NoError(t, err, "should be success")
    38  	assert.Equal(t, account, res.Account, "wrong account")
    39  
    40  	recipient, err := types.DecodeAddress("AsjHhFbCuULoUVZPiNNV6WEemtEi7Eiy6G4TDaUsMDiedCARbhQR")
    41  	assert.NoError(t, err, "should be success")
    42  	assert.Equal(t, recipient, res.Recipient, "wrong recipient")
    43  }
    44  
    45  func TestBlockConvBase58(t *testing.T) {
    46  	const accountBase58 = "AmMW2bVcfroiuV4Bvy56op5zzqn42xgrLCwSxMka23K75yTBmudz"
    47  	const recipientBase58 = "AmMW2bVcfroiuV4Bvy56op5zzqn42xgrLCwSxMka23K75yTBmudz"
    48  	const payloadBase58 = "525mQMtsWaDLVJbzQZgTFkSG33gtZsho7m4io1HUCeJi"
    49  	testBlock := &types.Block{Body: &types.BlockBody{Txs: []*types.Tx{}}}
    50  	result := ConvBlock(nil)
    51  	assert.Empty(t, result, "failed to convert nil")
    52  
    53  	result = ConvBlock(testBlock)
    54  	assert.Empty(t, result.Body.Txs, "failed to convert txs")
    55  
    56  	account, err := types.DecodeAddress(accountBase58)
    57  	assert.NoError(t, err, "should be decode account")
    58  
    59  	recipient, err := types.DecodeAddress(recipientBase58)
    60  	assert.NoError(t, err, "should be decode recipient")
    61  
    62  	payload, err := base58.Decode(payloadBase58)
    63  	assert.NoError(t, err, "should be decode payload")
    64  
    65  	testTx := &types.Tx{Body: &types.TxBody{
    66  		Account:   account,
    67  		Recipient: recipient,
    68  		Payload:   payload,
    69  	}}
    70  
    71  	testBlock.Body.Txs = append(testBlock.Body.Txs, testTx)
    72  	result = ConvBlock(testBlock)
    73  	assert.Equal(t, accountBase58, result.Body.Txs[0].Body.Account, "failed to convert account")
    74  	assert.Equal(t, recipientBase58, result.Body.Txs[0].Body.Recipient, "failed to convert recipient")
    75  	assert.Equal(t, payloadBase58, result.Body.Txs[0].Body.Payload, "failed to convert payload")
    76  	t.Log(BlockConvBase58Addr(testBlock))
    77  }