Skip to content

Commit

Permalink
Merge branch 'develop' into ruslan/collateral-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vsubhuman committed Mar 17, 2023
2 parents cd52cfd + 056e5ba commit 817f9e7
Show file tree
Hide file tree
Showing 24 changed files with 1,553 additions and 297 deletions.
16 changes: 15 additions & 1 deletion packages/yoroi-connector/example-cardano/index.js
Expand Up @@ -482,6 +482,17 @@ signTx.addEventListener("click", () => {
// add a keyhash input - for ADA held in a Shelley-era normal address (Base, Enterprise, Pointer)
const utxo = utxos[0];

const assets = CardanoWasm.MultiAsset.new();
for (const asset of utxo.assets) {
const policyId = CardanoWasm.ScriptHash.from_hex(asset.policyId);
const policyContent = assets.get(policyId) || CardanoWasm.Assets.new();
policyContent.insert(
CardanoWasm.AssetName.new(Buffer.from(asset.name, 'hex')),
CardanoWasm.BigNum.from_str(asset.amount)
);
assets.insert(policyId, policyContent);
}

const addr = CardanoWasm.Address.from_bech32(utxo.receiver);

const baseAddr = CardanoWasm.BaseAddress.from_address(addr);
Expand All @@ -492,7 +503,10 @@ signTx.addEventListener("click", () => {
CardanoWasm.TransactionHash.from_bytes(hexToBytes(utxo.tx_hash)), // tx hash
utxo.tx_index // index
),
CardanoWasm.Value.new(CardanoWasm.BigNum.from_str(utxo.amount))
CardanoWasm.Value.new_with_assets(
CardanoWasm.BigNum.from_str(utxo.amount),
assets
)
);

const shelleyOutputAddress =
Expand Down
Expand Up @@ -61,15 +61,23 @@ export function addrContainsAccountKey(
const accountKeyString = typeof targetAccountKey === 'string' ? targetAccountKey : Buffer.from(targetAccountKey.to_bytes()).toString('hex');

const asBase = RustModule.WalletV4.BaseAddress.from_address(wasmAddr);
// clean: de-allocate the pointer
wasmAddr.free();

if (asBase != null) {
if (Buffer.from(asBase.stake_cred().to_bytes()).toString('hex') === accountKeyString) {
return true;
}
const isAccountKey = Buffer.from(asBase.stake_cred().to_bytes()).toString('hex') === accountKeyString;
// clean: de-allocate the pointer
asBase.free();
if (isAccountKey) return true;

}

const asPointer = RustModule.WalletV4.PointerAddress.from_address(wasmAddr);
if (asPointer != null) {
// TODO
// clean: de-allocate the pointer
asPointer.free()
}

return acceptTypeMismatch;
}

Expand Down Expand Up @@ -263,20 +271,17 @@ export function certificateToPoolList(
certificateHex: string,
kind: $PropertyType<CertificateInsert, 'Kind'>,
): Array<PoolTuples> {
switch (kind) {
case RustModule.WalletV4.CertificateKind.StakeDeregistration: {
return [];
}
case RustModule.WalletV4.CertificateKind.StakeDelegation: {
const cert = RustModule.WalletV4.StakeDelegation.from_bytes(Buffer.from(certificateHex, 'hex'));
return RustModule.WasmScope(Scope => {
if (kind === Scope.WalletV4.CertificateKind.StakeDeregistration) return []
if (kind === Scope.WalletV4.CertificateKind.StakeDelegation) {
const cert = Scope.WalletV4.StakeDelegation.from_bytes(Buffer.from(certificateHex, 'hex'));
return [
[Buffer.from(cert.pool_keyhash().to_bytes()).toString('hex'), 1]
];
}
default: {
throw new Error(`${nameof(certificateToPoolList)} unexpected certificate kind ${kind}`);
}
}
throw new Error(`${nameof(certificateToPoolList)} unexpected certificate kind ${kind}`);
});
}
export function createCertificate(
Expand All @@ -287,6 +292,7 @@ export function createCertificate(
const credential = RustModule.WalletV4.StakeCredential.from_keyhash(
stakingKey.hash()
);
if (poolRequest == null) {
if (isRegistered) {
return [RustModule.WalletV4.Certificate.new_stake_deregistration(
Expand All @@ -295,6 +301,7 @@ export function createCertificate(
}
return []; // no need to undelegate if no staking key registered
}
const result = [];
if (!isRegistered) {
// if unregistered, need to register first
Expand Down
Expand Up @@ -166,6 +166,86 @@ export async function getAllAddressesForDisplay(
);
}

type AddressWithDerivationPath = {|
+address: string,
+path: Array<number>,
|};

export async function getAllAddressesWithPaths(
publicDeriver: IPublicDeriver<ConceptualWallet>,
): Promise<{|
utxoAddresses: Array<$ReadOnly<AddressWithDerivationPath>>,
accountingAddresses: Array<$ReadOnly<AddressWithDerivationPath>>,
|}> {
const withLevels = asHasLevels<ConceptualWallet>(publicDeriver);
if (!withLevels) {
throw new Error(`${nameof(getAllAddressesWithPaths)} publicDerviver traits missing`);
}
const derivationTables = withLevels.getParent().getDerivationTables();
const deps = Object.freeze({
GetPathWithSpecific,
GetAddress,
GetDerivationSpecific,
});
const depTables = Object.keys(deps)
.map(key => deps[key])
.flatMap(table => getAllSchemaTables(publicDeriver.getDb(), table));

return await raii(
publicDeriver.getDb(),
[
...depTables,
...mapToTables(publicDeriver.getDb(), derivationTables),
],
async dbTx => {
const utxoAddresses = [];
const accountingAddresses = [];
const withUtxos = asGetAllUtxos(publicDeriver);
if (withUtxos != null) {
const addrResponse = await withUtxos.rawGetAllUtxoAddresses(
dbTx,
{
GetPathWithSpecific: deps.GetPathWithSpecific,
GetAddress: deps.GetAddress,
GetDerivationSpecific: deps.GetDerivationSpecific,
},
undefined,
derivationTables,
);
for (const family of addrResponse) {
for (const addr of family.addrs) {
utxoAddresses.push({ address: addr.Hash, path: family.addressing.path });
}
}
}
const withAccounting = asGetAllAccounting(publicDeriver);
if (withAccounting != null) {
const addrResponse = await withAccounting.rawGetAllAccountingAddresses(
dbTx,
{
GetPathWithSpecific: deps.GetPathWithSpecific,
GetAddress: deps.GetAddress,
GetDerivationSpecific: deps.GetDerivationSpecific,
},
undefined,
derivationTables,
);
for (const family of addrResponse) {
for (const addr of family.addrs) {
accountingAddresses.push({ address: addr.Hash, path: family.addressing.path });
}
}
}

return {
utxoAddresses,
accountingAddresses,
};

},
);
}

export async function rawGetAddressRowsForWallet(
tx: lf$Transaction,
deps: {|
Expand Down
Expand Up @@ -328,7 +328,7 @@ export function normalizeToAddress(
}

export function isJormungandrAddress(
kind: CoreAddressT
type: CoreAddressT
): boolean {
// note: excluding legacy byron addresses
const types = [
Expand All @@ -338,7 +338,7 @@ export function isJormungandrAddress(
CoreAddressTypes.JORMUNGANDR_MULTISIG,
];

return kind in types;
return types.some(t => t === type);
}

export function isErgoAddress(
Expand Down

0 comments on commit 817f9e7

Please sign in to comment.