@dfinity/utils 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -32,6 +32,7 @@ npm i @dfinity/agent @dfinity/candid @dfinity/principal
32
32
  ### :toolbox: Functions
33
33
 
34
34
  - [defaultAgent](#gear-defaultagent)
35
+ - [createAgent](#gear-createagent)
35
36
  - [createServices](#gear-createservices)
36
37
  - [uint8ArrayToBigInt](#gear-uint8arraytobigint)
37
38
  - [arrayBufferToUint8Array](#gear-arraybuffertouint8array)
@@ -42,13 +43,30 @@ npm i @dfinity/agent @dfinity/candid @dfinity/principal
42
43
  - [toNullable](#gear-tonullable)
43
44
  - [fromNullable](#gear-fromnullable)
44
45
  - [fromDefinedNullable](#gear-fromdefinednullable)
46
+ - [principalToSubAccount](#gear-principaltosubaccount)
45
47
 
46
48
  #### :gear: defaultAgent
47
49
 
50
+ Get a default agent that connects to mainnet with the anonymous identity.
51
+
48
52
  | Function | Type |
49
53
  | -------------- | ------------- |
50
54
  | `defaultAgent` | `() => Agent` |
51
55
 
56
+ #### :gear: createAgent
57
+
58
+ Create an agent for a given identity
59
+
60
+ | Function | Type |
61
+ | ------------- | --------------------------------------------------------------------------------------------------------------------------- |
62
+ | `createAgent` | `({ identity, host, fetchRootKey, }: { identity: Identity; host?: string; fetchRootKey?: boolean; }) => Promise<HttpAgent>` |
63
+
64
+ Parameters:
65
+
66
+ - `identity`: A mandatory identity to use for the agent
67
+ - `host`: An optional host to connect to
68
+ - `fetchRootKey`: Fetch root key for certificate validation during local development or on testnet
69
+
52
70
  #### :gear: createServices
53
71
 
54
72
  | Function | Type |
@@ -109,6 +127,19 @@ npm i @dfinity/agent @dfinity/candid @dfinity/principal
109
127
  | --------------------- | ---------------------------- |
110
128
  | `fromDefinedNullable` | `<T>(value: [] or [T]) => T` |
111
129
 
130
+ #### :gear: principalToSubAccount
131
+
132
+ Convert a principal to a Uint8Array 32 length.
133
+ e.g. Useful to convert a canister ID when topping up cycles with the Cmc canister
134
+
135
+ | Function | Type |
136
+ | ----------------------- | -------------------------------------- |
137
+ | `principalToSubAccount` | `(principal: Principal) => Uint8Array` |
138
+
139
+ Parameters:
140
+
141
+ - `principal`: The principal that needs to be converted to Subaccount
142
+
112
143
  ### :factory: NullishError
113
144
 
114
145
  #### Constructors
@@ -1,2 +1,2 @@
1
- "use strict";var c=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var l=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var x=(t,r)=>{for(var e in r)c(t,e,{get:r[e],enumerable:!0})},b=(t,r,e,o)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of l(r))!d.call(t,n)&&n!==e&&c(t,n,{get:()=>r[n],enumerable:!(o=T(r,n))||o.enumerable});return t};var I=t=>b(c({},"__esModule",{value:!0}),t);var D={};x(D,{NullishError:()=>s,arrayBufferToUint8Array:()=>N,arrayOfNumberToUint8Array:()=>B,asciiStringToByteArray:()=>S,assertNonNullish:()=>y,createServices:()=>U,defaultAgent:()=>p,fromDefinedNullable:()=>C,fromNullable:()=>m,toNullable:()=>h,uint8ArrayToArrayOfNumber:()=>O,uint8ArrayToBigInt:()=>w});module.exports=I(D);var f=require("@dfinity/agent");var i=require("@dfinity/agent"),p=()=>new i.HttpAgent({host:"https://ic0.app",identity:new i.AnonymousIdentity});var U=({options:{canisterId:t,serviceOverride:r,certifiedServiceOverride:e,agent:o},idlFactory:n,certifiedIdlFactory:A})=>{let a=o??p(),u=r??f.Actor.createActor(n,{agent:a,canisterId:t}),g=e??f.Actor.createActor(A,{agent:a,canisterId:t});return{service:u,certifiedService:g,agent:a,canisterId:t}};var w=t=>{let r=new DataView(t.buffer,t.byteOffset,t.byteLength);if(typeof r.getBigUint64=="function")return r.getBigUint64(0);{let e=BigInt(r.getUint32(0)),o=BigInt(r.getUint32(4));return(e<<BigInt(32))+o}},N=t=>new Uint8Array(t),O=t=>Array.from(t),B=t=>new Uint8Array(t),S=t=>Array.from(t).map(r=>r.charCodeAt(0));var s=class extends Error{},y=(t,r)=>{if(t==null)throw new s(r)};var h=t=>t?[t]:[],m=t=>t==null?void 0:t[0],C=t=>{let r=m(t);return y(r),r};0&&(module.exports={NullishError,arrayBufferToUint8Array,arrayOfNumberToUint8Array,asciiStringToByteArray,assertNonNullish,createServices,defaultAgent,fromDefinedNullable,fromNullable,toNullable,uint8ArrayToArrayOfNumber,uint8ArrayToBigInt});
1
+ "use strict";var c=Object.defineProperty;var l=Object.getOwnPropertyDescriptor;var d=Object.getOwnPropertyNames;var T=Object.prototype.hasOwnProperty;var b=(t,r)=>{for(var e in r)c(t,e,{get:r[e],enumerable:!0})},x=(t,r,e,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of d(r))!T.call(t,o)&&o!==e&&c(t,o,{get:()=>r[o],enumerable:!(n=l(r,o))||n.enumerable});return t};var U=t=>x(c({},"__esModule",{value:!0}),t);var R={};b(R,{NullishError:()=>s,arrayBufferToUint8Array:()=>N,arrayOfNumberToUint8Array:()=>S,asciiStringToByteArray:()=>B,assertNonNullish:()=>f,createAgent:()=>I,createServices:()=>w,defaultAgent:()=>p,fromDefinedNullable:()=>P,fromNullable:()=>A,principalToSubAccount:()=>D,toNullable:()=>C,uint8ArrayToArrayOfNumber:()=>O,uint8ArrayToBigInt:()=>h});module.exports=U(R);var y=require("@dfinity/agent");var i=require("@dfinity/agent"),p=()=>new i.HttpAgent({host:"https://ic0.app",identity:new i.AnonymousIdentity}),I=async({identity:t,host:r,fetchRootKey:e=!1})=>{let n=new i.HttpAgent({identity:t,...r!==void 0&&{host:r}});return e&&await n.fetchRootKey(),n};var w=({options:{canisterId:t,serviceOverride:r,certifiedServiceOverride:e,agent:n},idlFactory:o,certifiedIdlFactory:m})=>{let a=n??p(),u=r??y.Actor.createActor(o,{agent:a,canisterId:t}),g=e??y.Actor.createActor(m,{agent:a,canisterId:t});return{service:u,certifiedService:g,agent:a,canisterId:t}};var h=t=>{let r=new DataView(t.buffer,t.byteOffset,t.byteLength);if(typeof r.getBigUint64=="function")return r.getBigUint64(0);{let e=BigInt(r.getUint32(0)),n=BigInt(r.getUint32(4));return(e<<BigInt(32))+n}},N=t=>new Uint8Array(t),O=t=>Array.from(t),S=t=>new Uint8Array(t),B=t=>Array.from(t).map(r=>r.charCodeAt(0));var s=class extends Error{},f=(t,r)=>{if(t==null)throw new s(r)};var C=t=>t?[t]:[],A=t=>t==null?void 0:t[0],P=t=>{let r=A(t);return f(r),r};var D=t=>{let r=t.toUint8Array(),e=new Uint8Array(32);return e[0]=r.length,e.set(r,1),e};0&&(module.exports={NullishError,arrayBufferToUint8Array,arrayOfNumberToUint8Array,asciiStringToByteArray,assertNonNullish,createAgent,createServices,defaultAgent,fromDefinedNullable,fromNullable,principalToSubAccount,toNullable,uint8ArrayToArrayOfNumber,uint8ArrayToBigInt});
2
2
  //# sourceMappingURL=index.cjs.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../../src/index.ts", "../../src/utils/actor.utils.ts", "../../src/utils/agent.utils.ts", "../../src/utils/arrays.utils.ts", "../../src/utils/asserts.utils.ts", "../../src/utils/did.utils.ts"],
4
- "sourcesContent": ["export type { CanisterOptions } from \"./types/canister.options\";\nexport * from \"./utils/actor.utils\";\nexport * from \"./utils/agent.utils\";\nexport * from \"./utils/arrays.utils\";\nexport * from \"./utils/asserts.utils\";\nexport * from \"./utils/did.utils\";\n", "import type { ActorSubclass, Agent } from \"@dfinity/agent\";\nimport { Actor } from \"@dfinity/agent\";\nimport type { IDL } from \"@dfinity/candid\";\nimport type { Principal } from \"@dfinity/principal\";\nimport type { CanisterOptions } from \"../types/canister.options\";\nimport { defaultAgent } from \"./agent.utils\";\n\ntype RequiredCanisterOptions<T> = Required<\n Pick<CanisterOptions<T>, \"canisterId\">\n> &\n Omit<CanisterOptions<T>, \"canisterId\">;\n\nexport const createServices = <T>({\n options: {\n canisterId,\n serviceOverride,\n certifiedServiceOverride,\n agent: agentOption,\n },\n idlFactory,\n certifiedIdlFactory,\n}: {\n options: RequiredCanisterOptions<T>;\n idlFactory: IDL.InterfaceFactory;\n certifiedIdlFactory: IDL.InterfaceFactory;\n}): {\n service: ActorSubclass<T>;\n certifiedService: ActorSubclass<T>;\n agent: Agent;\n canisterId: Principal;\n} => {\n const agent: Agent = agentOption ?? defaultAgent();\n\n const service: ActorSubclass<T> =\n serviceOverride ??\n Actor.createActor<T>(idlFactory, {\n agent,\n canisterId,\n });\n\n const certifiedService: ActorSubclass<T> =\n certifiedServiceOverride ??\n Actor.createActor<T>(certifiedIdlFactory, {\n agent,\n canisterId,\n });\n\n return { service, certifiedService, agent, canisterId };\n};\n", "import type { Agent } from \"@dfinity/agent\";\nimport { AnonymousIdentity, HttpAgent } from \"@dfinity/agent\";\n\n/**\n * @returns The default agent to use. An agent that connects to mainnet with the anonymous identity.\n */\nexport const defaultAgent = (): Agent =>\n new HttpAgent({\n host: \"https://ic0.app\",\n identity: new AnonymousIdentity(),\n });\n", "export const uint8ArrayToBigInt = (array: Uint8Array): bigint => {\n const view = new DataView(array.buffer, array.byteOffset, array.byteLength);\n if (typeof view.getBigUint64 === \"function\") {\n return view.getBigUint64(0);\n } else {\n const high = BigInt(view.getUint32(0));\n const low = BigInt(view.getUint32(4));\n\n return (high << BigInt(32)) + low;\n }\n};\n\nexport const arrayBufferToUint8Array = (buffer: ArrayBuffer): Uint8Array =>\n new Uint8Array(buffer);\n\nexport const uint8ArrayToArrayOfNumber = (array: Uint8Array): Array<number> =>\n Array.from(array);\n\nexport const arrayOfNumberToUint8Array = (numbers: Array<number>): Uint8Array =>\n new Uint8Array(numbers);\n\nexport const asciiStringToByteArray = (text: string): Array<number> =>\n Array.from(text).map((c) => c.charCodeAt(0));\n", "export class NullishError extends Error {}\n\nexport const assertNonNullish: <T>(\n value: T,\n message?: string\n) => asserts value is NonNullable<T> = <T>(\n value: T,\n message?: string\n): void => {\n if (value === null || value === undefined) {\n throw new NullishError(message);\n }\n};\n", "import { assertNonNullish } from \"./asserts.utils\";\n\nexport const toNullable = <T>(value?: T): [] | [T] => {\n return value ? [value] : [];\n};\n\nexport const fromNullable = <T>(value: [] | [T]): T | undefined => {\n return value?.[0];\n};\n\nexport const fromDefinedNullable = <T>(value: [] | [T]): T => {\n const result = fromNullable(value);\n\n assertNonNullish(result);\n\n return result;\n};\n"],
5
- "mappings": "yaAAA,wUCCA,MAAsB,0BCAtB,MAA6C,0BAKhC,EAAe,IAC1B,GAAI,aAAU,CACZ,KAAM,kBACN,SAAU,GAAI,oBAChB,CAAC,EDEI,GAAM,GAAiB,CAAI,CAChC,QAAS,CACP,aACA,kBACA,2BACA,MAAO,GAET,aACA,yBAUG,CACH,GAAM,GAAe,GAAe,EAAa,EAE3C,EACJ,GACA,QAAM,YAAe,EAAY,CAC/B,QACA,YACF,CAAC,EAEG,EACJ,GACA,QAAM,YAAe,EAAqB,CACxC,QACA,YACF,CAAC,EAEH,MAAO,CAAE,UAAS,mBAAkB,QAAO,YAAW,CACxD,EEhDO,GAAM,GAAqB,AAAC,GAA8B,CAC/D,GAAM,GAAO,GAAI,UAAS,EAAM,OAAQ,EAAM,WAAY,EAAM,UAAU,EAC1E,GAAI,MAAO,GAAK,cAAiB,WAC/B,MAAO,GAAK,aAAa,CAAC,EACrB,CACL,GAAM,GAAO,OAAO,EAAK,UAAU,CAAC,CAAC,EAC/B,EAAM,OAAO,EAAK,UAAU,CAAC,CAAC,EAEpC,MAAQ,IAAQ,OAAO,EAAE,GAAK,CAChC,CACF,EAEa,EAA0B,AAAC,GACtC,GAAI,YAAW,CAAM,EAEV,EAA4B,AAAC,GACxC,MAAM,KAAK,CAAK,EAEL,EAA4B,AAAC,GACxC,GAAI,YAAW,CAAO,EAEX,EAAyB,AAAC,GACrC,MAAM,KAAK,CAAI,EAAE,IAAI,AAAC,GAAM,EAAE,WAAW,CAAC,CAAC,ECtBtC,GAAM,GAAN,aAA2B,MAAM,CAAC,EAE5B,EAG0B,CACrC,EACA,IACS,CACT,GAAI,GAAU,KACZ,KAAM,IAAI,GAAa,CAAO,CAElC,ECVO,GAAM,GAAa,AAAI,GACrB,EAAQ,CAAC,CAAK,EAAI,CAAC,EAGf,EAAe,AAAI,GACvB,iBAAQ,GAGJ,EAAsB,AAAI,GAAuB,CAC5D,GAAM,GAAS,EAAa,CAAK,EAEjC,SAAiB,CAAM,EAEhB,CACT",
3
+ "sources": ["../../src/index.ts", "../../src/utils/actor.utils.ts", "../../src/utils/agent.utils.ts", "../../src/utils/arrays.utils.ts", "../../src/utils/asserts.utils.ts", "../../src/utils/did.utils.ts", "../../src/utils/principal.utils.ts"],
4
+ "sourcesContent": ["export type { CanisterOptions } from \"./types/canister.options\";\nexport * from \"./utils/actor.utils\";\nexport * from \"./utils/agent.utils\";\nexport * from \"./utils/arrays.utils\";\nexport * from \"./utils/asserts.utils\";\nexport * from \"./utils/did.utils\";\nexport * from \"./utils/principal.utils\";\n", "import type { ActorSubclass, Agent } from \"@dfinity/agent\";\nimport { Actor } from \"@dfinity/agent\";\nimport type { IDL } from \"@dfinity/candid\";\nimport type { Principal } from \"@dfinity/principal\";\nimport type { CanisterOptions } from \"../types/canister.options\";\nimport { defaultAgent } from \"./agent.utils\";\n\ntype RequiredCanisterOptions<T> = Required<\n Pick<CanisterOptions<T>, \"canisterId\">\n> &\n Omit<CanisterOptions<T>, \"canisterId\">;\n\nexport const createServices = <T>({\n options: {\n canisterId,\n serviceOverride,\n certifiedServiceOverride,\n agent: agentOption,\n },\n idlFactory,\n certifiedIdlFactory,\n}: {\n options: RequiredCanisterOptions<T>;\n idlFactory: IDL.InterfaceFactory;\n certifiedIdlFactory: IDL.InterfaceFactory;\n}): {\n service: ActorSubclass<T>;\n certifiedService: ActorSubclass<T>;\n agent: Agent;\n canisterId: Principal;\n} => {\n const agent: Agent = agentOption ?? defaultAgent();\n\n const service: ActorSubclass<T> =\n serviceOverride ??\n Actor.createActor<T>(idlFactory, {\n agent,\n canisterId,\n });\n\n const certifiedService: ActorSubclass<T> =\n certifiedServiceOverride ??\n Actor.createActor<T>(certifiedIdlFactory, {\n agent,\n canisterId,\n });\n\n return { service, certifiedService, agent, canisterId };\n};\n", "import type { Agent, Identity } from \"@dfinity/agent\";\nimport { AnonymousIdentity, HttpAgent } from \"@dfinity/agent\";\n\n/**\n * Get a default agent that connects to mainnet with the anonymous identity.\n * @returns The default agent to use\n */\nexport const defaultAgent = (): Agent =>\n new HttpAgent({\n host: \"https://ic0.app\",\n identity: new AnonymousIdentity(),\n });\n\n/**\n * Create an agent for a given identity\n * @param identity A mandatory identity to use for the agent\n * @param host An optional host to connect to\n * @param fetchRootKey Fetch root key for certificate validation during local development or on testnet\n */\nexport const createAgent = async ({\n identity,\n host,\n fetchRootKey = false,\n}: {\n identity: Identity;\n host?: string;\n fetchRootKey?: boolean;\n}): Promise<HttpAgent> => {\n const agent: HttpAgent = new HttpAgent({\n identity,\n ...(host !== undefined && { host }),\n });\n\n if (fetchRootKey) {\n await agent.fetchRootKey();\n }\n\n return agent;\n};\n", "export const uint8ArrayToBigInt = (array: Uint8Array): bigint => {\n const view = new DataView(array.buffer, array.byteOffset, array.byteLength);\n if (typeof view.getBigUint64 === \"function\") {\n return view.getBigUint64(0);\n } else {\n const high = BigInt(view.getUint32(0));\n const low = BigInt(view.getUint32(4));\n\n return (high << BigInt(32)) + low;\n }\n};\n\nexport const arrayBufferToUint8Array = (buffer: ArrayBuffer): Uint8Array =>\n new Uint8Array(buffer);\n\nexport const uint8ArrayToArrayOfNumber = (array: Uint8Array): Array<number> =>\n Array.from(array);\n\nexport const arrayOfNumberToUint8Array = (numbers: Array<number>): Uint8Array =>\n new Uint8Array(numbers);\n\nexport const asciiStringToByteArray = (text: string): Array<number> =>\n Array.from(text).map((c) => c.charCodeAt(0));\n", "export class NullishError extends Error {}\n\nexport const assertNonNullish: <T>(\n value: T,\n message?: string\n) => asserts value is NonNullable<T> = <T>(\n value: T,\n message?: string\n): void => {\n if (value === null || value === undefined) {\n throw new NullishError(message);\n }\n};\n", "import { assertNonNullish } from \"./asserts.utils\";\n\nexport const toNullable = <T>(value?: T): [] | [T] => {\n return value ? [value] : [];\n};\n\nexport const fromNullable = <T>(value: [] | [T]): T | undefined => {\n return value?.[0];\n};\n\nexport const fromDefinedNullable = <T>(value: [] | [T]): T => {\n const result = fromNullable(value);\n\n assertNonNullish(result);\n\n return result;\n};\n", "import type { Principal } from \"@dfinity/principal\";\n\n/**\n * Convert a principal to a Uint8Array 32 length.\n * e.g. Useful to convert a canister ID when topping up cycles with the Cmc canister\n * @param principal The principal that needs to be converted to Subaccount\n */\nexport const principalToSubAccount = (principal: Principal): Uint8Array => {\n const bytes: Uint8Array = principal.toUint8Array();\n const subAccount: Uint8Array = new Uint8Array(32);\n subAccount[0] = bytes.length;\n subAccount.set(bytes, 1);\n return subAccount;\n};\n"],
5
+ "mappings": "yaAAA,sXCCA,MAAsB,0BCAtB,MAA6C,0BAMhC,EAAe,IAC1B,GAAI,aAAU,CACZ,KAAM,kBACN,SAAU,GAAI,oBAChB,CAAC,EAQU,EAAc,MAAO,CAChC,WACA,OACA,eAAe,MAKS,CACxB,GAAM,GAAmB,GAAI,aAAU,CACrC,WACA,GAAI,IAAS,QAAa,CAAE,MAAK,CACnC,CAAC,EAED,MAAI,IACF,KAAM,GAAM,aAAa,EAGpB,CACT,ED1BO,GAAM,GAAiB,CAAI,CAChC,QAAS,CACP,aACA,kBACA,2BACA,MAAO,GAET,aACA,yBAUG,CACH,GAAM,GAAe,GAAe,EAAa,EAE3C,EACJ,GACA,QAAM,YAAe,EAAY,CAC/B,QACA,YACF,CAAC,EAEG,EACJ,GACA,QAAM,YAAe,EAAqB,CACxC,QACA,YACF,CAAC,EAEH,MAAO,CAAE,UAAS,mBAAkB,QAAO,YAAW,CACxD,EEhDO,GAAM,GAAqB,AAAC,GAA8B,CAC/D,GAAM,GAAO,GAAI,UAAS,EAAM,OAAQ,EAAM,WAAY,EAAM,UAAU,EAC1E,GAAI,MAAO,GAAK,cAAiB,WAC/B,MAAO,GAAK,aAAa,CAAC,EACrB,CACL,GAAM,GAAO,OAAO,EAAK,UAAU,CAAC,CAAC,EAC/B,EAAM,OAAO,EAAK,UAAU,CAAC,CAAC,EAEpC,MAAQ,IAAQ,OAAO,EAAE,GAAK,CAChC,CACF,EAEa,EAA0B,AAAC,GACtC,GAAI,YAAW,CAAM,EAEV,EAA4B,AAAC,GACxC,MAAM,KAAK,CAAK,EAEL,EAA4B,AAAC,GACxC,GAAI,YAAW,CAAO,EAEX,EAAyB,AAAC,GACrC,MAAM,KAAK,CAAI,EAAE,IAAI,AAAC,GAAM,EAAE,WAAW,CAAC,CAAC,ECtBtC,GAAM,GAAN,aAA2B,MAAM,CAAC,EAE5B,EAG0B,CACrC,EACA,IACS,CACT,GAAI,GAAU,KACZ,KAAM,IAAI,GAAa,CAAO,CAElC,ECVO,GAAM,GAAa,AAAI,GACrB,EAAQ,CAAC,CAAK,EAAI,CAAC,EAGf,EAAe,AAAI,GACvB,iBAAQ,GAGJ,EAAsB,AAAI,GAAuB,CAC5D,GAAM,GAAS,EAAa,CAAK,EAEjC,SAAiB,CAAM,EAEhB,CACT,ECTO,GAAM,GAAwB,AAAC,GAAqC,CACzE,GAAM,GAAoB,EAAU,aAAa,EAC3C,EAAyB,GAAI,YAAW,EAAE,EAChD,SAAW,GAAK,EAAM,OACtB,EAAW,IAAI,EAAO,CAAC,EAChB,CACT",
6
6
  "names": []
7
7
  }
package/dist/esm/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import{Actor as a}from"@dfinity/agent";import{AnonymousIdentity as u,HttpAgent as A}from"@dfinity/agent";var s=()=>new A({host:"https://ic0.app",identity:new u});var b=({options:{canisterId:t,serviceOverride:r,certifiedServiceOverride:e,agent:n},idlFactory:p,certifiedIdlFactory:f})=>{let o=n??s(),y=r??a.createActor(p,{agent:o,canisterId:t}),m=e??a.createActor(f,{agent:o,canisterId:t});return{service:y,certifiedService:m,agent:o,canisterId:t}};var U=t=>{let r=new DataView(t.buffer,t.byteOffset,t.byteLength);if(typeof r.getBigUint64=="function")return r.getBigUint64(0);{let e=BigInt(r.getUint32(0)),n=BigInt(r.getUint32(4));return(e<<BigInt(32))+n}},w=t=>new Uint8Array(t),N=t=>Array.from(t),O=t=>new Uint8Array(t),B=t=>Array.from(t).map(r=>r.charCodeAt(0));var i=class extends Error{},c=(t,r)=>{if(t==null)throw new i(r)};var C=t=>t?[t]:[],l=t=>t?.[0],D=t=>{let r=l(t);return c(r),r};export{i as NullishError,w as arrayBufferToUint8Array,O as arrayOfNumberToUint8Array,B as asciiStringToByteArray,c as assertNonNullish,b as createServices,s as defaultAgent,D as fromDefinedNullable,l as fromNullable,C as toNullable,N as uint8ArrayToArrayOfNumber,U as uint8ArrayToBigInt};
1
+ import{Actor as c}from"@dfinity/agent";import{AnonymousIdentity as m,HttpAgent as s}from"@dfinity/agent";var a=()=>new s({host:"https://ic0.app",identity:new m}),d=async({identity:t,host:r,fetchRootKey:e=!1})=>{let n=new s({identity:t,...r!==void 0&&{host:r}});return e&&await n.fetchRootKey(),n};var U=({options:{canisterId:t,serviceOverride:r,certifiedServiceOverride:e,agent:n},idlFactory:y,certifiedIdlFactory:f})=>{let o=n??a(),A=r??c.createActor(y,{agent:o,canisterId:t}),u=e??c.createActor(f,{agent:o,canisterId:t});return{service:A,certifiedService:u,agent:o,canisterId:t}};var w=t=>{let r=new DataView(t.buffer,t.byteOffset,t.byteLength);if(typeof r.getBigUint64=="function")return r.getBigUint64(0);{let e=BigInt(r.getUint32(0)),n=BigInt(r.getUint32(4));return(e<<BigInt(32))+n}},h=t=>new Uint8Array(t),N=t=>Array.from(t),O=t=>new Uint8Array(t),S=t=>Array.from(t).map(r=>r.charCodeAt(0));var i=class extends Error{},p=(t,r)=>{if(t==null)throw new i(r)};var P=t=>t?[t]:[],l=t=>t?.[0],D=t=>{let r=l(t);return p(r),r};var F=t=>{let r=t.toUint8Array(),e=new Uint8Array(32);return e[0]=r.length,e.set(r,1),e};export{i as NullishError,h as arrayBufferToUint8Array,O as arrayOfNumberToUint8Array,S as asciiStringToByteArray,p as assertNonNullish,d as createAgent,U as createServices,a as defaultAgent,D as fromDefinedNullable,l as fromNullable,F as principalToSubAccount,P as toNullable,N as uint8ArrayToArrayOfNumber,w as uint8ArrayToBigInt};
2
2
  //# sourceMappingURL=index.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../../src/utils/actor.utils.ts", "../../src/utils/agent.utils.ts", "../../src/utils/arrays.utils.ts", "../../src/utils/asserts.utils.ts", "../../src/utils/did.utils.ts"],
4
- "sourcesContent": ["import type { ActorSubclass, Agent } from \"@dfinity/agent\";\nimport { Actor } from \"@dfinity/agent\";\nimport type { IDL } from \"@dfinity/candid\";\nimport type { Principal } from \"@dfinity/principal\";\nimport type { CanisterOptions } from \"../types/canister.options\";\nimport { defaultAgent } from \"./agent.utils\";\n\ntype RequiredCanisterOptions<T> = Required<\n Pick<CanisterOptions<T>, \"canisterId\">\n> &\n Omit<CanisterOptions<T>, \"canisterId\">;\n\nexport const createServices = <T>({\n options: {\n canisterId,\n serviceOverride,\n certifiedServiceOverride,\n agent: agentOption,\n },\n idlFactory,\n certifiedIdlFactory,\n}: {\n options: RequiredCanisterOptions<T>;\n idlFactory: IDL.InterfaceFactory;\n certifiedIdlFactory: IDL.InterfaceFactory;\n}): {\n service: ActorSubclass<T>;\n certifiedService: ActorSubclass<T>;\n agent: Agent;\n canisterId: Principal;\n} => {\n const agent: Agent = agentOption ?? defaultAgent();\n\n const service: ActorSubclass<T> =\n serviceOverride ??\n Actor.createActor<T>(idlFactory, {\n agent,\n canisterId,\n });\n\n const certifiedService: ActorSubclass<T> =\n certifiedServiceOverride ??\n Actor.createActor<T>(certifiedIdlFactory, {\n agent,\n canisterId,\n });\n\n return { service, certifiedService, agent, canisterId };\n};\n", "import type { Agent } from \"@dfinity/agent\";\nimport { AnonymousIdentity, HttpAgent } from \"@dfinity/agent\";\n\n/**\n * @returns The default agent to use. An agent that connects to mainnet with the anonymous identity.\n */\nexport const defaultAgent = (): Agent =>\n new HttpAgent({\n host: \"https://ic0.app\",\n identity: new AnonymousIdentity(),\n });\n", "export const uint8ArrayToBigInt = (array: Uint8Array): bigint => {\n const view = new DataView(array.buffer, array.byteOffset, array.byteLength);\n if (typeof view.getBigUint64 === \"function\") {\n return view.getBigUint64(0);\n } else {\n const high = BigInt(view.getUint32(0));\n const low = BigInt(view.getUint32(4));\n\n return (high << BigInt(32)) + low;\n }\n};\n\nexport const arrayBufferToUint8Array = (buffer: ArrayBuffer): Uint8Array =>\n new Uint8Array(buffer);\n\nexport const uint8ArrayToArrayOfNumber = (array: Uint8Array): Array<number> =>\n Array.from(array);\n\nexport const arrayOfNumberToUint8Array = (numbers: Array<number>): Uint8Array =>\n new Uint8Array(numbers);\n\nexport const asciiStringToByteArray = (text: string): Array<number> =>\n Array.from(text).map((c) => c.charCodeAt(0));\n", "export class NullishError extends Error {}\n\nexport const assertNonNullish: <T>(\n value: T,\n message?: string\n) => asserts value is NonNullable<T> = <T>(\n value: T,\n message?: string\n): void => {\n if (value === null || value === undefined) {\n throw new NullishError(message);\n }\n};\n", "import { assertNonNullish } from \"./asserts.utils\";\n\nexport const toNullable = <T>(value?: T): [] | [T] => {\n return value ? [value] : [];\n};\n\nexport const fromNullable = <T>(value: [] | [T]): T | undefined => {\n return value?.[0];\n};\n\nexport const fromDefinedNullable = <T>(value: [] | [T]): T => {\n const result = fromNullable(value);\n\n assertNonNullish(result);\n\n return result;\n};\n"],
5
- "mappings": "AACA,uCCAA,kEAKO,GAAM,GAAe,IAC1B,GAAI,GAAU,CACZ,KAAM,kBACN,SAAU,GAAI,EAChB,CAAC,EDEI,GAAM,GAAiB,CAAI,CAChC,QAAS,CACP,aACA,kBACA,2BACA,MAAO,GAET,aACA,yBAUG,CACH,GAAM,GAAe,GAAe,EAAa,EAE3C,EACJ,GACA,EAAM,YAAe,EAAY,CAC/B,QACA,YACF,CAAC,EAEG,EACJ,GACA,EAAM,YAAe,EAAqB,CACxC,QACA,YACF,CAAC,EAEH,MAAO,CAAE,UAAS,mBAAkB,QAAO,YAAW,CACxD,EEhDO,GAAM,GAAqB,AAAC,GAA8B,CAC/D,GAAM,GAAO,GAAI,UAAS,EAAM,OAAQ,EAAM,WAAY,EAAM,UAAU,EAC1E,GAAI,MAAO,GAAK,cAAiB,WAC/B,MAAO,GAAK,aAAa,CAAC,EACrB,CACL,GAAM,GAAO,OAAO,EAAK,UAAU,CAAC,CAAC,EAC/B,EAAM,OAAO,EAAK,UAAU,CAAC,CAAC,EAEpC,MAAQ,IAAQ,OAAO,EAAE,GAAK,CAChC,CACF,EAEa,EAA0B,AAAC,GACtC,GAAI,YAAW,CAAM,EAEV,EAA4B,AAAC,GACxC,MAAM,KAAK,CAAK,EAEL,EAA4B,AAAC,GACxC,GAAI,YAAW,CAAO,EAEX,EAAyB,AAAC,GACrC,MAAM,KAAK,CAAI,EAAE,IAAI,AAAC,GAAM,EAAE,WAAW,CAAC,CAAC,ECtBtC,GAAM,GAAN,aAA2B,MAAM,CAAC,EAE5B,EAG0B,CACrC,EACA,IACS,CACT,GAAI,GAAU,KACZ,KAAM,IAAI,GAAa,CAAO,CAElC,ECVO,GAAM,GAAa,AAAI,GACrB,EAAQ,CAAC,CAAK,EAAI,CAAC,EAGf,EAAe,AAAI,GACvB,IAAQ,GAGJ,EAAsB,AAAI,GAAuB,CAC5D,GAAM,GAAS,EAAa,CAAK,EAEjC,SAAiB,CAAM,EAEhB,CACT",
3
+ "sources": ["../../src/utils/actor.utils.ts", "../../src/utils/agent.utils.ts", "../../src/utils/arrays.utils.ts", "../../src/utils/asserts.utils.ts", "../../src/utils/did.utils.ts", "../../src/utils/principal.utils.ts"],
4
+ "sourcesContent": ["import type { ActorSubclass, Agent } from \"@dfinity/agent\";\nimport { Actor } from \"@dfinity/agent\";\nimport type { IDL } from \"@dfinity/candid\";\nimport type { Principal } from \"@dfinity/principal\";\nimport type { CanisterOptions } from \"../types/canister.options\";\nimport { defaultAgent } from \"./agent.utils\";\n\ntype RequiredCanisterOptions<T> = Required<\n Pick<CanisterOptions<T>, \"canisterId\">\n> &\n Omit<CanisterOptions<T>, \"canisterId\">;\n\nexport const createServices = <T>({\n options: {\n canisterId,\n serviceOverride,\n certifiedServiceOverride,\n agent: agentOption,\n },\n idlFactory,\n certifiedIdlFactory,\n}: {\n options: RequiredCanisterOptions<T>;\n idlFactory: IDL.InterfaceFactory;\n certifiedIdlFactory: IDL.InterfaceFactory;\n}): {\n service: ActorSubclass<T>;\n certifiedService: ActorSubclass<T>;\n agent: Agent;\n canisterId: Principal;\n} => {\n const agent: Agent = agentOption ?? defaultAgent();\n\n const service: ActorSubclass<T> =\n serviceOverride ??\n Actor.createActor<T>(idlFactory, {\n agent,\n canisterId,\n });\n\n const certifiedService: ActorSubclass<T> =\n certifiedServiceOverride ??\n Actor.createActor<T>(certifiedIdlFactory, {\n agent,\n canisterId,\n });\n\n return { service, certifiedService, agent, canisterId };\n};\n", "import type { Agent, Identity } from \"@dfinity/agent\";\nimport { AnonymousIdentity, HttpAgent } from \"@dfinity/agent\";\n\n/**\n * Get a default agent that connects to mainnet with the anonymous identity.\n * @returns The default agent to use\n */\nexport const defaultAgent = (): Agent =>\n new HttpAgent({\n host: \"https://ic0.app\",\n identity: new AnonymousIdentity(),\n });\n\n/**\n * Create an agent for a given identity\n * @param identity A mandatory identity to use for the agent\n * @param host An optional host to connect to\n * @param fetchRootKey Fetch root key for certificate validation during local development or on testnet\n */\nexport const createAgent = async ({\n identity,\n host,\n fetchRootKey = false,\n}: {\n identity: Identity;\n host?: string;\n fetchRootKey?: boolean;\n}): Promise<HttpAgent> => {\n const agent: HttpAgent = new HttpAgent({\n identity,\n ...(host !== undefined && { host }),\n });\n\n if (fetchRootKey) {\n await agent.fetchRootKey();\n }\n\n return agent;\n};\n", "export const uint8ArrayToBigInt = (array: Uint8Array): bigint => {\n const view = new DataView(array.buffer, array.byteOffset, array.byteLength);\n if (typeof view.getBigUint64 === \"function\") {\n return view.getBigUint64(0);\n } else {\n const high = BigInt(view.getUint32(0));\n const low = BigInt(view.getUint32(4));\n\n return (high << BigInt(32)) + low;\n }\n};\n\nexport const arrayBufferToUint8Array = (buffer: ArrayBuffer): Uint8Array =>\n new Uint8Array(buffer);\n\nexport const uint8ArrayToArrayOfNumber = (array: Uint8Array): Array<number> =>\n Array.from(array);\n\nexport const arrayOfNumberToUint8Array = (numbers: Array<number>): Uint8Array =>\n new Uint8Array(numbers);\n\nexport const asciiStringToByteArray = (text: string): Array<number> =>\n Array.from(text).map((c) => c.charCodeAt(0));\n", "export class NullishError extends Error {}\n\nexport const assertNonNullish: <T>(\n value: T,\n message?: string\n) => asserts value is NonNullable<T> = <T>(\n value: T,\n message?: string\n): void => {\n if (value === null || value === undefined) {\n throw new NullishError(message);\n }\n};\n", "import { assertNonNullish } from \"./asserts.utils\";\n\nexport const toNullable = <T>(value?: T): [] | [T] => {\n return value ? [value] : [];\n};\n\nexport const fromNullable = <T>(value: [] | [T]): T | undefined => {\n return value?.[0];\n};\n\nexport const fromDefinedNullable = <T>(value: [] | [T]): T => {\n const result = fromNullable(value);\n\n assertNonNullish(result);\n\n return result;\n};\n", "import type { Principal } from \"@dfinity/principal\";\n\n/**\n * Convert a principal to a Uint8Array 32 length.\n * e.g. Useful to convert a canister ID when topping up cycles with the Cmc canister\n * @param principal The principal that needs to be converted to Subaccount\n */\nexport const principalToSubAccount = (principal: Principal): Uint8Array => {\n const bytes: Uint8Array = principal.toUint8Array();\n const subAccount: Uint8Array = new Uint8Array(32);\n subAccount[0] = bytes.length;\n subAccount.set(bytes, 1);\n return subAccount;\n};\n"],
5
+ "mappings": "AACA,uCCAA,kEAMO,GAAM,GAAe,IAC1B,GAAI,GAAU,CACZ,KAAM,kBACN,SAAU,GAAI,EAChB,CAAC,EAQU,EAAc,MAAO,CAChC,WACA,OACA,eAAe,MAKS,CACxB,GAAM,GAAmB,GAAI,GAAU,CACrC,WACA,GAAI,IAAS,QAAa,CAAE,MAAK,CACnC,CAAC,EAED,MAAI,IACF,KAAM,GAAM,aAAa,EAGpB,CACT,ED1BO,GAAM,GAAiB,CAAI,CAChC,QAAS,CACP,aACA,kBACA,2BACA,MAAO,GAET,aACA,yBAUG,CACH,GAAM,GAAe,GAAe,EAAa,EAE3C,EACJ,GACA,EAAM,YAAe,EAAY,CAC/B,QACA,YACF,CAAC,EAEG,EACJ,GACA,EAAM,YAAe,EAAqB,CACxC,QACA,YACF,CAAC,EAEH,MAAO,CAAE,UAAS,mBAAkB,QAAO,YAAW,CACxD,EEhDO,GAAM,GAAqB,AAAC,GAA8B,CAC/D,GAAM,GAAO,GAAI,UAAS,EAAM,OAAQ,EAAM,WAAY,EAAM,UAAU,EAC1E,GAAI,MAAO,GAAK,cAAiB,WAC/B,MAAO,GAAK,aAAa,CAAC,EACrB,CACL,GAAM,GAAO,OAAO,EAAK,UAAU,CAAC,CAAC,EAC/B,EAAM,OAAO,EAAK,UAAU,CAAC,CAAC,EAEpC,MAAQ,IAAQ,OAAO,EAAE,GAAK,CAChC,CACF,EAEa,EAA0B,AAAC,GACtC,GAAI,YAAW,CAAM,EAEV,EAA4B,AAAC,GACxC,MAAM,KAAK,CAAK,EAEL,EAA4B,AAAC,GACxC,GAAI,YAAW,CAAO,EAEX,EAAyB,AAAC,GACrC,MAAM,KAAK,CAAI,EAAE,IAAI,AAAC,GAAM,EAAE,WAAW,CAAC,CAAC,ECtBtC,GAAM,GAAN,aAA2B,MAAM,CAAC,EAE5B,EAG0B,CACrC,EACA,IACS,CACT,GAAI,GAAU,KACZ,KAAM,IAAI,GAAa,CAAO,CAElC,ECVO,GAAM,GAAa,AAAI,GACrB,EAAQ,CAAC,CAAK,EAAI,CAAC,EAGf,EAAe,AAAI,GACvB,IAAQ,GAGJ,EAAsB,AAAI,GAAuB,CAC5D,GAAM,GAAS,EAAa,CAAK,EAEjC,SAAiB,CAAM,EAEhB,CACT,ECTO,GAAM,GAAwB,AAAC,GAAqC,CACzE,GAAM,GAAoB,EAAU,aAAa,EAC3C,EAAyB,GAAI,YAAW,EAAE,EAChD,SAAW,GAAK,EAAM,OACtB,EAAW,IAAI,EAAO,CAAC,EAChB,CACT",
6
6
  "names": []
7
7
  }
@@ -4,3 +4,4 @@ export * from "./utils/agent.utils";
4
4
  export * from "./utils/arrays.utils";
5
5
  export * from "./utils/asserts.utils";
6
6
  export * from "./utils/did.utils";
7
+ export * from "./utils/principal.utils";
@@ -1,5 +1,18 @@
1
- import type { Agent } from "@dfinity/agent";
1
+ import type { Agent, Identity } from "@dfinity/agent";
2
+ import { HttpAgent } from "@dfinity/agent";
2
3
  /**
3
- * @returns The default agent to use. An agent that connects to mainnet with the anonymous identity.
4
+ * Get a default agent that connects to mainnet with the anonymous identity.
5
+ * @returns The default agent to use
4
6
  */
5
7
  export declare const defaultAgent: () => Agent;
8
+ /**
9
+ * Create an agent for a given identity
10
+ * @param identity A mandatory identity to use for the agent
11
+ * @param host An optional host to connect to
12
+ * @param fetchRootKey Fetch root key for certificate validation during local development or on testnet
13
+ */
14
+ export declare const createAgent: ({ identity, host, fetchRootKey, }: {
15
+ identity: Identity;
16
+ host?: string | undefined;
17
+ fetchRootKey?: boolean | undefined;
18
+ }) => Promise<HttpAgent>;
@@ -0,0 +1,7 @@
1
+ import type { Principal } from "@dfinity/principal";
2
+ /**
3
+ * Convert a principal to a Uint8Array 32 length.
4
+ * e.g. Useful to convert a canister ID when topping up cycles with the Cmc canister
5
+ * @param principal The principal that needs to be converted to Subaccount
6
+ */
7
+ export declare const principalToSubAccount: (principal: Principal) => Uint8Array;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dfinity/utils",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "A collection of utilities and constants for NNS/SNS projects.",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/cjs/index.cjs.js",