This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add bip122:p2wpkh account support (#294)
* feat: add bip122:p2wpkh account support * refactor: add and use asInternalAccountStruct for internal accounts
- Loading branch information
Showing
9 changed files
with
156 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './types'; |
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,41 @@ | ||
import { BtcP2wpkhAddressStruct } from './types'; | ||
|
||
describe('types', () => { | ||
describe('BtcP2wpkhAddressStruct', () => { | ||
const errorPrefix = 'Could not decode P2WPKH address'; | ||
|
||
it.each([ | ||
'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4', | ||
'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx', | ||
])('is valid address; %s', (address) => { | ||
expect(() => BtcP2wpkhAddressStruct.assert(address)).not.toThrow(); | ||
}); | ||
|
||
it.each([ | ||
// Too short | ||
'', | ||
'bc1q', | ||
// Must have at least 6 characters after separator '1' | ||
'bc1q000', | ||
])('throws an error if address is too short: %s', (address) => { | ||
expect(() => BtcP2wpkhAddressStruct.assert(address)).toThrow( | ||
`${errorPrefix}: ${address} too short`, | ||
); | ||
}); | ||
|
||
it('throws an error if address is too long', () => { | ||
const address = | ||
'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4w508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4w508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4'; | ||
expect(() => BtcP2wpkhAddressStruct.assert(address)).toThrow( | ||
`${errorPrefix}: Exceeds length limit`, | ||
); | ||
}); | ||
|
||
it('throws an error if there no seperator', () => { | ||
const address = 'bc0qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4'; | ||
expect(() => BtcP2wpkhAddressStruct.assert(address)).toThrow( | ||
`${errorPrefix}: No separator character for ${address}`, | ||
); | ||
}); | ||
}); | ||
}); |
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,51 @@ | ||
import { bech32 } from 'bech32'; | ||
import type { Infer } from 'superstruct'; | ||
import { object, string, array, enums, literal, refine } from 'superstruct'; | ||
|
||
import { BaseAccount } from '../base-types'; | ||
|
||
export const BtcP2wpkhAddressStruct = refine( | ||
string(), | ||
'BtcP2wpkhAddressStruct', | ||
(address: string) => { | ||
try { | ||
bech32.decode(address); | ||
} catch (error) { | ||
return new Error( | ||
`Could not decode P2WPKH address: ${(error as Error).message}`, | ||
); | ||
} | ||
return true; | ||
}, | ||
); | ||
|
||
/** | ||
* Supported Bitcoin methods. | ||
*/ | ||
export enum BtcMethod { | ||
// General transaction methods | ||
SendTransaction = 'btc_sendTransaction', | ||
} | ||
|
||
/** | ||
* Supported Bitcoin account types. | ||
*/ | ||
export enum BtcAccountType { | ||
P2wpkh = 'bip122:p2wpkh', | ||
} | ||
|
||
export const BtcP2wpkhAccountStruct = object({ | ||
...BaseAccount, | ||
|
||
/** | ||
* Account type. | ||
*/ | ||
type: literal(`${BtcAccountType.P2wpkh}`), | ||
|
||
/** | ||
* Account supported methods. | ||
*/ | ||
methods: array(enums([`${BtcMethod.SendTransaction}`])), | ||
}); | ||
|
||
export type BtcP2wpkhAccount = Infer<typeof BtcP2wpkhAccountStruct>; |
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