Skip to content

Commit

Permalink
chore: upgrade CSL to v13, drop isByron param in deriveAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
slowbackspace committed Jan 10, 2025
1 parent a452d9b commit cbd83bc
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 76 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
7 changes: 7 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Dropped Byron support from `deriveAddress` util
- `isTestnet` parameter changed to `network` (mainnet/preprod/preview)
- Updated Cardano-Serialization-Lib to v13
- Updated Blockfrost OpenAPI to 0.1.71

## [5.7.0] - 2024-11-05

### Added
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"coverage-ci": "vitest run --coverage"
},
"dependencies": {
"@blockfrost/openapi": "0.1.70-beta.0",
"@emurgo/cardano-serialization-lib-nodejs": "^11.5.0",
"@blockfrost/openapi": "0.1.71",
"@emurgo/cardano-serialization-lib-nodejs": "^13.2.1",
"@emurgo/cip14-js": "3.0.1",
"bottleneck": "^2.19.5",
"form-data": "^4.0.0",
Expand Down
49 changes: 15 additions & 34 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import {
Bip32PublicKey,
BaseAddress,
NetworkInfo,
StakeCredential,
Credential,
RewardAddress,
ByronAddress,
} from '@emurgo/cardano-serialization-lib-nodejs';
import AssetFingerprint from '@emurgo/cip14-js';
import { ParseAssetResult } from '../types/utils';
Expand Down Expand Up @@ -43,28 +42,28 @@ export const deriveAddress = (
accountPublicKey: string,
role: number,
addressIndex: number,
isTestnet: boolean,
isByron?: boolean,
network: 'mainnet' | 'preview' | 'preprod',
): { address: string; path: [number, number] } => {
const accountKey = Bip32PublicKey.from_bytes(
Buffer.from(accountPublicKey, 'hex'),
);
const utxoPubKey = accountKey.derive(role).derive(addressIndex);
const mainStakeKey = accountKey.derive(2).derive(0);

const testnetNetworkInfo = NetworkInfo.testnet();
const mainnetNetworkInfo = NetworkInfo.mainnet();
const networkInfo: Record<typeof network, NetworkInfo> = {
mainnet: NetworkInfo.mainnet(),
preprod: NetworkInfo.testnet_preprod(),
preview: NetworkInfo.testnet_preview(),
};

const networkId = isTestnet
? testnetNetworkInfo.network_id()
: mainnetNetworkInfo.network_id();
const networkId = networkInfo[network].network_id();

const utxoPubRawKey = utxoPubKey.to_raw_key();
const utxoPubKeyHash = utxoPubRawKey.hash();
const mainStakeRawKey = mainStakeKey.to_raw_key();
const mainStakeKeyHash = mainStakeRawKey.hash();
const utxoStakeCred = StakeCredential.from_keyhash(utxoPubKeyHash);
const mainStakeCred = StakeCredential.from_keyhash(mainStakeKeyHash);
const utxoStakeCred = Credential.from_keyhash(utxoPubKeyHash);
const mainStakeCred = Credential.from_keyhash(mainStakeKeyHash);
const baseAddr = BaseAddress.new(networkId, utxoStakeCred, mainStakeCred);

utxoStakeCred.free();
Expand All @@ -77,11 +76,11 @@ export const deriveAddress = (
const baseAddrBech32 = baseAddr.to_address().to_bech32();
baseAddr.free();

if (role === 2 && !isByron) {
if (role === 2) {
const addressSpecificStakeKey = accountKey.derive(2).derive(addressIndex);
const stakeRawKey = addressSpecificStakeKey.to_raw_key();
const stakeKeyHash = stakeRawKey.hash();
const stakeCred = StakeCredential.from_keyhash(stakeKeyHash);
const stakeCred = Credential.from_keyhash(stakeKeyHash);
// always return stake address
const rewardAddr = RewardAddress.new(networkId, stakeCred);
const address = rewardAddr.to_address();
Expand All @@ -100,31 +99,13 @@ export const deriveAddress = (
};
}

if (isByron) {
const protocolMagic = isTestnet
? testnetNetworkInfo.protocol_magic()
: mainnetNetworkInfo.protocol_magic();

const byronAddress = ByronAddress.icarus_from_key(
utxoPubKey,
protocolMagic,
);

const byronAddrBase58 = byronAddress.to_base58();
byronAddress.free();

return {
address: byronAddrBase58,
path: [role, addressIndex],
};
}

mainStakeKey.free();
utxoPubKey.free();
accountKey.free();

testnetNetworkInfo.free();
mainnetNetworkInfo.free();
networkInfo['mainnet'].free();
networkInfo['preprod'].free();
networkInfo['preview'].free();

return {
address: baseAddrBech32,
Expand Down
57 changes: 30 additions & 27 deletions test/fixtures/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ export const deriveAddressFixtures = [
'7ec9738746cb4708df52a455b43aa3fdee8955abaf37f68ffc79bb84fbf9e1b39d77e2deb9749faf890ff8326d350ed3fd0e4aa271b35cad063692af87102152',
role: 0,
index: 1,
isTestnet: false,
isByron: false,
network: 'mainnet',
response: {
address:
'addr1qy535472n2ctu3x55v03zmm9jnz54grqu3sueap9pnk4xys49ucjdfty5p5qlw5qe28v9k988stffc2g0hx2xx86a2dq5u58qk',
Expand All @@ -262,8 +261,7 @@ export const deriveAddressFixtures = [
'7ec9738746cb4708df52a455b43aa3fdee8955abaf37f68ffc79bb84fbf9e1b39d77e2deb9749faf890ff8326d350ed3fd0e4aa271b35cad063692af87102152',
role: 2,
index: 0,
isTestnet: false,
isByron: false,
network: 'mainnet',
response: {
address: 'stake1uy2j7vfx54j2q6q0h2qv4rkzmznnc955u9y8mn9rrraw4xspqg6tp',
path: [2, 0],
Expand All @@ -274,8 +272,7 @@ export const deriveAddressFixtures = [
'6d17587575a3b4f0f86ebad3977e8f7e4981faa863eccf5c1467065c74fe3435943769446dd290d103fb3d360128e86de4b47faea73ffb0900c94c6a61ef9ea2',
role: 0,
index: 0,
isTestnet: false,
isByron: false,
network: 'mainnet',
response: {
address:
'addr1q8u5ktsj5zsmhvwv0ep9zuhfu39x3wyt9wxjnsn3cagsyy59ckxhkvuc5xj49rw6zrp443wlygmhv8gwcu38jk6ms6usrmcafl',
Expand All @@ -287,8 +284,7 @@ export const deriveAddressFixtures = [
'6d17587575a3b4f0f86ebad3977e8f7e4981faa863eccf5c1467065c74fe3435943769446dd290d103fb3d360128e86de4b47faea73ffb0900c94c6a61ef9ea2',
role: 0,
index: 1,
isByron: false,
isTestnet: false,
network: 'mainnet',
response: {
address:
'addr1qxnthyxq8x9lv95h74k5av3sy3yzljr56ttxu4lggv8qstv9ckxhkvuc5xj49rw6zrp443wlygmhv8gwcu38jk6ms6us8mueja',
Expand All @@ -300,8 +296,7 @@ export const deriveAddressFixtures = [
'6d17587575a3b4f0f86ebad3977e8f7e4981faa863eccf5c1467065c74fe3435943769446dd290d103fb3d360128e86de4b47faea73ffb0900c94c6a61ef9ea2',
role: 2,
index: 0,
isByron: false,
isTestnet: false,
network: 'mainnet',
response: {
address: 'stake1uxzutrtmxwv2rf2j3hdpps66ch0jydmkr58vwgnetddcdwg32u4rc',
path: [2, 0],
Expand All @@ -312,8 +307,7 @@ export const deriveAddressFixtures = [
'6d17587575a3b4f0f86ebad3977e8f7e4981faa863eccf5c1467065c74fe3435943769446dd290d103fb3d360128e86de4b47faea73ffb0900c94c6a61ef9ea2',
role: 2,
index: 1,
isTestnet: false,
isByron: false,
network: 'mainnet',
response: {
address: 'stake1uyq2806du47kxcvr2504vuvmngauy5mhv3d45d2mukek3ugj46jvz',
path: [2, 1],
Expand All @@ -324,8 +318,7 @@ export const deriveAddressFixtures = [
'7ec9738746cb4708df52a455b43aa3fdee8955abaf37f68ffc79bb84fbf9e1b39d77e2deb9749faf890ff8326d350ed3fd0e4aa271b35cad063692af87102152',
role: 1,
index: 3,
isByron: false,
isTestnet: true,
network: 'preview',
response: {
address:
'addr_test1qqtpyxyh5j023fq88xhj862guwrymhwjadt5czqvumpv02s49ucjdfty5p5qlw5qe28v9k988stffc2g0hx2xx86a2dqhvnrk6',
Expand All @@ -337,8 +330,7 @@ export const deriveAddressFixtures = [
'7ec9738746cb4708df52a455b43aa3fdee8955abaf37f68ffc79bb84fbf9e1b39d77e2deb9749faf890ff8326d350ed3fd0e4aa271b35cad063692af87102152',
role: 2,
index: 0,
isByron: false,
isTestnet: true,
network: 'preview',
response: {
address:
'stake_test1uq2j7vfx54j2q6q0h2qv4rkzmznnc955u9y8mn9rrraw4xsx2zc0u',
Expand All @@ -350,8 +342,7 @@ export const deriveAddressFixtures = [
'7ec9738746cb4708df52a455b43aa3fdee8955abaf37f68ffc79bb84fbf9e1b39d77e2deb9749faf890ff8326d350ed3fd0e4aa271b35cad063692af87102152',
role: 2,
index: 1,
isByron: false,
isTestnet: true,
network: 'preview',
response: {
address:
'stake_test1uq8peju6cs3k03s8qgmuxvs7za456jnpflk08epc4x5m3ug4ydclp',
Expand All @@ -361,24 +352,36 @@ export const deriveAddressFixtures = [
{
publicKey:
'7ec9738746cb4708df52a455b43aa3fdee8955abaf37f68ffc79bb84fbf9e1b39d77e2deb9749faf890ff8326d350ed3fd0e4aa271b35cad063692af87102152',
role: 0,
index: 1,
isByron: true,
isTestnet: false,
role: 1,
index: 3,
network: 'preprod',
response: {
address: 'Ae2tdPwUPEZL5AWYBw6qiS9sqRyJyQQaYSxWznXvestdemMjetAU6CyFohr',
path: [0, 1],
address:
'addr_test1qqtpyxyh5j023fq88xhj862guwrymhwjadt5czqvumpv02s49ucjdfty5p5qlw5qe28v9k988stffc2g0hx2xx86a2dqhvnrk6',
path: [1, 3],
},
},
{
publicKey:
'7ec9738746cb4708df52a455b43aa3fdee8955abaf37f68ffc79bb84fbf9e1b39d77e2deb9749faf890ff8326d350ed3fd0e4aa271b35cad063692af87102152',
role: 2,
index: 0,
network: 'preprod',
response: {
address:
'stake_test1uq2j7vfx54j2q6q0h2qv4rkzmznnc955u9y8mn9rrraw4xsx2zc0u',
path: [2, 0],
},
},
{
publicKey:
'7ec9738746cb4708df52a455b43aa3fdee8955abaf37f68ffc79bb84fbf9e1b39d77e2deb9749faf890ff8326d350ed3fd0e4aa271b35cad063692af87102152',
role: 2,
index: 1,
isByron: true,
isTestnet: false,
network: 'preprod',
response: {
address: 'Ae2tdPwUPEZ14JFcncepdpDj1TwgSX8DDDXYfaoZUQTuqTBRbsnyYcYCDUT',
address:
'stake_test1uq8peju6cs3k03s8qgmuxvs7za456jnpflk08epc4x5m3ug4ydclp',
path: [2, 1],
},
},
Expand Down
5 changes: 2 additions & 3 deletions test/tests/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ import { SignatureVerificationError } from '../../../src/utils/errors';

describe('helpers', () => {
deriveAddressFixtures.forEach(fixture => {
test(`deriveAddress - publicKey: ${fixture.publicKey} role: ${fixture.role} index: ${fixture.index} isByron: ${fixture.isByron}`, () => {
test(`deriveAddress - publicKey: ${fixture.publicKey} role: ${fixture.role} index: ${fixture.index} network: ${fixture.network}`, () => {
const response = deriveAddress(
fixture.publicKey,
fixture.role,
fixture.index,
fixture.isTestnet,
fixture.isByron,
fixture.network,
);
expect(response).toStrictEqual(fixture.response);
});
Expand Down
20 changes: 10 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ __metadata:
version: 0.0.0-use.local
resolution: "@blockfrost/blockfrost-js@workspace:."
dependencies:
"@blockfrost/openapi": "npm:0.1.70-beta.0"
"@emurgo/cardano-serialization-lib-nodejs": "npm:^11.5.0"
"@blockfrost/openapi": "npm:0.1.71"
"@emurgo/cardano-serialization-lib-nodejs": "npm:^13.2.1"
"@emurgo/cip14-js": "npm:3.0.1"
"@types/json-bigint": "npm:^1.0.2"
"@types/node": "npm:^18"
Expand Down Expand Up @@ -63,22 +63,22 @@ __metadata:
languageName: unknown
linkType: soft

"@blockfrost/openapi@npm:0.1.70-beta.0":
version: 0.1.70-beta.0
resolution: "@blockfrost/openapi@npm:0.1.70-beta.0"
"@blockfrost/openapi@npm:0.1.71":
version: 0.1.71
resolution: "@blockfrost/openapi@npm:0.1.71"
dependencies:
ajv: "npm:^8.12.0"
cbor: "npm:^9.0.1"
rimraf: "npm:6.0.1"
yaml: "npm:^2.3.4"
checksum: 4171f7c75f08763d03f67c5fac38e8dcb93e569ef72718afd4025f7a201e456a8a0ef732262ee6ae9d26596536d2037ecec92c9906894589211f06019a798563
checksum: 34c7c79aad3302e8d6f434020a9e574c05cb1929b8c48efe04b297c55ef61f7ed3316a587dcd1af7818a0b154998e3afd8677da17b97676f19144de103c4a4ed
languageName: node
linkType: hard

"@emurgo/cardano-serialization-lib-nodejs@npm:^11.5.0":
version: 11.5.0
resolution: "@emurgo/cardano-serialization-lib-nodejs@npm:11.5.0"
checksum: 3fff8448001c6d70807ef8e1a80a663ef60381e55bfd26c0f1b644e096895da7298fb991ac86670d4c5aee2e3e417e44ac80ab59080f7af107d8fa89906f9075
"@emurgo/cardano-serialization-lib-nodejs@npm:^13.2.1":
version: 13.2.1
resolution: "@emurgo/cardano-serialization-lib-nodejs@npm:13.2.1"
checksum: 6572ac1baebe1032a0e1ba701e8be0dc74d00ff332f1b58622851532d9bded9d89b797d862551842fce420ece05b67ced94c9edad33e7f124e48d74c28b0ac4e
languageName: node
linkType: hard

Expand Down

0 comments on commit cbd83bc

Please sign in to comment.