Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cardano: Add vote delegation to node api #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions sandbox/src/Cardano.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,11 @@ function CardanoAddress({ bb02 }: Props) {
<ShowError err={err} />
</form>
</div>

);
}

function CardanoSignTransaction({ bb02 }: Props) {
type TxType = 'normal' | 'zero-ttl' | 'tokens' | 'delegate' | 'withdraw-staking-rewards';
type TxType = 'normal' | 'zero-ttl' | 'tokens' | 'delegate' | 'vote-delegation' | 'withdraw-staking-rewards';
const [txType, setTxType] = useState<TxType>('normal');
const [running, setRunning] = useState(false);
const [result, setResult] = useState<bitbox.CardanoSignTransactionResult | undefined>();
Expand All @@ -151,6 +150,7 @@ function CardanoSignTransaction({ bb02 }: Props) {
['zero-ttl', 'Transaction with TTL=0'],
['tokens', 'Transaction sending tokens'],
['delegate', 'Delegate staking to a pool'],
['vote-delegation', 'Delegate vote to a dRep'],
['withdraw-staking-rewards', 'Withdraw staking rewards'],
];

Expand All @@ -176,6 +176,7 @@ function CardanoSignTransaction({ bb02 }: Props) {
};

const changeAddress = await bb02.cardanoAddress(network, changeConfig, false);
const drepType: bitbox.CardanoDrepType = 'alwaysAbstain';
const transaction = () => {
switch (txType) {
case 'normal':
Expand Down Expand Up @@ -290,6 +291,31 @@ function CardanoSignTransaction({ bb02 }: Props) {
validityIntervalStart: BigInt(41110811),
allowZeroTTL: false,
};
case 'vote-delegation':
return {
network,
inputs,
outputs: [
{
encodedAddress: changeAddress,
value: BigInt(2741512),
scriptConfig: changeConfig,
},
],
fee: BigInt(191681),
ttl: BigInt(41539125),
certificates: [
{
voteDelegation: {
keypath: "m/1852'/1815'/0'/2/0",
type: drepType,
},
},
],
withdrawals: [],
validityIntervalStart: BigInt(41110811),
allowZeroTTL: false,
};
case 'withdraw-staking-rewards':
return {
network,
Expand Down
22 changes: 21 additions & 1 deletion scripts/build-protos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ fn add_serde_attrs(c: &mut prost_build::Config) {
"shiftcrypto.bitbox02.BTCPubRequest.XPubType.CAPITAL_YPUB",
"serde(rename = \"Ypub\")",
),
// Cardano
(
"shiftcrypto.bitbox02.CardanoNetwork.CardanoMainnet",
"serde(rename = \"mainnet\")",
),
// Cardano
(
"shiftcrypto.bitbox02.CardanoNetwork.CardanoTestnet",
"serde(rename = \"testnet\")",
Expand All @@ -92,6 +92,26 @@ fn add_serde_attrs(c: &mut prost_build::Config) {
"keypath_stake",
"serde(deserialize_with = \"crate::keypath::serde_deserialize\")",
),
(
"shiftcrypto.bitbox02.CardanoSignTransactionRequest.Certificate.VoteDelegation.CardanoDRepType.KEY_HASH",
"serde(rename = \"keyHash\")",
),
(
"shiftcrypto.bitbox02.CardanoSignTransactionRequest.Certificate.VoteDelegation.CardanoDRepType.SCRIPT_HASH",
"serde(rename = \"scriptHash\")",
),
(
"shiftcrypto.bitbox02.CardanoSignTransactionRequest.Certificate.VoteDelegation.CardanoDRepType.ALWAYS_ABSTAIN",
"serde(rename = \"alwaysAbstain\")",
),
(
"shiftcrypto.bitbox02.CardanoSignTransactionRequest.Certificate.VoteDelegation.CardanoDRepType.ALWAYS_NO_CONFIDENCE",
"serde(rename = \"alwaysNoConfidence\")",
),
(
"shiftcrypto.bitbox02.CardanoSignTransactionRequest.Certificate.VoteDelegation.type",
"serde(deserialize_with = \"crate::cardano::serde_deserialize_drep_type\")",
),
];

for (path, attr) in type_attrs {
Expand Down
10 changes: 10 additions & 0 deletions src/cardano.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ where
Ok(network as i32)
}

#[cfg(feature = "wasm")]
pub(crate) fn serde_deserialize_drep_type<'de, D>(deserializer: D) -> Result<i32, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::Deserialize;
let drep_type = pb::cardano_sign_transaction_request::certificate::vote_delegation::CardanoDRepType::deserialize(deserializer)?;
Ok(drep_type as i32)
}

#[cfg(feature = "wasm")]
#[derive(serde::Deserialize)]
pub(crate) struct SerdeScriptConfig(pb::cardano_script_config::Config);
Expand Down
8 changes: 8 additions & 0 deletions src/shiftcrypto.bitbox02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,10 @@ pub mod cardano_sign_transaction_request {
)]
pub keypath: ::prost::alloc::vec::Vec<u32>,
#[prost(enumeration = "vote_delegation::CardanoDRepType", tag = "2")]
#[cfg_attr(
feature = "wasm",
serde(deserialize_with = "crate::cardano::serde_deserialize_drep_type")
)]
pub r#type: i32,
#[prost(bytes = "vec", optional, tag = "3")]
pub drep_credhash: ::core::option::Option<::prost::alloc::vec::Vec<u8>>,
Expand All @@ -1269,9 +1273,13 @@ pub mod cardano_sign_transaction_request {
)]
#[repr(i32)]
pub enum CardanoDRepType {
#[cfg_attr(feature = "wasm", serde(rename = "keyHash"))]
KeyHash = 0,
#[cfg_attr(feature = "wasm", serde(rename = "scriptHash"))]
ScriptHash = 1,
#[cfg_attr(feature = "wasm", serde(rename = "alwaysAbstain"))]
AlwaysAbstain = 2,
#[cfg_attr(feature = "wasm", serde(rename = "alwaysNoConfidence"))]
AlwaysNoConfidence = 3,
}
impl CardanoDRepType {
Expand Down
8 changes: 8 additions & 0 deletions src/wasm/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type CardanoOutput = {
scriptConfig?: CardanoScriptConfig;
assetGroups?: CardanoAssetGroup[];
}
type CardanoDrepType = 'keyHash' | 'scriptHash' | 'alwaysAbstain' | 'alwaysNoConfidence'
type CardanoCertificate =
| {
stakeRegistration: {
Expand All @@ -119,6 +120,13 @@ type CardanoCertificate =
keypath: Keypath
poolKeyhash: Uint8Array
}
}
| {
voteDelegation: {
keypath: Keypath
type: CardanoDrepType
drepCredHash?: Uint8Array
}
};
type CardanoWithdrawal = {
keypath: Keypath;
Expand Down
Loading