Skip to content

Commit 7e1c6b5

Browse files
committed
feat: signer
1 parent 67808f8 commit 7e1c6b5

File tree

1 file changed

+72
-4
lines changed

1 file changed

+72
-4
lines changed

packages/sdk/src/mintlayer-connect-sdk.ts

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ import initWasm, {
4141
encode_output_data_deposit,
4242
encode_output_create_delegation,
4343
encode_output_delegate_staking,
44+
encode_signed_transaction,
45+
encode_witness,
46+
SignatureHashType,
4447
} from '@mintlayer/wasm-lib';
4548

4649
const BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
@@ -2787,14 +2790,79 @@ export class TransactionSigner {
27872790
this.key = privateKey;
27882791
}
27892792

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);
27932861
return signature;
27942862
}
27952863

27962864
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);
27982866
return transaction_signed;
27992867
}
28002868

0 commit comments

Comments
 (0)