Skip to content

Commit eb4173a

Browse files
committed
Cardano: Add vote delegation to node api
Added vote delegation to node api and wasm generation. Signed-off-by: RostarMarek <[email protected]>
1 parent dc55eac commit eb4173a

File tree

5 files changed

+75
-3
lines changed

5 files changed

+75
-3
lines changed

sandbox/src/Cardano.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,11 @@ function CardanoAddress({ bb02 }: Props) {
133133
<ShowError err={err} />
134134
</form>
135135
</div>
136-
137136
);
138137
}
139138

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

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

178178
const changeAddress = await bb02.cardanoAddress(network, changeConfig, false);
179+
const drepType: bitbox.CardanoDrepType = 'alwaysAbstain';
179180
const transaction = () => {
180181
switch (txType) {
181182
case 'normal':
@@ -290,6 +291,31 @@ function CardanoSignTransaction({ bb02 }: Props) {
290291
validityIntervalStart: BigInt(41110811),
291292
allowZeroTTL: false,
292293
};
294+
case 'vote-delegation':
295+
return {
296+
network,
297+
inputs,
298+
outputs: [
299+
{
300+
encodedAddress: changeAddress,
301+
value: BigInt(2741512),
302+
scriptConfig: changeConfig,
303+
},
304+
],
305+
fee: BigInt(191681),
306+
ttl: BigInt(41539125),
307+
certificates: [
308+
{
309+
voteDelegation: {
310+
keypath: "m/1852'/1815'/0'/2/0",
311+
type: drepType,
312+
},
313+
},
314+
],
315+
withdrawals: [],
316+
validityIntervalStart: BigInt(41110811),
317+
allowZeroTTL: false,
318+
};
293319
case 'withdraw-staking-rewards':
294320
return {
295321
network,

scripts/build-protos.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ fn add_serde_attrs(c: &mut prost_build::Config) {
6767
"shiftcrypto.bitbox02.BTCPubRequest.XPubType.CAPITAL_YPUB",
6868
"serde(rename = \"Ypub\")",
6969
),
70+
// Cardano
7071
(
7172
"shiftcrypto.bitbox02.CardanoNetwork.CardanoMainnet",
7273
"serde(rename = \"mainnet\")",
7374
),
74-
// Cardano
7575
(
7676
"shiftcrypto.bitbox02.CardanoNetwork.CardanoTestnet",
7777
"serde(rename = \"testnet\")",
@@ -92,6 +92,26 @@ fn add_serde_attrs(c: &mut prost_build::Config) {
9292
"keypath_stake",
9393
"serde(deserialize_with = \"crate::keypath::serde_deserialize\")",
9494
),
95+
(
96+
"shiftcrypto.bitbox02.CardanoSignTransactionRequest.Certificate.VoteDelegation.CardanoDRepType.KEY_HASH",
97+
"serde(rename = \"keyHash\")",
98+
),
99+
(
100+
"shiftcrypto.bitbox02.CardanoSignTransactionRequest.Certificate.VoteDelegation.CardanoDRepType.SCRIPT_HASH",
101+
"serde(rename = \"scriptHash\")",
102+
),
103+
(
104+
"shiftcrypto.bitbox02.CardanoSignTransactionRequest.Certificate.VoteDelegation.CardanoDRepType.ALWAYS_ABSTAIN",
105+
"serde(rename = \"alwaysAbstain\")",
106+
),
107+
(
108+
"shiftcrypto.bitbox02.CardanoSignTransactionRequest.Certificate.VoteDelegation.CardanoDRepType.ALWAYS_NO_CONFIDENCE",
109+
"serde(rename = \"alwaysNoConfidence\")",
110+
),
111+
(
112+
"shiftcrypto.bitbox02.CardanoSignTransactionRequest.Certificate.VoteDelegation.type",
113+
"serde(deserialize_with = \"crate::cardano::serde_deserialize_drep_type\")",
114+
),
95115
];
96116

97117
for (path, attr) in type_attrs {

src/cardano.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ where
1515
Ok(network as i32)
1616
}
1717

18+
#[cfg(feature = "wasm")]
19+
pub(crate) fn serde_deserialize_drep_type<'de, D>(deserializer: D) -> Result<i32, D::Error>
20+
where
21+
D: serde::Deserializer<'de>,
22+
{
23+
use serde::Deserialize;
24+
let drep_type = pb::cardano_sign_transaction_request::certificate::vote_delegation::CardanoDRepType::deserialize(deserializer)?;
25+
Ok(drep_type as i32)
26+
}
27+
1828
#[cfg(feature = "wasm")]
1929
#[derive(serde::Deserialize)]
2030
pub(crate) struct SerdeScriptConfig(pb::cardano_script_config::Config);

src/shiftcrypto.bitbox02.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,10 @@ pub mod cardano_sign_transaction_request {
12481248
)]
12491249
pub keypath: ::prost::alloc::vec::Vec<u32>,
12501250
#[prost(enumeration = "vote_delegation::CardanoDRepType", tag = "2")]
1251+
#[cfg_attr(
1252+
feature = "wasm",
1253+
serde(deserialize_with = "crate::cardano::serde_deserialize_drep_type")
1254+
)]
12511255
pub r#type: i32,
12521256
#[prost(bytes = "vec", optional, tag = "3")]
12531257
pub drep_credhash: ::core::option::Option<::prost::alloc::vec::Vec<u8>>,
@@ -1269,9 +1273,13 @@ pub mod cardano_sign_transaction_request {
12691273
)]
12701274
#[repr(i32)]
12711275
pub enum CardanoDRepType {
1276+
#[cfg_attr(feature = "wasm", serde(rename = "keyHash"))]
12721277
KeyHash = 0,
1278+
#[cfg_attr(feature = "wasm", serde(rename = "scriptHash"))]
12731279
ScriptHash = 1,
1280+
#[cfg_attr(feature = "wasm", serde(rename = "alwaysAbstain"))]
12741281
AlwaysAbstain = 2,
1282+
#[cfg_attr(feature = "wasm", serde(rename = "alwaysNoConfidence"))]
12751283
AlwaysNoConfidence = 3,
12761284
}
12771285
impl CardanoDRepType {

src/wasm/types.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ type CardanoOutput = {
103103
scriptConfig?: CardanoScriptConfig;
104104
assetGroups?: CardanoAssetGroup[];
105105
}
106+
type CardanoDrepType = 'keyHash' | 'scriptHash' | 'alwaysAbstain' | 'alwaysNoConfidence'
106107
type CardanoCertificate =
107108
| {
108109
stakeRegistration: {
@@ -119,6 +120,13 @@ type CardanoCertificate =
119120
keypath: Keypath
120121
poolKeyhash: Uint8Array
121122
}
123+
}
124+
| {
125+
voteDelegation: {
126+
keypath: Keypath
127+
type: CardanoDrepType
128+
drepCredHash?: Uint8Array
129+
}
122130
};
123131
type CardanoWithdrawal = {
124132
keypath: Keypath;

0 commit comments

Comments
 (0)