-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add compat and t-addr bech32 encoding
- Loading branch information
Showing
14 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { fromBech32, toBech32 } from './format/convert.js'; | ||
import { Inner } from './format/inner.js'; | ||
import { Prefixes } from './format/prefix.js'; | ||
|
||
const innerName = Inner.penumbracompat1; | ||
const prefix = Prefixes.penumbracompat1; | ||
|
||
export const bech32CompatAddress = ({ [innerName]: bytes }: { [innerName]: Uint8Array }) => | ||
toBech32(bytes, prefix); | ||
|
||
export const compatAddressFromBech32 = (penumbracompat1: string): { [innerName]: Uint8Array } => ({ | ||
[innerName]: fromBech32(penumbracompat1 as `${typeof prefix}1${string}`, prefix), | ||
}); | ||
|
||
export const isCompatAddress = (check: string): check is `${typeof prefix}1${string}` => { | ||
try { | ||
compatAddressFromBech32(check); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
}; | ||
|
||
export { PENUMBRA_BECH32M_ADDRESS_LENGTH, PENUMBRA_BECH32M_ADDRESS_PREFIX } from './index.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { describe } from 'vitest'; | ||
import { generateTests } from './util/generate-tests.js'; | ||
import { bech32CompatAddress, compatAddressFromBech32 } from '../penumbracompat1.js'; | ||
import { Prefixes } from '../format/prefix.js'; | ||
import { Inner } from '../format/inner.js'; | ||
|
||
describe('compat address conversion', () => { | ||
const okInner = new Uint8Array([ | ||
175, 182, 158, 255, 239, 16, 245, 221, 208, 117, 160, 44, 235, 175, 198, 0, 6, 216, 6, 143, 192, | ||
155, 159, 103, 97, 103, 136, 5, 78, 209, 17, 200, 68, 220, 182, 45, 20, 246, 181, 16, 117, 182, | ||
46, 141, 74, 101, 196, 86, 185, 124, 206, 253, 195, 57, 224, 34, 210, 22, 123, 246, 136, 10, | ||
208, 159, 24, 235, 148, 153, 211, 7, 137, 198, 158, 226, 221, 22, 208, 152, 246, 247, | ||
]); | ||
const okBech32 = | ||
'penumbracompat1147mfall0zr6am5r45qkwht7xqqrdsp50czde7empv7yq2nk3z8yyfh9k9520ddgswkmzar22vhz9dwtuem7uxw0qytfpv7lk3q9dp8ccaw2fn5c838rfackazmgf3ahhwqq0da'; | ||
|
||
generateTests( | ||
Prefixes.penumbracompat1, | ||
Inner.penumbracompat1, | ||
okInner, | ||
okBech32, | ||
bech32CompatAddress, | ||
compatAddressFromBech32, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { describe } from 'vitest'; | ||
import { generateTests } from './util/generate-tests.js'; | ||
import { bech32TransparentAddress, transparentAddressFromBech32 } from '../tpenumbra.js'; | ||
import { Prefixes } from '../format/prefix.js'; | ||
import { Inner } from '../format/inner.js'; | ||
|
||
describe('transparent address conversion', () => { | ||
const okInner = new Uint8Array([ | ||
102, 236, 169, 166, 203, 152, 194, 89, 236, 246, 59, 69, 221, 32, 49, 49, 83, 29, 119, 117, 124, | ||
201, 194, 156, 219, 251, 137, 202, 157, 235, 1, 15, | ||
]); | ||
const okBech32 = 'tpenumbra1vmk2nfktnrp9nm8k8dza6gp3x9f36am40nyu98xmlwyu480tqy8sr3jfzd'; | ||
|
||
generateTests( | ||
Prefixes.tpenumbra, | ||
Inner.tpenumbra, | ||
okInner, | ||
okBech32, | ||
bech32TransparentAddress, | ||
transparentAddressFromBech32, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { fromBech32, toBech32 } from './format/convert.js'; | ||
import { Inner } from './format/inner.js'; | ||
import { Prefixes } from './format/prefix.js'; | ||
|
||
const innerName = Inner.tpenumbra; | ||
const prefix = Prefixes.tpenumbra; | ||
|
||
export const bech32TransparentAddress = ({ [innerName]: bytes }: { [innerName]: Uint8Array }) => | ||
toBech32(bytes, prefix); | ||
|
||
export const transparentAddressFromBech32 = (penumbra1: string): { [innerName]: Uint8Array } => ({ | ||
[innerName]: fromBech32(penumbra1 as `${typeof prefix}1${string}`, prefix), | ||
}); | ||
|
||
export const isAddress = (check: string): check is `${typeof prefix}1${string}` => { | ||
try { | ||
transparentAddressFromBech32(check); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
}; | ||
|
||
export { | ||
PENUMBRA_BECH32M_TRANSPARENT_LENGTH, | ||
PENUMBRA_BECH32M_TRANSPARENT_PREFIX, | ||
} from './index.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import { Address } from '@penumbra-zone/protobuf/penumbra/core/keys/v1/keys_pb'; | ||
import { addressFromBech32m } from '@penumbra-zone/bech32m/penumbra'; | ||
import { compatAddressFromBech32, isCompatAddress } from '@penumbra-zone/bech32m/penumbracompat1'; | ||
|
||
export const parseIntoAddr = (addrStr: string): Address => { | ||
if (isCompatAddress(addrStr)) { | ||
return new Address(compatAddressFromBech32(addrStr)); | ||
} | ||
return new Address(addressFromBech32m(addrStr)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters