-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17ea4a6
commit 7617bf2
Showing
11 changed files
with
370 additions
and
224 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 |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
"@leather.io/models": "workspace:*", | ||
"@stacks/network": "6.13.0", | ||
"@stacks/transactions-v7": "npm:@stacks/[email protected]", | ||
"zod": "3.23.8" | ||
"zod": "3.24.1" | ||
}, | ||
"devDependencies": { | ||
"tsup": "8.1.0", | ||
|
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 { addressResponseBodySchema, btcAddressBaseSchema, stxAddressSchema } from './get-addresses'; | ||
|
||
describe('getAddresses', () => { | ||
const baseRespnseBodyBtc = { | ||
symbol: 'BTC', | ||
type: 'p2wpkh', | ||
address: '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', | ||
publicKey: '02d9b4b6e', | ||
derivationPath: "m/44'/0'/0'/0/0", | ||
}; | ||
|
||
const baseRespnseBodyStx = { | ||
symbol: 'STX', | ||
address: 'SP1P72Z370VRYK5V9V3YVQAS1Z4X6D6GKQJ8K2JGK', | ||
publicKey: '02d9b4b6e', | ||
}; | ||
|
||
describe('btcAddressBaseSchema', () => { | ||
test('schema mathches test data', () => { | ||
const result = btcAddressBaseSchema.safeParse(baseRespnseBodyBtc); | ||
expect(result.success).toEqual(true); | ||
}); | ||
|
||
test('schema allows additional values', () => { | ||
const result = btcAddressBaseSchema.safeParse({ | ||
...baseRespnseBodyBtc, | ||
additionalProperties: 'should not be allowed', | ||
}); | ||
expect(result.success).toEqual(true); | ||
}); | ||
}); | ||
|
||
describe('stxAddressSchema', () => { | ||
test('schema allows additional values STX address values', () => { | ||
const result = stxAddressSchema.safeParse({ | ||
...baseRespnseBodyStx, | ||
additionalProperties: 'should not be allowed', | ||
}); | ||
expect(result.success).toEqual(true); | ||
}); | ||
}); | ||
|
||
describe('getAddressesResponseBody', () => { | ||
test('schema matches test data', () => { | ||
const result = addressResponseBodySchema.safeParse({ | ||
addresses: [baseRespnseBodyBtc, baseRespnseBodyStx], | ||
}); | ||
expect(result.success).toEqual(true); | ||
}); | ||
}); | ||
}); |
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,42 +1,89 @@ | ||
import { AllowAdditionalProperties } from '@leather.io/models'; | ||
import { z } from 'zod'; | ||
|
||
import { DefineRpcMethod, RpcRequest, RpcResponse } from '../rpc/schemas'; | ||
import { | ||
DefineRpcMethod, | ||
createRpcRequestSchema, | ||
createRpcResponseSchema, | ||
defaultErrorSchema, | ||
} from '../rpc/schemas'; | ||
|
||
export type PaymentTypes = 'p2pkh' | 'p2sh' | 'p2wpkh-p2sh' | 'p2wpkh' | 'p2tr'; | ||
const rpcGetAddressesMethodName = 'getAddresses'; | ||
|
||
export interface BtcAddressBase extends AllowAdditionalProperties { | ||
symbol: 'BTC'; | ||
type: PaymentTypes; | ||
address: string; | ||
publicKey: string; | ||
derivationPath: string; | ||
} | ||
export const bitcoinPaymentTypesSchema = z.enum(['p2pkh', 'p2sh', 'p2wpkh-p2sh', 'p2wpkh', 'p2tr']); | ||
export type BitcoinPaymentTypes = z.infer<typeof bitcoinPaymentTypesSchema>; | ||
|
||
export interface NativeSegwitAddress extends BtcAddressBase { | ||
type: 'p2wpkh'; | ||
} | ||
/** @deprecated use `BitcoinPaymentTypes` */ | ||
export type PaymentTypes = BitcoinPaymentTypes; | ||
|
||
export interface TaprootAddress extends BtcAddressBase { | ||
type: 'p2tr'; | ||
tweakedPublicKey: string; | ||
} | ||
// | ||
// Bitcoin | ||
export const btcAddressBaseSchema = z.object({ | ||
symbol: z.literal('BTC'), | ||
type: bitcoinPaymentTypesSchema, | ||
address: z.string(), | ||
publicKey: z.string(), | ||
derivationPath: z.string(), | ||
}); | ||
|
||
export type BtcAddress = NativeSegwitAddress | TaprootAddress; | ||
export type BtcAddressBase = z.infer<typeof btcAddressBaseSchema>; | ||
|
||
export interface StxAddress extends AllowAdditionalProperties { | ||
symbol: 'STX'; | ||
address: string; | ||
publicKey: string; | ||
} | ||
const nativeSegwitAddressSchema = btcAddressBaseSchema | ||
.extend({ | ||
type: z.literal('p2wpkh'), | ||
}) | ||
.passthrough(); | ||
|
||
export type Address = BtcAddress | StxAddress; | ||
export type NativeSegwitAddress = z.infer<typeof nativeSegwitAddressSchema>; | ||
|
||
export interface AddressResponseBody extends AllowAdditionalProperties { | ||
addresses: Address[]; | ||
} | ||
const taprootAddressSchema = btcAddressBaseSchema | ||
.extend({ | ||
type: z.literal('p2tr'), | ||
tweakedPublicKey: z.string(), | ||
}) | ||
.passthrough(); | ||
|
||
export type GetAddressesRequest = RpcRequest<'getAddresses'>; | ||
export type TaprootAddress = z.infer<typeof taprootAddressSchema>; | ||
|
||
export type GetAddressesResponse = RpcResponse<AddressResponseBody>; | ||
export const btcAddressSchema = z.discriminatedUnion('type', [ | ||
nativeSegwitAddressSchema, | ||
taprootAddressSchema, | ||
]); | ||
|
||
export type BtcAddress = z.infer<typeof btcAddressSchema>; | ||
|
||
// | ||
// Stacks | ||
export const stxAddressSchema = z | ||
.object({ | ||
symbol: z.literal('STX'), | ||
address: z.string(), | ||
publicKey: z.string(), | ||
}) | ||
.passthrough(); | ||
|
||
export type StxAddress = z.infer<typeof stxAddressSchema>; | ||
|
||
export const addressSchema = z.union([btcAddressSchema, stxAddressSchema]); | ||
|
||
export type Address = z.infer<typeof addressSchema>; | ||
|
||
export const getAddressesRequestSchema = createRpcRequestSchema(rpcGetAddressesMethodName); | ||
|
||
export type GetAddressesRequest = z.infer<typeof getAddressesRequestSchema>; | ||
|
||
// | ||
// Combined addresses response | ||
export const addressResponseBodySchema = z | ||
.object({ addresses: z.array(addressSchema) }) | ||
.passthrough(); | ||
|
||
export const getAddressesResponseSchema = createRpcResponseSchema( | ||
addressResponseBodySchema, | ||
defaultErrorSchema | ||
); | ||
|
||
export type AddressResponseBody = z.infer<typeof addressResponseBodySchema>; | ||
|
||
export type GetAddressesResponse = z.infer<typeof getAddressesResponseSchema>; | ||
|
||
export type DefineGetAddressesMethod = DefineRpcMethod<GetAddressesRequest, GetAddressesResponse>; |
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
Oops, something went wrong.