generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds Solana account type to keyring API (#82)
We are adding the `SolAccountType` to support Solana account creation. --------- Co-authored-by: Charly Chevalier <[email protected]>
- Loading branch information
Showing
8 changed files
with
115 additions
and
5 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
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,7 @@ | ||
import type { SolDataAccount } from './types'; | ||
import type { KeyringAccount } from '../api'; | ||
import type { Extends } from '../utils'; | ||
import { expectTrue } from '../utils'; | ||
|
||
// `SolDataAccount` extends `KeyringAccount` | ||
expectTrue<Extends<SolDataAccount, KeyringAccount>>(); |
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,28 @@ | ||
import { SolAddressStruct } from './types'; | ||
|
||
describe('types', () => { | ||
describe('SolAddressStruct', () => { | ||
it.each([ | ||
'7EcDhSYGxXyscszYEp35KHN8vvw3svAuLKTzXwCFLtV', | ||
'A5R99q8qeyUhgwYAdp5h8pAD1iteVjCzpCv7G6JRZLaQ', | ||
])('is valid address: %s', (address) => { | ||
expect(() => SolAddressStruct.assert(address)).not.toThrow(); | ||
}); | ||
|
||
it.each([ | ||
// Invalid lengths, too long (45 chars) | ||
'7EcDhSYGxXyscszYEp35KHN8vvw3svAuLKTzXwCFLtV11', | ||
// Too short (31 chars) | ||
'7EcDhSYGxXyscszYEp35KHN8vvw', | ||
// Empty or invalid input | ||
'', | ||
// Eth style address | ||
'0x1234', | ||
'not-an-address', | ||
])('rejects invalid address: %s', (address) => { | ||
expect(() => SolAddressStruct.assert(address)).toThrow( | ||
`Expected a value of type \`SolAddress\`, but received: \`"${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,44 @@ | ||
import type { Infer } from '@metamask/superstruct'; | ||
import { array, enums, literal } from '@metamask/superstruct'; | ||
|
||
import { KeyringAccountStruct, SolAccountType } from '../api'; | ||
import { object, definePattern } from '../superstruct'; | ||
|
||
/** | ||
* Solana addresses are represented in the format of a 256-bit ed25519 public key and | ||
* are encoded using base58. | ||
* They are usually 32 to 44 characters long. | ||
*/ | ||
export const SolAddressStruct = definePattern( | ||
'SolAddress', | ||
/^[1-9A-HJ-NP-Za-km-z]{32,44}$/iu, | ||
); | ||
|
||
/** | ||
* Supported Solana methods. | ||
*/ | ||
export enum SolMethod { | ||
// General transaction methods | ||
SendAndConfirmTransaction = 'sendAndConfirmTransaction', | ||
} | ||
|
||
export const SolDataAccountStruct = object({ | ||
...KeyringAccountStruct.schema, | ||
|
||
/** | ||
* Account address. | ||
*/ | ||
address: SolAddressStruct, | ||
|
||
/** | ||
* Account type. | ||
*/ | ||
type: literal(`${SolAccountType.DataAccount}`), | ||
|
||
/** | ||
* Account supported methods. | ||
*/ | ||
methods: array(enums([`${SolMethod.SendAndConfirmTransaction}`])), | ||
}); | ||
|
||
export type SolDataAccount = Infer<typeof SolDataAccountStruct>; |