Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Cardano primitives interface to support multiple implementations. The Rust-derived cardano-wallet dependency is the default and has also been updated to recent tx balance behaviour
- Loading branch information
1 parent
c528a4e
commit f744f78
Showing
47 changed files
with
925 additions
and
686 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { TransactionInput, TransactionOutput } from '../Transaction' | ||
import { FeeAlgorithm } from './FeeAlgorithm' | ||
import { Transaction } from './Transaction' | ||
import { AddressType, Address, UtxoWithAddressing } from '../Wallet' | ||
import { ChainSettings } from './ChainSettings' | ||
import { TransactionSelection } from './TransactionSelection' | ||
|
||
export interface Cardano { | ||
buildTransaction: ( | ||
inputs: TransactionInput[], | ||
outputs: TransactionOutput[], | ||
feeAlgorithm?: FeeAlgorithm | ||
) => Transaction | ||
account: ( | ||
mnemonic: string, | ||
passphrase: string, | ||
accountIndex: number | ||
) => { privateParentKey: string, publicParentKey: string } | ||
address: ( | ||
args: { | ||
publicParentKey: string, | ||
index: number, | ||
type: AddressType | ||
accountIndex: number | ||
}, | ||
chainSettings?: ChainSettings | ||
) => Address | ||
signMessage: ( | ||
args: { | ||
privateParentKey: string | ||
addressType: AddressType | ||
signingIndex: number | ||
message: string | ||
} | ||
) => { signature: string, publicKey: string } | ||
verifyMessage: ( | ||
args: { | ||
publicKey: string | ||
message: string | ||
signature: string | ||
} | ||
) => boolean | ||
inputSelection: ( | ||
outputs: TransactionOutput[], | ||
utxoSet: UtxoWithAddressing[], | ||
changeAddress: string, | ||
feeAlgorithm?: FeeAlgorithm | ||
) => TransactionSelection | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export enum ChainSettings { | ||
mainnet = 'mainnet', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export enum FeeAlgorithm { | ||
default = 'default' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { TransactionInput } from '../Transaction' | ||
import { AddressType } from '../Wallet' | ||
import { ChainSettings } from './ChainSettings' | ||
|
||
export interface Transaction { | ||
toHex: () => string | ||
toJson: () => any | ||
id: () => string | ||
addWitness: (args: { privateParentKey: string, addressing: TransactionInput['addressing'], chainSettings?: ChainSettings }) => void | ||
addExternalWitness: (args: { publicParentKey: string, addressType: AddressType, witnessIndex: number, witnessHex: string }) => void | ||
finalize: () => string | ||
fee: () => string | ||
estimateFee?: () => string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { TransactionInput, TransactionOutput } from '../Transaction' | ||
|
||
export interface TransactionSelection { | ||
inputs: TransactionInput[] | ||
changeOutput: TransactionOutput | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { Cardano } from './Cardano' | ||
export { FeeAlgorithm } from './FeeAlgorithm' | ||
export { ChainSettings } from './ChainSettings' | ||
export { TransactionSelection } from './TransactionSelection' |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { BlockchainSettings as CardanoBlockchainSettings, Bip44AccountPublic } from 'cardano-wallet' | ||
import { AddressType } from '../Wallet' | ||
import Transaction, { TransactionInput } from '../Transaction' | ||
import { ChainSettings } from '../Cardano' | ||
|
||
export interface KeyManager { | ||
signTransaction: (transaction: ReturnType<typeof Transaction>, inputs: TransactionInput[], chainSettings?: CardanoBlockchainSettings, transactionsAsProofForSpending?: { [transactionId: string]: string }) => Promise<string> | ||
signTransaction: (transaction: ReturnType<typeof Transaction>, inputs: TransactionInput[], chainSettings?: ChainSettings, transactionsAsProofForSpending?: { [transactionId: string]: string }) => Promise<string> | ||
signMessage: (addressType: AddressType, signingIndex: number, message: string) => Promise<{ publicKey: string, signature: string }> | ||
publicAccount: () => Promise<Bip44AccountPublic> | ||
publicParentKey: () => Promise<string> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { InMemoryKeyManager } from './InMemoryKey' | ||
export { KeyManager } from './KeyManager' | ||
export * from './errors' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,10 @@ | ||
import { getBindingsForEnvironment } from '../lib/bindings' | ||
import { InsufficientTransactionInput } from './errors' | ||
import { TransactionInput, TransactionInputCodec } from './TransactionInput' | ||
import { TransactionOutput, TransactionOutputCodec } from './TransactionOutput' | ||
import { validateCodec } from '../lib/validator' | ||
import { convertCoinToLovelace } from '../Utils' | ||
const { TransactionBuilder, TxoPointer, TxOut, Coin, LinearFeeAlgorithm, TransactionFinalized } = getBindingsForEnvironment() | ||
import { FeeAlgorithm, Cardano } from '../Cardano' | ||
|
||
export function Transaction (inputs: TransactionInput[], outputs: TransactionOutput[], feeAlgorithm = LinearFeeAlgorithm.default()) { | ||
export function Transaction (cardano: Cardano, inputs: TransactionInput[], outputs: TransactionOutput[], feeAlgorithm = FeeAlgorithm.default) { | ||
validateCodec<typeof TransactionInputCodec>(TransactionInputCodec, inputs) | ||
validateCodec<typeof TransactionOutputCodec>(TransactionOutputCodec, outputs) | ||
|
||
const transactionBuilder = buildTransaction(inputs, outputs) | ||
|
||
const balance = transactionBuilder.get_balance(feeAlgorithm) | ||
if (balance.is_negative()) throw new InsufficientTransactionInput() | ||
|
||
/* | ||
The get_balance_without_fees from the WASM bindings returns: | ||
Σ(transactionInputValues) - Σ(transactionOutputValues) | ||
This represents the fee paid on a transaction, as the positive balance | ||
between inputs and the associated outputs is equal to the fee paid | ||
*/ | ||
const feeAsCoinType = transactionBuilder.get_balance_without_fees().value() | ||
const fee = convertCoinToLovelace(feeAsCoinType) | ||
|
||
const cardanoTransaction = transactionBuilder.make_transaction() | ||
|
||
return { | ||
toHex: () => cardanoTransaction.to_hex(), | ||
toJson: () => cardanoTransaction.to_json(), | ||
id: () => cardanoTransaction.id(), | ||
finalize: () => new TransactionFinalized(cardanoTransaction), | ||
fee: () => fee | ||
} | ||
} | ||
|
||
export function buildTransaction (inputs: TransactionInput[], outputs: TransactionOutput[]) { | ||
const transactionBuilder = new TransactionBuilder() | ||
|
||
inputs.forEach(input => { | ||
const pointer = TxoPointer.from_json(input.pointer) | ||
const value = Coin.from(0, Number(input.value.value)) | ||
transactionBuilder.add_input(pointer, value) | ||
}) | ||
|
||
outputs.forEach(output => { | ||
const txOut = TxOut.from_json(output) | ||
transactionBuilder.add_output(txOut) | ||
}) | ||
|
||
return transactionBuilder | ||
return cardano.buildTransaction(inputs, outputs, feeAlgorithm) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { Transaction, buildTransaction } from './Transaction' | ||
import { Transaction } from './Transaction' | ||
import { TransactionInput } from './TransactionInput' | ||
import { TransactionOutput } from './TransactionOutput' | ||
|
||
export default Transaction | ||
export { TransactionInput, TransactionOutput, buildTransaction } | ||
export { TransactionInput, TransactionOutput } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.