1
+ import {
2
+ Avalanche ,
3
+ BinTools ,
4
+ BN ,
5
+ Buffer
6
+ } from "../../src" ;
7
+ import {
8
+ AVMAPI ,
9
+ KeyChain as AVMKeyChain ,
10
+ SECPTransferOutput ,
11
+ SECPTransferInput ,
12
+ TransferableOutput ,
13
+ TransferableInput ,
14
+ UTXOSet ,
15
+ UTXO ,
16
+ AmountOutput ,
17
+ UnsignedTx ,
18
+ AVMConstants ,
19
+ OperationTx ,
20
+ TransferableOperation ,
21
+ Tx ,
22
+ KeyChain ,
23
+ NFTMintOperation ,
24
+ NFTMintOutput
25
+ } from "../../src/apis/avm"
26
+ import { OutputOwners } from "../../src/common" ;
27
+ import { Defaults } from "../../src/utils"
28
+
29
+ const getUTXOIDs = ( utxoSet : UTXOSet , txid : string , outputType : number = AVMConstants . SECPXFEROUTPUTID_CODECONE , assetID = "2fombhL7aGPwj3KH4bfrmJwW6PVnMobf9Y2fn9GwxiAAJyFDbe" ) : string [ ] => {
30
+ const utxoids : string [ ] = utxoSet . getUTXOIDs ( )
31
+ let result : string [ ] = [ ]
32
+ for ( let index : number = 0 ; index < utxoids . length ; ++ index ) {
33
+ if ( utxoids [ index ] . indexOf ( txid . slice ( 0 , 10 ) ) != - 1 && utxoSet . getUTXO ( utxoids [ index ] ) . getOutput ( ) . getOutputID ( ) == outputType && assetID == bintools . cb58Encode ( utxoSet . getUTXO ( utxoids [ index ] ) . getAssetID ( ) ) ) {
34
+ result . push ( utxoids [ index ] )
35
+ }
36
+ }
37
+ return result
38
+ }
39
+
40
+ const ip : string = "localhost"
41
+ const port : number = 9650
42
+ const protocol : string = "http"
43
+ const networkID : number = 12345
44
+ const avalanche : Avalanche = new Avalanche ( ip , port , protocol , networkID )
45
+ const xchain : AVMAPI = avalanche . XChain ( )
46
+ const bintools : BinTools = BinTools . getInstance ( )
47
+ const xKeychain : KeyChain = xchain . keyChain ( )
48
+ const privKey : string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
49
+ xKeychain . importKey ( privKey )
50
+ const xAddresses : Buffer [ ] = xchain . keyChain ( ) . getAddresses ( )
51
+ const xAddressStrings : string [ ] = xchain . keyChain ( ) . getAddressStrings ( )
52
+ const blockchainid : string = Defaults . network [ '12345' ] . X . blockchainID
53
+ const outputs : TransferableOutput [ ] = [ ]
54
+ const inputs : TransferableInput [ ] = [ ]
55
+ const operations : TransferableOperation [ ] = [ ]
56
+ const fee : BN = xchain . getDefaultTxFee ( )
57
+ const threshold : number = 1
58
+ const locktime : BN = new BN ( 0 )
59
+ const memo : Buffer = bintools . stringToBuffer ( "AVM manual OperationTx to mint an NFT" )
60
+ const payload : Buffer = bintools . stringToBuffer ( "NFT Payload" )
61
+ const groupID : number = 0
62
+ // Uncomment for codecID 00 01
63
+ // const codecID: number = 1
64
+
65
+ const main = async ( ) : Promise < any > => {
66
+ const avaxAssetID : Buffer = await xchain . getAVAXAssetID ( )
67
+ const avmUTXOResponse : any = await xchain . getUTXOs ( xAddressStrings )
68
+ const utxoSet : UTXOSet = avmUTXOResponse . utxos
69
+ const utxos : UTXO [ ] = utxoSet . getAllUTXOs ( )
70
+ utxos . forEach ( ( utxo : UTXO , index : number ) => {
71
+ const txid : Buffer = utxo . getTxID ( )
72
+ const outputidx : Buffer = utxo . getOutputIdx ( )
73
+ const assetID : Buffer = utxo . getAssetID ( ) ;
74
+ if ( utxo . getOutput ( ) . getTypeID ( ) != 10 && utxo . getOutput ( ) . getTypeID ( ) != 11 ) {
75
+ const amountOutput : AmountOutput = utxo . getOutput ( ) as AmountOutput
76
+ const amt : BN = amountOutput . getAmount ( ) . clone ( )
77
+
78
+ if ( assetID . toString ( 'hex' ) === avaxAssetID . toString ( 'hex' ) ) {
79
+ const secpTransferOutput : SECPTransferOutput = new SECPTransferOutput ( amt . sub ( fee ) , xAddresses , locktime , threshold )
80
+ // Uncomment for codecID 00 01
81
+ // secpTransferOutput.setCodecID(codecID)
82
+ const transferableOutput : TransferableOutput = new TransferableOutput ( avaxAssetID , secpTransferOutput )
83
+ outputs . push ( transferableOutput )
84
+
85
+ const secpTransferInput : SECPTransferInput = new SECPTransferInput ( amt )
86
+ // Uncomment for codecID 00 01
87
+ // secpTransferInput.setCodecID(codecID)
88
+ secpTransferInput . addSignatureIdx ( 0 , xAddresses [ 0 ] )
89
+ const input : TransferableInput = new TransferableInput ( txid , outputidx , avaxAssetID , secpTransferInput )
90
+ inputs . push ( input )
91
+ } else {
92
+ const secpTransferOutput : SECPTransferOutput = new SECPTransferOutput ( amt , xAddresses , locktime , threshold )
93
+ // Uncomment for codecID 00 01
94
+ // secpTransferOutput.setCodecID(codecID)
95
+ const transferableOutput : TransferableOutput = new TransferableOutput ( assetID , secpTransferOutput )
96
+ outputs . push ( transferableOutput )
97
+
98
+ const secpTransferInput : SECPTransferInput = new SECPTransferInput ( amt )
99
+ // Uncomment for codecID 00 01
100
+ // secpTransferInput.setCodecID(codecID)
101
+ secpTransferInput . addSignatureIdx ( 0 , xAddresses [ 0 ] )
102
+ const input : TransferableInput = new TransferableInput ( txid , outputidx , assetID , secpTransferInput )
103
+ inputs . push ( input )
104
+ }
105
+ } else if ( utxo . getOutput ( ) . getTypeID ( ) != 7 && utxo . getOutput ( ) . getTypeID ( ) != 11 ) {
106
+ const outputOwners : OutputOwners = new OutputOwners ( xAddresses , locktime , threshold )
107
+ const nftMintOutputUTXOIDs : string [ ] = getUTXOIDs ( utxoSet , bintools . cb58Encode ( txid ) , AVMConstants . NFTMINTOUTPUTID , bintools . cb58Encode ( assetID ) )
108
+ const mintOwner : NFTMintOutput = utxo . getOutput ( ) as NFTMintOutput
109
+ // Uncomment for codecID 00 01
110
+ // mintOwner.setCodecID(codecID)
111
+ const nftMintOperation : NFTMintOperation = new NFTMintOperation ( groupID , payload , [ outputOwners ] )
112
+ // Uncomment for codecID 00 01
113
+ // nftMintOperation.setCodecID(codecID)
114
+ const spenders : Buffer [ ] = mintOwner . getSpenders ( xAddresses )
115
+ const nftMintOutputUTXOID : string = utxo . getUTXOID ( )
116
+ if ( nftMintOutputUTXOID === nftMintOutputUTXOIDs [ 0 ] ) {
117
+ spenders . forEach ( ( spender : Buffer ) => {
118
+ const idx : number = mintOwner . getAddressIdx ( spender )
119
+ nftMintOperation . addSignatureIdx ( idx , spender )
120
+ } )
121
+
122
+ const transferableOperation : TransferableOperation = new TransferableOperation ( utxo . getAssetID ( ) , [ nftMintOutputUTXOID ] , nftMintOperation )
123
+ operations . push ( transferableOperation )
124
+ }
125
+ }
126
+ } )
127
+ const operationTx : OperationTx = new OperationTx (
128
+ networkID ,
129
+ bintools . cb58Decode ( blockchainid ) ,
130
+ outputs ,
131
+ inputs ,
132
+ memo ,
133
+ operations
134
+ )
135
+ // Uncomment for codecID 00 01
136
+ // operationTx.setCodecID(codecID)
137
+
138
+ const unsignedTx : UnsignedTx = new UnsignedTx ( operationTx )
139
+ const tx : Tx = unsignedTx . sign ( xKeychain )
140
+ const id : string = await xchain . issueTx ( tx )
141
+ console . log ( id )
142
+ }
143
+
144
+ main ( )
145
+
0 commit comments