@@ -41,6 +41,9 @@ import initWasm, {
41
41
encode_output_data_deposit ,
42
42
encode_output_create_delegation ,
43
43
encode_output_delegate_staking ,
44
+ encode_signed_transaction ,
45
+ encode_witness ,
46
+ SignatureHashType ,
44
47
} from '@mintlayer/wasm-lib' ;
45
48
46
49
const BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' ;
@@ -2787,14 +2790,79 @@ export class TransactionSigner {
2787
2790
this . key = privateKey ;
2788
2791
}
2789
2792
2790
- // TODO implement signatures
2791
- private createSignature ( ) {
2792
- const signature = new Uint8Array ( this . key . length * 64 ) ;
2793
+ private createSignature ( tx : Transaction ) {
2794
+ const network = Network . Testnet ; // TODO: make network configurable
2795
+ const optUtxos_ = tx . JSONRepresentation . inputs . map ( ( input ) => {
2796
+ if ( input . input . input_type !== 'UTXO' ) {
2797
+ return 0
2798
+ }
2799
+ const { utxo } : UtxoInput = input as UtxoInput ;
2800
+ if ( input . input . input_type === 'UTXO' ) {
2801
+ if ( utxo . type === 'Transfer' ) {
2802
+ if ( utxo . value . type === 'TokenV1' ) {
2803
+ return encode_output_token_transfer ( Amount . from_atoms ( utxo . value . amount . atoms ) , utxo . destination , utxo . value . token_id , network ) ;
2804
+ } else {
2805
+ return encode_output_transfer ( Amount . from_atoms ( utxo . value . amount . atoms ) , utxo . destination , network ) ;
2806
+ }
2807
+ }
2808
+ if ( utxo . type === 'LockThenTransfer' ) {
2809
+ let lockEncoded : Uint8Array = new Uint8Array ( ) ;
2810
+ if ( utxo . lock . type === 'UntilTime' ) {
2811
+ // @ts -ignore
2812
+ lockEncoded = encode_lock_until_time ( BigInt ( utxo . lock . content . timestamp ) ) ; // TODO: check if timestamp is correct
2813
+ }
2814
+ if ( utxo . lock . type === 'ForBlockCount' ) {
2815
+ lockEncoded = encode_lock_for_block_count ( BigInt ( utxo . lock . content ) ) ;
2816
+ }
2817
+ if ( utxo . value . type === 'TokenV1' ) {
2818
+ return encode_output_token_lock_then_transfer ( Amount . from_atoms ( utxo . value . amount . atoms ) , utxo . destination , utxo . value . token_id , lockEncoded , network ) ;
2819
+ } else {
2820
+ return encode_output_lock_then_transfer ( Amount . from_atoms ( utxo . value . amount . atoms ) , utxo . destination , lockEncoded , network ) ;
2821
+ }
2822
+ }
2823
+ return null
2824
+ }
2825
+ } )
2826
+
2827
+ const optUtxos : any [ ] = [ ]
2828
+ for ( let i = 0 ; i < optUtxos_ . length ; i ++ ) {
2829
+ if ( tx . JSONRepresentation . inputs [ i ] . input . input_type !== 'UTXO' ) {
2830
+ optUtxos . push ( 0 )
2831
+ continue
2832
+ } else {
2833
+ optUtxos . push ( 1 )
2834
+ optUtxos . push ( ...optUtxos_ [ i ] )
2835
+ continue
2836
+ }
2837
+ }
2838
+
2839
+ const encodedWitnesses = tx . JSONRepresentation . inputs . map (
2840
+ ( input , index ) => {
2841
+ const address =
2842
+ input ?. utxo ?. destination ||
2843
+ input ?. input ?. authority ||
2844
+ input ?. input ?. destination
2845
+ const addressPrivateKey = addressesPrivateKeys [ address ]
2846
+
2847
+ const witness = encode_witness (
2848
+ SignatureHashType . ALL ,
2849
+ addressPrivateKey ,
2850
+ address ,
2851
+ transaction ,
2852
+ optUtxos ,
2853
+ index ,
2854
+ network ,
2855
+ )
2856
+ return witness
2857
+ } ,
2858
+ )
2859
+
2860
+ const signature = mergeUint8Arrays ( encodedWitnesses ) ;
2793
2861
return signature ;
2794
2862
}
2795
2863
2796
2864
private encodeSignedTransaction ( tx : Transaction , signature : Uint8Array ) : string {
2797
- const transaction_signed = new Uint8Array ( this . key . length * 64 ) . toString ( ) ; // TODO implement
2865
+ const transaction_signed = encode_signed_transaction ( tx . HEXRepresentation_unsigned , signature ) ;
2798
2866
return transaction_signed ;
2799
2867
}
2800
2868
0 commit comments