Skip to content

Commit

Permalink
updated bech32 decoders and tests to add ledger
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Kuroda committed Jan 12, 2025
1 parent ea61e44 commit 4af7c8c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
22 changes: 19 additions & 3 deletions ironfish/src/wallet/exporter/encoders/bech32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class Bech32Encoder implements AccountEncoder {
[1, decoderV1],
[2, decoderV2],
[3, decoderV3],
[4, decoderV4],
])

/**
Expand Down Expand Up @@ -62,6 +63,8 @@ export class Bech32Encoder implements AccountEncoder {
bw.writeBytes(Buffer.from(value.proofAuthorizingKey, 'hex'))
}

bw.writeU8(Number(!!value.ledger))

return Bech32m.encode(bw.render().toString('hex'), BECH32_ACCOUNT_PREFIX)
}

Expand Down Expand Up @@ -126,8 +129,8 @@ export class Bech32Encoder implements AccountEncoder {
size += 1 // proofAuthorizingKey byte
if (value.proofAuthorizingKey) {
size += KEY_LENGTH
size += 1 // ledger
}
size += 1 // ledger

return size
}
Expand All @@ -154,7 +157,6 @@ function decoderV1(
const sequence = reader.readU32()
createdAt = { hash, sequence }
}
const ledger = reader.readU8() === 1

return {
version: ACCOUNT_SCHEMA_VERSION,
Expand All @@ -166,7 +168,7 @@ function decoderV1(
publicAddress,
createdAt,
proofAuthorizingKey: null,
ledger,
ledger: false,
}
}

Expand Down Expand Up @@ -207,3 +209,17 @@ function decoderV3(
proofAuthorizingKey,
}
}

function decoderV4(
reader: bufio.BufferReader,
options?: AccountDecodingOptions,
): AccountImport {
const accountImport = decoderV3(reader, options)

const ledger = reader.readU8() === 1

return {
...accountImport,
ledger,
}
}
4 changes: 2 additions & 2 deletions ironfish/src/wallet/exporter/encoders/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type AccountEncodedJSON = {
publicKeyPackage: string
}
proofAuthorizingKey?: string | null
ledger: boolean
ledger: boolean | undefined
}

const AccountEncodedJSONSchema: yup.ObjectSchema<AccountEncodedJSON> = yup
Expand Down Expand Up @@ -97,7 +97,7 @@ const AccountEncodedJSONSchema: yup.ObjectSchema<AccountEncodedJSON> = yup
.optional()
.default(undefined),
proofAuthorizingKey: yup.string().nullable().optional(),
ledger: yup.boolean().defined(),
ledger: yup.boolean().optional(),
})
.defined()

Expand Down

0 comments on commit 4af7c8c

Please sign in to comment.