|
| 1 | +import * as CardanoWasm from '@emurgo/cardano-serialization-lib-nodejs'; |
| 2 | +import { Transaction } from '@emurgo/cardano-serialization-lib-nodejs'; |
| 3 | +import { mnemonicToEntropy } from 'bip39'; |
| 4 | +import { Service } from "./service"; |
| 5 | +import { InternalAdaConfig, UTXO } from "../types/ada"; |
| 6 | +import { |
| 7 | + BlockFrostAPI, |
| 8 | + BlockfrostServerError, |
| 9 | +} from "@blockfrost/blockfrost-js"; |
| 10 | +import { InvalidIntegration } from "../errors/integrations"; |
| 11 | +import { PeerType, TransactionOperation } from "fireblocks-sdk"; |
| 12 | + |
| 13 | +const CARDANO_PARAMS = { |
| 14 | + COINS_PER_UTXO_WORD: '34482', |
| 15 | + MAX_TX_SIZE: 16384, |
| 16 | + MAX_VALUE_SIZE: 5000, |
| 17 | +}; |
| 18 | + |
| 19 | +export class AdaService extends Service { |
| 20 | + private client: BlockFrostAPI; |
| 21 | + |
| 22 | + constructor({ testnet, integrations }: InternalAdaConfig) { |
| 23 | + super({ testnet, integrations }); |
| 24 | + this.client = new BlockFrostAPI({ projectId: 'testnetQMV4zxv1wbnSaqTFWuW3tVVOGA9noUkZ' }); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Craft ada delegate transaction |
| 29 | + * @param accountId id of the kiln account to use for the stake transaction |
| 30 | + * @param walletAddress withdrawal creds /!\ losing it => losing the ability to withdraw |
| 31 | + */ |
| 32 | + async craftStakeTx( |
| 33 | + accountId: string, |
| 34 | + walletAddress: string, |
| 35 | + ): Promise<Transaction> { |
| 36 | + |
| 37 | + let utxo: UTXO = []; |
| 38 | + try { |
| 39 | + utxo = await this.client.addressesUtxosAll(walletAddress); |
| 40 | + } catch (error) { |
| 41 | + if (error instanceof BlockfrostServerError && error.status_code === 404) { |
| 42 | + // Address derived from the seed was not used yet |
| 43 | + // In this case Blockfrost API will return 404 |
| 44 | + utxo = []; |
| 45 | + } else { |
| 46 | + throw error; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if (utxo.length === 0) { |
| 51 | + throw new Error(`You should send ADA to ${walletAddress} to have enough funds to sent a transaction`); |
| 52 | + } |
| 53 | + |
| 54 | + // Get current blockchain slot from latest block |
| 55 | + const latestBlock = await this.client.blocksLatest(); |
| 56 | + const currentSlot = latestBlock.slot; |
| 57 | + if (!currentSlot) { |
| 58 | + throw Error('Failed to fetch slot number'); |
| 59 | + } |
| 60 | + |
| 61 | + return this.composeTransaction( |
| 62 | + walletAddress, |
| 63 | + 'addr_test1qqh2fphcgd0qsmwsqf4v8v9z2w3cpmzw5y9nx6h8z9v85qj7mjg5eydjgyvn3md3fwlyt2e4veynlwutp7u99m4l6q2sp3rdkv', |
| 64 | + '1000000', |
| 65 | + utxo, |
| 66 | + currentSlot, |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Sign transaction with given integration |
| 72 | + * @param integration |
| 73 | + * @param transaction |
| 74 | + */ |
| 75 | + async sign(integration: string, transaction: Transaction): Promise<Transaction> { |
| 76 | + const currentIntegration = this.integrations?.find(int => int.name === integration); |
| 77 | + if (!currentIntegration) { |
| 78 | + throw new InvalidIntegration(`Unknown integration, please provide an integration name that matches one of the integrations provided in the config.`); |
| 79 | + } |
| 80 | + |
| 81 | + // We only support fireblocks integration for now |
| 82 | + if (currentIntegration.provider !== 'fireblocks') { |
| 83 | + throw new InvalidIntegration(`Unsupported integration provider: ${currentIntegration.provider}`); |
| 84 | + } |
| 85 | + |
| 86 | + if (!this.fbSigner) { |
| 87 | + throw new InvalidIntegration(`Could not retrieve fireblocks signer.`); |
| 88 | + } |
| 89 | + |
| 90 | + const message = transaction.to_hex(); |
| 91 | + |
| 92 | + const payload = { |
| 93 | + rawMessageData: { |
| 94 | + messages: [ |
| 95 | + { |
| 96 | + "content": message, |
| 97 | + }, |
| 98 | + ] |
| 99 | + }, |
| 100 | + inputsSelection: { |
| 101 | + inputsToSpend: JSON.parse(transaction.body().inputs().to_json()), |
| 102 | + } |
| 103 | + }; |
| 104 | + |
| 105 | + const tx = await this.fbSigner.signWithFB(payload, 'ADA_TEST'); |
| 106 | + const sigBuffer = Buffer.from(tx.signedMessages![0].signature.fullSig, 'hex'); |
| 107 | + console.log(tx.signedMessages); |
| 108 | + // // transaction.set_is_valid(true); |
| 109 | + // |
| 110 | + // const txHash = CardanoWasm.hash_transaction(transaction.body()); |
| 111 | + // const witnesses = CardanoWasm.TransactionWitnessSet.new(); |
| 112 | + // const vkeyWitnesses = CardanoWasm.Vkeywitnesses.new(); |
| 113 | + // const MNEMONIC = 'word 1 word 2'; |
| 114 | + // const bip32PrvKey = this.mnemonicToPrivateKey(MNEMONIC); |
| 115 | + // const { signKey } = this.deriveAddressPrvKey(bip32PrvKey, this.testnet); |
| 116 | + // vkeyWitnesses.add(CardanoWasm.make_vkey_witness(txHash, signKey)); |
| 117 | + // witnesses.set_vkeys(vkeyWitnesses); |
| 118 | + // console.log(witnesses); |
| 119 | + // const tx = CardanoWasm.Transaction.new(transaction.body(), witnesses); |
| 120 | + |
| 121 | + return transaction; |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Broadcast transaction to the network |
| 127 | + * @param transaction |
| 128 | + */ |
| 129 | + async broadcast(transaction: Transaction): Promise<string | undefined> { |
| 130 | + try { |
| 131 | + return await this.client.txSubmit(transaction.to_bytes()); |
| 132 | + } catch (error) { |
| 133 | + // submit could fail if the transactions is rejected by cardano node |
| 134 | + if (error instanceof BlockfrostServerError && error.status_code === 400) { |
| 135 | + console.log(error.message); |
| 136 | + } else { |
| 137 | + // rethrow other errors |
| 138 | + throw error; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private composeTransaction ( |
| 144 | + address: string, |
| 145 | + outputAddress: string, |
| 146 | + outputAmount: string, |
| 147 | + utxos: UTXO, |
| 148 | + currentSlot: number, |
| 149 | + ): Transaction { |
| 150 | + if (!utxos || utxos.length === 0) { |
| 151 | + throw Error(`No utxo on address ${address}`); |
| 152 | + } |
| 153 | + |
| 154 | + const txBuilder = CardanoWasm.TransactionBuilder.new( |
| 155 | + CardanoWasm.TransactionBuilderConfigBuilder.new() |
| 156 | + .fee_algo( |
| 157 | + CardanoWasm.LinearFee.new( |
| 158 | + CardanoWasm.BigNum.from_str('44'), |
| 159 | + CardanoWasm.BigNum.from_str('155381'), |
| 160 | + ), |
| 161 | + ) |
| 162 | + .pool_deposit(CardanoWasm.BigNum.from_str('500000000')) |
| 163 | + .key_deposit(CardanoWasm.BigNum.from_str('2000000')) |
| 164 | + .coins_per_utxo_word( |
| 165 | + CardanoWasm.BigNum.from_str(CARDANO_PARAMS.COINS_PER_UTXO_WORD), |
| 166 | + ) |
| 167 | + .max_value_size(CARDANO_PARAMS.MAX_VALUE_SIZE) |
| 168 | + .max_tx_size(CARDANO_PARAMS.MAX_TX_SIZE) |
| 169 | + .build(), |
| 170 | + ); |
| 171 | + |
| 172 | + const outputAddr = CardanoWasm.Address.from_bech32(outputAddress); |
| 173 | + const changeAddr = CardanoWasm.Address.from_bech32(address); |
| 174 | + |
| 175 | + // Set TTL to +2h from currentSlot |
| 176 | + // If the transaction is not included in a block before that slot it will be cancelled. |
| 177 | + const ttl = currentSlot + 7200; |
| 178 | + txBuilder.set_ttl(ttl); |
| 179 | + |
| 180 | + // Add output to the tx |
| 181 | + txBuilder.add_output( |
| 182 | + CardanoWasm.TransactionOutput.new( |
| 183 | + outputAddr, |
| 184 | + CardanoWasm.Value.new(CardanoWasm.BigNum.from_str(outputAmount)), |
| 185 | + ), |
| 186 | + ); |
| 187 | + |
| 188 | + // Filter out multi asset utxo to keep this simple |
| 189 | + const lovelaceUtxos = utxos.filter( |
| 190 | + (u: any) => !u.amount.find((a: any) => a.unit !== 'lovelace'), |
| 191 | + ); |
| 192 | + |
| 193 | + // Create TransactionUnspentOutputs from utxos fetched from Blockfrost |
| 194 | + const unspentOutputs = CardanoWasm.TransactionUnspentOutputs.new(); |
| 195 | + for (const utxo of lovelaceUtxos) { |
| 196 | + const amount = utxo.amount.find( |
| 197 | + (a: any) => a.unit === 'lovelace', |
| 198 | + )?.quantity; |
| 199 | + |
| 200 | + if (!amount) continue; |
| 201 | + |
| 202 | + const inputValue = CardanoWasm.Value.new( |
| 203 | + CardanoWasm.BigNum.from_str(amount.toString()), |
| 204 | + ); |
| 205 | + |
| 206 | + const input = CardanoWasm.TransactionInput.new( |
| 207 | + CardanoWasm.TransactionHash.from_bytes(Buffer.from(utxo.tx_hash, 'hex')), |
| 208 | + utxo.output_index, |
| 209 | + ); |
| 210 | + const output = CardanoWasm.TransactionOutput.new(changeAddr, inputValue); |
| 211 | + unspentOutputs.add(CardanoWasm.TransactionUnspentOutput.new(input, output)); |
| 212 | + } |
| 213 | + |
| 214 | + txBuilder.add_inputs_from( |
| 215 | + unspentOutputs, |
| 216 | + CardanoWasm.CoinSelectionStrategyCIP2.LargestFirst, |
| 217 | + ); |
| 218 | + |
| 219 | + // Adds a change output if there are more ADA in utxo than we need for the transaction, |
| 220 | + // these coins will be returned to change address |
| 221 | + txBuilder.add_change_if_needed(changeAddr); |
| 222 | + |
| 223 | + // Build transaction |
| 224 | + return txBuilder.build_tx(); |
| 225 | + }; |
| 226 | + |
| 227 | + private harden (num: number): number { |
| 228 | + return 0x80000000 + num; |
| 229 | + }; |
| 230 | + |
| 231 | + private deriveAddressPrvKey ( |
| 232 | + bipPrvKey: CardanoWasm.Bip32PrivateKey, |
| 233 | + testnet: boolean, |
| 234 | + ): { |
| 235 | + signKey: CardanoWasm.PrivateKey; |
| 236 | + address: string; |
| 237 | + } { |
| 238 | + const networkId = testnet |
| 239 | + ? CardanoWasm.NetworkInfo.testnet().network_id() |
| 240 | + : CardanoWasm.NetworkInfo.mainnet().network_id(); |
| 241 | + const accountIndex = 0; |
| 242 | + const addressIndex = 0; |
| 243 | + |
| 244 | + const accountKey = bipPrvKey |
| 245 | + .derive(this.harden(1852)) // purpose |
| 246 | + .derive(this.harden(1815)) // coin type |
| 247 | + .derive(this.harden(accountIndex)); // account # |
| 248 | + |
| 249 | + const utxoKey = accountKey |
| 250 | + .derive(0) // external |
| 251 | + .derive(addressIndex); |
| 252 | + |
| 253 | + const stakeKey = accountKey |
| 254 | + .derive(2) // chimeric |
| 255 | + .derive(0) |
| 256 | + .to_public(); |
| 257 | + |
| 258 | + const baseAddress = CardanoWasm.BaseAddress.new( |
| 259 | + networkId, |
| 260 | + CardanoWasm.StakeCredential.from_keyhash( |
| 261 | + utxoKey.to_public().to_raw_key().hash(), |
| 262 | + ), |
| 263 | + CardanoWasm.StakeCredential.from_keyhash(stakeKey.to_raw_key().hash()), |
| 264 | + ); |
| 265 | + |
| 266 | + const address = baseAddress.to_address().to_bech32(); |
| 267 | + |
| 268 | + return { signKey: utxoKey.to_raw_key(), address: address }; |
| 269 | + }; |
| 270 | + |
| 271 | + private mnemonicToPrivateKey ( |
| 272 | + mnemonic: string, |
| 273 | + ): CardanoWasm.Bip32PrivateKey { |
| 274 | + const entropy = mnemonicToEntropy(mnemonic); |
| 275 | + |
| 276 | + const rootKey = CardanoWasm.Bip32PrivateKey.from_bip39_entropy( |
| 277 | + Buffer.from(entropy, 'hex'), |
| 278 | + Buffer.from(''), |
| 279 | + ); |
| 280 | + |
| 281 | + return rootKey; |
| 282 | + }; |
| 283 | +} |
0 commit comments